refactor: user management

This commit is contained in:
Adam Cooke
2021-07-28 14:17:59 +00:00
parent e9a52e9512
commit daf469ce7f
27 changed files with 106 additions and 359 deletions
+1 -31
View File
@@ -31,7 +31,7 @@ class User < ApplicationRecord
validates :first_name, :presence => true
validates :last_name, :presence => true
validates :email_address, :presence => true, :uniqueness => true, :format => {:with => /@/}
validates :email_address, :presence => true, :uniqueness => true, :format => {:with => /@/, allow_blank: true}
validates :time_zone, :presence => true
default_value :time_zone, -> { 'UTC' }
@@ -39,23 +39,6 @@ class User < ApplicationRecord
has_many :organization_users, :dependent => :destroy, :as => :user
has_many :organizations, :through => :organization_users
scope :verified, -> { where.not(:email_verified_at => nil) }
when_attribute :email_address, :changes_to => :anything do
before_save do |was, now|
unless self.new_record? && self.email_verified_at
self.email_verification_token = rand(999999).to_s.rjust(6, '0')
self.email_verified_at = nil
end
end
after_commit do |was, new|
if self.email_verified_at.nil? && was.present?
AppMailer.verify_email_address(self).deliver
end
end
end
def organizations_scope
@organizations_scope ||= begin
if self.admin?
@@ -74,15 +57,6 @@ class User < ApplicationRecord
uuid
end
def verify!
self.email_verified_at = Time.now
self.save!
end
def verified?
email_verified_at.present?
end
def md5_for_gravatar
@md5_for_gravatar ||= Digest::MD5.hexdigest(email_address.to_s.downcase)
end
@@ -95,10 +69,6 @@ class User < ApplicationRecord
"#{name} <#{email_address}>"
end
def generate_login_token
JWT.encode({'user' => self.id, 'timestamp' => Time.now.to_f}, Postal.signing_key.to_s, 'HS256')
end
def self.[](email)
where(:email_address => email).first
end