Move "everyone" role and "instance actor" account magic number IDs to constants (#29260)

This commit is contained in:
Matt Jankowski 2024-02-19 06:09:43 -05:00 committed by GitHub
parent 96ddf1d482
commit 245064bb98
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 26 additions and 19 deletions

View file

@ -38,6 +38,8 @@ class UserRole < ApplicationRecord
delete_user_data: (1 << 19),
}.freeze
EVERYONE_ROLE_ID = -99
module Flags
NONE = 0
ALL = FLAGS.values.reduce(&:|)
@ -94,7 +96,7 @@ class UserRole < ApplicationRecord
before_validation :set_position
scope :assignable, -> { where.not(id: -99).order(position: :asc) }
scope :assignable, -> { where.not(id: EVERYONE_ROLE_ID).order(position: :asc) }
has_many :users, inverse_of: :role, foreign_key: 'role_id', dependent: :nullify
@ -103,9 +105,9 @@ class UserRole < ApplicationRecord
end
def self.everyone
UserRole.find(-99)
UserRole.find(EVERYONE_ROLE_ID)
rescue ActiveRecord::RecordNotFound
UserRole.create!(id: -99, permissions: Flags::DEFAULT)
UserRole.create!(id: EVERYONE_ROLE_ID, permissions: Flags::DEFAULT)
end
def self.that_can(*any_of_privileges)
@ -113,7 +115,7 @@ class UserRole < ApplicationRecord
end
def everyone?
id == -99
id == EVERYONE_ROLE_ID
end
def nobody?