forked from External/greenlight
Refactored code to reduce number of database queries (#960)
This commit is contained in:
@ -20,7 +20,7 @@ class Role < ApplicationRecord
|
||||
has_and_belongs_to_many :users, join_table: :users_roles
|
||||
has_many :role_permissions
|
||||
|
||||
default_scope { order(:priority) }
|
||||
default_scope { includes(:role_permissions) }
|
||||
scope :by_priority, -> { order(:priority) }
|
||||
scope :editable_roles, ->(provider) { where(provider: provider).where.not(name: %w[super_admin denied pending]) }
|
||||
|
||||
@ -85,23 +85,36 @@ class Role < ApplicationRecord
|
||||
|
||||
# Returns the value if enabled or the default if not enabled
|
||||
def get_permission(name, return_boolean = true)
|
||||
permission = role_permissions.find_or_create_by!(name: name)
|
||||
value = nil
|
||||
|
||||
value = if permission[:enabled]
|
||||
permission[:value]
|
||||
else
|
||||
case name
|
||||
when "can_appear_in_share_list"
|
||||
Rails.configuration.shared_access_default.to_s
|
||||
role_permissions.each do |permission|
|
||||
next if permission.name != name
|
||||
|
||||
value = if permission.enabled
|
||||
permission.value
|
||||
else
|
||||
"false"
|
||||
default_value(name)
|
||||
end
|
||||
end
|
||||
|
||||
# Create the role_permissions since it doesn't exist
|
||||
role_permissions.create(name: name) if value.nil?
|
||||
|
||||
if return_boolean
|
||||
value == "true"
|
||||
else
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def default_value(name)
|
||||
case name
|
||||
when "can_appear_in_share_list"
|
||||
Rails.configuration.shared_access_default.to_s
|
||||
else
|
||||
"false"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -42,27 +42,21 @@ class Room < ApplicationRecord
|
||||
|
||||
search_param = "%#{string}%"
|
||||
|
||||
joins(:owner).where(search_query, search: search_param)
|
||||
where(search_query, search: search_param)
|
||||
end
|
||||
|
||||
def self.admins_order(column, direction)
|
||||
# Include the owner of the table
|
||||
table = joins(:owner)
|
||||
|
||||
return table.order(Arel.sql("#{column} #{direction}")) if table.column_names.include?(column) || column == "users.name"
|
||||
return table.order(Arel.sql("rooms.#{column} #{direction}")) if table.column_names.include?(column) || column == "users.name"
|
||||
|
||||
table
|
||||
end
|
||||
|
||||
# Determines if a user owns a room.
|
||||
def owned_by?(user)
|
||||
return false if user.nil?
|
||||
user.rooms.include?(self)
|
||||
end
|
||||
|
||||
# Determines whether room is a home room
|
||||
def home_room?
|
||||
owner.main_room == self
|
||||
user_id == user&.id
|
||||
end
|
||||
|
||||
def shared_users
|
||||
|
@ -28,24 +28,36 @@ class Setting < ApplicationRecord
|
||||
|
||||
# Returns the value if enabled or the default if not enabled
|
||||
def get_value(name)
|
||||
feature = features.find_or_create_by!(name: name)
|
||||
if feature[:enabled]
|
||||
feature[:value]
|
||||
else
|
||||
case name
|
||||
when "Branding Image"
|
||||
Rails.configuration.branding_image_default
|
||||
when "Primary Color"
|
||||
Rails.configuration.primary_color_default
|
||||
when "Registration Method"
|
||||
Rails.configuration.registration_method_default
|
||||
when "Room Authentication"
|
||||
false
|
||||
when "Room Limit"
|
||||
Rails.configuration.number_of_rooms_default
|
||||
when "Shared Access"
|
||||
Rails.configuration.shared_access_default
|
||||
end
|
||||
# Return feature value if already exists
|
||||
features.each do |feature|
|
||||
next if feature.name != name
|
||||
|
||||
return feature.value if feature.enabled
|
||||
return default_value(name)
|
||||
end
|
||||
|
||||
# Create the feature since it doesn't exist
|
||||
features.create(name: name)
|
||||
default_value(name)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def default_value(name)
|
||||
# return default value
|
||||
case name
|
||||
when "Branding Image"
|
||||
Rails.configuration.branding_image_default
|
||||
when "Primary Color"
|
||||
Rails.configuration.primary_color_default
|
||||
when "Registration Method"
|
||||
Rails.configuration.registration_method_default
|
||||
when "Room Authentication"
|
||||
false
|
||||
when "Room Limit"
|
||||
Rails.configuration.number_of_rooms_default
|
||||
when "Shared Access"
|
||||
Rails.configuration.shared_access_default
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -32,7 +32,7 @@ class User < ApplicationRecord
|
||||
has_many :shared_access
|
||||
belongs_to :main_room, class_name: 'Room', foreign_key: :room_id, required: false
|
||||
|
||||
has_and_belongs_to_many :roles, -> { includes :role_permissions }, join_table: :users_roles
|
||||
has_and_belongs_to_many :roles, join_table: :users_roles
|
||||
|
||||
validates :name, length: { maximum: 256 }, presence: true
|
||||
validates :provider, presence: true
|
||||
@ -183,7 +183,7 @@ class User < ApplicationRecord
|
||||
|
||||
# role functions
|
||||
def highest_priority_role
|
||||
roles.by_priority.first
|
||||
roles.min_by(&:priority)
|
||||
end
|
||||
|
||||
def add_role(role)
|
||||
@ -217,7 +217,11 @@ class User < ApplicationRecord
|
||||
# rubocop:disable Naming/PredicateName
|
||||
def has_role?(role)
|
||||
# rubocop:enable Naming/PredicateName
|
||||
roles.exists?(name: role)
|
||||
roles.each do |single_role|
|
||||
return true if single_role.name.eql? role.to_s
|
||||
end
|
||||
|
||||
false
|
||||
end
|
||||
|
||||
def self.with_role(role)
|
||||
|
Reference in New Issue
Block a user