2023-08-03 17:14:09 -07:00
|
|
|
class AuthUser < AuthRecord
|
|
|
|
self.table_name = 'users'
|
2023-08-06 15:52:05 -07:00
|
|
|
|
2023-08-06 18:03:06 -07:00
|
|
|
devise :database_authenticatable, :encryptable, :registerable, :validatable,
|
2024-03-14 15:34:24 -07:00
|
|
|
:rememberable, :trackable, :recoverable, :omniauthable,
|
|
|
|
omniauth_providers: [:developer]
|
2023-08-06 15:52:05 -07:00
|
|
|
|
|
|
|
validates :name, presence: true, uniqueness: {case_sensitive: false},
|
|
|
|
length: {maximum: 20}
|
2023-08-06 16:08:13 -07:00
|
|
|
|
2023-08-06 16:23:22 -07:00
|
|
|
has_one :user, foreign_key: :remote_id, inverse_of: :auth_user
|
|
|
|
|
|
|
|
# It's important to keep AuthUser and User in sync. When we create an AuthUser
|
|
|
|
# (e.g. through the registration process), we create a matching User, too. And
|
|
|
|
# when the AuthUser's name changes, we update User to match.
|
|
|
|
#
|
|
|
|
# TODO: Should we sync deletions too? We don't do deletions anywhere in app
|
|
|
|
# right now, so I'll hold off to avoid leaving dead code around.
|
2023-08-06 18:35:52 -07:00
|
|
|
after_create :create_user!
|
|
|
|
after_update :sync_name_with_user!, if: :saved_change_to_name?
|
2023-08-06 16:23:22 -07:00
|
|
|
|
2023-08-06 18:35:52 -07:00
|
|
|
def create_user!
|
|
|
|
User.create!(name: name, auth_server_id: 1, remote_id: id)
|
2023-08-06 16:08:13 -07:00
|
|
|
end
|
2023-08-06 16:23:22 -07:00
|
|
|
|
2023-08-06 18:35:52 -07:00
|
|
|
def sync_name_with_user!
|
2023-08-06 16:23:22 -07:00
|
|
|
user.name = name
|
|
|
|
user.save!
|
|
|
|
end
|
2023-08-03 17:14:09 -07:00
|
|
|
end
|