11# typed: strict
22
33class User < ApplicationRecord
4+ rolify
5+ devise :database_authenticatable , :registerable ,
6+ :recoverable , :rememberable , :validatable ,
7+ :trackable , :lockable , :confirmable
8+
49 has_many :api_keys , dependent : :delete_all
510 has_many :archive_items , foreign_key : :submitter_id , dependent : :nullify
611
@@ -9,17 +14,6 @@ class User < ApplicationRecord
914
1015 has_one :applicant , dependent : :destroy
1116
12- # Include default devise modules. Others available are:
13- # :timeoutable and :omniauthable
14- devise :database_authenticatable , :registerable ,
15- :recoverable , :rememberable , :validatable ,
16- :trackable , :lockable , :confirmable
17-
18- sig { returns ( T ::Boolean ) }
19- def super_admin?
20- self . super_admin
21- end
22-
2317 # `Devise::Recoverable#set_reset_password_token` is a protected method, which prevents us from
2418 # calling it directly. Since we need to be able to do that for tests and for duck-punching other
2519 # `Devise::Recoverable` methods, we pull it into the public space here.
@@ -33,7 +27,7 @@ def set_reset_password_token
3327 # Like the original method, it also creates the user's `reset_password_token`.
3428 sig { returns ( String ) }
3529 def send_setup_instructions
36- raise AlreadySetupError if sign_in_count . positive ?
30+ raise AlreadySetupError unless self . is_new_user ?
3731
3832 token = set_reset_password_token
3933
@@ -54,7 +48,7 @@ def send_setup_instructions
5448 def self . create_from_applicant ( applicant )
5549 raise ApplicantNotApprovedError unless applicant . approved?
5650
57- self . create! ( {
51+ user = self . create! ( {
5852 applicant : applicant ,
5953 email : applicant . email ,
6054 # The user will have to change their password immediately. This is just to pass validation.
@@ -64,6 +58,20 @@ def self.create_from_applicant(applicant)
6458 confirmed_at : applicant . confirmed_at ,
6559 confirmation_sent_at : applicant . confirmation_sent_at
6660 } )
61+
62+ user . assign_default_roles
63+
64+ user
65+ end
66+
67+ # All new users are implicitly Insights users.
68+ # All new users are also "new" until they have completed their initial setup.
69+ sig { void }
70+ def assign_default_roles
71+ if self . roles . blank?
72+ self . add_role :new_user
73+ self . add_role :insights_user
74+ end
6775 end
6876end
6977
0 commit comments