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,
|
2024-03-14 16:13:31 -07:00
|
|
|
omniauth_providers: [:neopass]
|
2023-08-06 15:52:05 -07:00
|
|
|
|
|
|
|
validates :name, presence: true, uniqueness: {case_sensitive: false},
|
2024-04-01 05:47:00 -07:00
|
|
|
length: {maximum: 30}
|
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
|
2024-03-14 19:11:06 -07:00
|
|
|
|
|
|
|
def uses_omniauth?
|
|
|
|
provider? && uid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def email_required?
|
|
|
|
!uses_omniauth?
|
|
|
|
end
|
|
|
|
|
|
|
|
def password_required?
|
Oops, stop requiring a new password whenever AuthUser is changed
Ah right, I went and checked the Devise source code, and the default
implementation for `password_required?` is a bit trickier than I
expected:
```ruby
def password_required?
!persisted? || !password.nil? || !password_confirmation.nil?
end
```
Looks like `super` does a good enough job here, though! (I'm actually
kinda surprised, I wasn't sure how Ruby's `super` rules worked, and
this isn't a subclass thing—or maybe it is, maybe the `devise` method
adds a mixin? Idk! But it does what I expect, so, great!)
So now, we require the password if 1) Devise doesn't see a UI reason
not to, *and* 2) the user isn't using OmniAuth (i.e. NeoPass).
This had caused a bug where it was impossible to use the Settings page
*without* changing your password! (The form says it's okay to leave it
blank, which stopped being true! But now it's fixed!)
2024-03-14 19:19:56 -07:00
|
|
|
super && !uses_omniauth?
|
2024-03-14 19:11:06 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.from_omniauth(auth)
|
|
|
|
raise MissingAuthInfoError, "Username missing" if auth.uid.blank?
|
|
|
|
raise MissingAuthInfoError, "Email missing" if auth.info.email.blank?
|
|
|
|
|
|
|
|
transaction do
|
|
|
|
find_or_create_by!(provider: auth.provider, uid: auth.uid) do |user|
|
|
|
|
# Use the Neopets username if possible, or a unique username if not.
|
|
|
|
dti_username = build_unique_username_like(auth.uid)
|
|
|
|
user.name = dti_username
|
|
|
|
|
|
|
|
# Copy the email address from their Neopets account to their DTI
|
|
|
|
# account, unless they already have a DTI account with this email, in
|
|
|
|
# which case, ignore it. (It's primarily for their own convenience with
|
|
|
|
# password recovery!)
|
|
|
|
email_exists = AuthUser.where(email: auth.info.email).exists?
|
|
|
|
user.email = auth.info.email unless email_exists
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.build_unique_username_like(name)
|
|
|
|
name_query = sanitize_sql_like(name) + "%"
|
|
|
|
similar_names = where("name LIKE ?", name_query).pluck(:name)
|
|
|
|
|
|
|
|
# Use the given name itself, if we can.
|
|
|
|
return name unless similar_names.include?(name)
|
|
|
|
|
|
|
|
# If not, try appending "-neopass".
|
|
|
|
return "#{name}-neopass" unless similar_names.include?("#{name}-neopass")
|
|
|
|
|
|
|
|
# After that, try appending "-neopass-1", "-neopass-2", etc, until a
|
|
|
|
# unique name arises. (We don't expect this to happen basically ever, but
|
|
|
|
# it's nice to have a guarantee!)
|
|
|
|
max = similar_names.size + 1
|
|
|
|
candidates = (1..max).map { |n| "#{name}-neopass-#{n}"}
|
|
|
|
numerical_name = candidates.find { |name| !similar_names.include?(name) }
|
|
|
|
return numerical_name unless numerical_name.nil?
|
|
|
|
|
|
|
|
raise "Failed to build unique username (shouldn't be possible?)"
|
|
|
|
end
|
|
|
|
|
|
|
|
class MissingAuthInfoError < ArgumentError;end
|
2023-08-03 17:14:09 -07:00
|
|
|
end
|