مراية لـ
https://github.com/postalserver/postal.git
تم المزامنة 2025-12-01 05:43:04 +00:00
refactor: user management
هذا الالتزام موجود في:
@@ -69,20 +69,6 @@ class Organization < ApplicationRecord
|
||||
suspended_at.present?
|
||||
end
|
||||
|
||||
def admin?(user)
|
||||
user.admin? ||
|
||||
!!(owner?(user) || user_assignment(user)&.admin?)
|
||||
end
|
||||
|
||||
def owner?(user)
|
||||
self.owner == user
|
||||
end
|
||||
|
||||
def accessible_by?(user)
|
||||
user.admin? ||
|
||||
!!(user_assignment(user))
|
||||
end
|
||||
|
||||
def user_assignment(user)
|
||||
@user_assignments ||= {}
|
||||
@user_assignments[user.id] ||= organization_users.where(:user => user).first
|
||||
|
||||
@@ -16,43 +16,4 @@ class OrganizationUser < ApplicationRecord
|
||||
belongs_to :organization
|
||||
belongs_to :user, :polymorphic => true, :optional => true
|
||||
|
||||
validate :validate_uniqueness
|
||||
|
||||
before_create :create_user_invite
|
||||
after_destroy :remove_user_invites
|
||||
|
||||
def email_address
|
||||
@email_address ||= user&.email_address
|
||||
end
|
||||
|
||||
def email_address=(value)
|
||||
@email_address = value
|
||||
end
|
||||
|
||||
def create_user_invite
|
||||
if self.user.nil?
|
||||
user = UserInvite.where(:email_address => @email_address).first_or_initialize
|
||||
if user.save
|
||||
self.user = user
|
||||
else
|
||||
errors.add :base, user.errors.full_messages.to_sentence
|
||||
throw :abort
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def validate_uniqueness
|
||||
if self.email_address.present?
|
||||
if organization.organization_users.where.not(:id => self.id).any? { |ou| ou.user.email_address.upcase == self.email_address.upcase }
|
||||
errors.add :email_address, "is already assigned or has an pending invite"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def remove_user_invites
|
||||
if self.user.is_a?(UserInvite) && self.user.organizations.empty?
|
||||
self.user.destroy
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -119,10 +119,6 @@ class Server < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def accessible_by?(user)
|
||||
organization.accessible_by?(user)
|
||||
end
|
||||
|
||||
def to_param
|
||||
permalink
|
||||
end
|
||||
|
||||
@@ -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
|
||||
|
||||
المرجع في مشكلة جديدة
حظر مستخدم