added AuthenticationToken model, updated some restrictions for the authentication token table, added dependent destroy to the user model for authentication tokens

This commit is contained in:
Janis4411
2022-08-02 13:57:44 +02:00
committed by Sebastian Serth
parent fe410a5306
commit d64daadd21
4 changed files with 39 additions and 1 deletions

View File

@ -0,0 +1,14 @@
# frozen_string_literal: true
require 'securerandom'
class AuthenticationToken < ApplicationRecord
include Creation
def self.generate!(user)
create!(
shared_secret: SecureRandom.hex(32),
user: user,
expire_at: 7.days.from_now
)
end
end

View File

@ -6,6 +6,7 @@ class User < ApplicationRecord
ROLES = %w[admin teacher learner].freeze
belongs_to :consumer
has_many :authentication_token, dependent: :destroy
has_many :study_group_memberships, as: :user
has_many :study_groups, through: :study_group_memberships, as: :user
has_many :exercises, as: :user

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
class CreateAuthenticationTokens < ActiveRecord::Migration[6.1]
def change
create_table :authentication_tokens, id: :uuid do |t|
t.string :shared_secret, null: false, index: {unique: true}
t.references :user, polymorphic: true, null: false
t.date :expire_at, null: false
t.timestamps
end
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_04_15_215112) do
ActiveRecord::Schema.define(version: 2022_07_21_131946) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
@ -30,6 +30,17 @@ ActiveRecord::Schema.define(version: 2022_04_15_215112) do
t.index ["user_type", "user_id"], name: "index_anomaly_notifications_on_user"
end
create_table "authentication_tokens", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "shared_secret", null: false
t.string "user_type", null: false
t.bigint "user_id", null: false
t.date "expire_at", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["shared_secret"], name: "index_authentication_tokens_on_shared_secret", unique: true
t.index ["user_type", "user_id"], name: "index_authentication_tokens_on_user"
end
create_table "codeharbor_links", id: :serial, force: :cascade do |t|
t.string "api_key"
t.datetime "created_at"