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:

committed by
Sebastian Serth

parent
fe410a5306
commit
d64daadd21
14
app/models/authentication_token.rb
Normal file
14
app/models/authentication_token.rb
Normal 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
|
@ -6,6 +6,7 @@ class User < ApplicationRecord
|
|||||||
ROLES = %w[admin teacher learner].freeze
|
ROLES = %w[admin teacher learner].freeze
|
||||||
|
|
||||||
belongs_to :consumer
|
belongs_to :consumer
|
||||||
|
has_many :authentication_token, dependent: :destroy
|
||||||
has_many :study_group_memberships, as: :user
|
has_many :study_group_memberships, as: :user
|
||||||
has_many :study_groups, through: :study_group_memberships, as: :user
|
has_many :study_groups, through: :study_group_memberships, as: :user
|
||||||
has_many :exercises, as: :user
|
has_many :exercises, as: :user
|
||||||
|
12
db/migrate/20220721131946_create_authentication_tokens.rb
Normal file
12
db/migrate/20220721131946_create_authentication_tokens.rb
Normal 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
|
13
db/schema.rb
13
db/schema.rb
@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# 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
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "pg_trgm"
|
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"
|
t.index ["user_type", "user_id"], name: "index_anomaly_notifications_on_user"
|
||||||
end
|
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|
|
create_table "codeharbor_links", id: :serial, force: :cascade do |t|
|
||||||
t.string "api_key"
|
t.string "api_key"
|
||||||
t.datetime "created_at"
|
t.datetime "created_at"
|
||||||
|
Reference in New Issue
Block a user