Skip to content

Commit

Permalink
fix(user model): email lookups are now case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach committed Aug 13, 2021
1 parent 2a93852 commit 32b7409
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class User

field :name, type: String
field :nickname, type: String
field :email, type: String, uniq: {scope: :authority_id}
field :email, type: String
field :phone, type: String
field :country, type: String
field :image, type: String
Expand Down Expand Up @@ -52,7 +52,7 @@ class User
has_many :access_grants, class_name: 'Doorkeeper::AccessGrant', dependent: :destroy, foreign_key: :resource_owner_id

def self.find_by_email(authority, email)
User.where(authority_id: authority, email: email).first
User.where(authority_id: authority, email: /#{email}/i).first
end

field :sys_admin, default: false
Expand Down Expand Up @@ -107,12 +107,16 @@ def email=(new_email)
super(new_email)

# For looking up user pictures without making the email public
self.email_digest = new_email ? Digest::MD5.hexdigest(new_email) : nil
self.email_digest = new_email ? Digest::MD5.hexdigest(new_email.downcase) : nil
end

protected

# Validations
validates :email, :email => true
validates :password, length: { minimum: 6, message: 'must be at least 6 characters' }, allow_blank: true
validates_each :email do |record, attr_name, value|
user = User.find_by_email(record.authority_id, value)
record.errors.add(attr_name, 'already exists, it must be unique') if user && user.id != record.id
end
end

0 comments on commit 32b7409

Please sign in to comment.