forked from External/greenlight
Reduce number of roles queries for the admin controller (#631)
This commit is contained in:
parent
5a3ad3159c
commit
8c63f793a5
|
@ -162,7 +162,7 @@ class AdminsController < ApplicationController
|
||||||
private
|
private
|
||||||
|
|
||||||
def find_user
|
def find_user
|
||||||
@user = User.find_by!(uid: params[:user_uid])
|
@user = User.where(uid: params[:user_uid]).includes(:roles).first
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_setting
|
def find_setting
|
||||||
|
@ -176,10 +176,10 @@ class AdminsController < ApplicationController
|
||||||
|
|
||||||
# Gets the list of users based on your configuration
|
# Gets the list of users based on your configuration
|
||||||
def user_list
|
def user_list
|
||||||
initial_list = if current_user.has_role? :super_admin
|
initial_list = if current_user.has_cached_role? :super_admin
|
||||||
User.where.not(id: current_user.id)
|
User.where.not(id: current_user.id).includes(:roles)
|
||||||
else
|
else
|
||||||
User.without_role(:super_admin).where.not(id: current_user.id)
|
User.without_role(:super_admin).where.not(id: current_user.id).includes(:roles)
|
||||||
end
|
end
|
||||||
|
|
||||||
list = @role.present? ? initial_list.with_role(@role.to_sym) : initial_list
|
list = @role.present? ? initial_list.with_role(@role.to_sym) : initial_list
|
||||||
|
|
|
@ -116,7 +116,7 @@ class ApplicationController < ActionController::Base
|
||||||
|
|
||||||
# Checks to make sure that the admin has changed his password from the default
|
# Checks to make sure that the admin has changed his password from the default
|
||||||
def check_admin_password
|
def check_admin_password
|
||||||
if current_user&.has_role?(:admin) && current_user&.greenlight_account? &&
|
if current_user&.has_cached_role?(:admin) && current_user&.greenlight_account? &&
|
||||||
current_user&.authenticate(Rails.configuration.admin_password_default)
|
current_user&.authenticate(Rails.configuration.admin_password_default)
|
||||||
|
|
||||||
flash.now[:alert] = I18n.t("default_admin",
|
flash.now[:alert] = I18n.t("default_admin",
|
||||||
|
@ -156,10 +156,10 @@ class ApplicationController < ActionController::Base
|
||||||
|
|
||||||
# Checks if the user is banned and logs him out if he is
|
# Checks if the user is banned and logs him out if he is
|
||||||
def check_user_role
|
def check_user_role
|
||||||
if current_user&.has_role? :denied
|
if current_user&.has_cached_role? :denied
|
||||||
session.delete(:user_id)
|
session.delete(:user_id)
|
||||||
redirect_to root_path, flash: { alert: I18n.t("registration.banned.fail") }
|
redirect_to root_path, flash: { alert: I18n.t("registration.banned.fail") }
|
||||||
elsif current_user&.has_role? :pending
|
elsif current_user&.has_cached_role? :pending
|
||||||
session.delete(:user_id)
|
session.delete(:user_id)
|
||||||
redirect_to root_path, flash: { alert: I18n.t("registration.approval.fail") }
|
redirect_to root_path, flash: { alert: I18n.t("registration.approval.fail") }
|
||||||
end
|
end
|
||||||
|
|
|
@ -274,7 +274,7 @@ class RoomsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def verify_user_not_admin
|
def verify_user_not_admin
|
||||||
redirect_to admins_path if current_user && current_user&.has_role?(:super_admin)
|
redirect_to admins_path if current_user && current_user&.has_cached_role?(:super_admin)
|
||||||
end
|
end
|
||||||
|
|
||||||
def auth_required
|
def auth_required
|
||||||
|
@ -287,7 +287,7 @@ class RoomsController < ApplicationController
|
||||||
|
|
||||||
# Does not apply to admin
|
# Does not apply to admin
|
||||||
# 15+ option is used as unlimited
|
# 15+ option is used as unlimited
|
||||||
return false if current_user&.has_role?(:admin) || limit == 15
|
return false if current_user&.has_cached_role?(:admin) || limit == 15
|
||||||
|
|
||||||
current_user.rooms.count >= limit
|
current_user.rooms.count >= limit
|
||||||
end
|
end
|
||||||
|
|
|
@ -185,7 +185,7 @@ class UsersController < ApplicationController
|
||||||
private
|
private
|
||||||
|
|
||||||
def find_user
|
def find_user
|
||||||
@user = User.find_by!(uid: params[:user_uid])
|
@user = User.where(uid: params[:user_uid]).includes(:roles).first
|
||||||
end
|
end
|
||||||
|
|
||||||
def ensure_unauthenticated
|
def ensure_unauthenticated
|
||||||
|
|
|
@ -106,7 +106,7 @@ module ApplicationHelper
|
||||||
# Returns the page that the logo redirects to when clicked on
|
# Returns the page that the logo redirects to when clicked on
|
||||||
def home_page
|
def home_page
|
||||||
return root_path unless current_user
|
return root_path unless current_user
|
||||||
return admins_path if current_user.has_role? :super_admin
|
return admins_path if current_user.has_cached_role? :super_admin
|
||||||
current_user.main_room
|
current_user.main_room
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,7 +37,7 @@ module RoomsHelper
|
||||||
|
|
||||||
# Does not apply to admin or users that aren't signed in
|
# Does not apply to admin or users that aren't signed in
|
||||||
# 15+ option is used as unlimited
|
# 15+ option is used as unlimited
|
||||||
return false if current_user&.has_role?(:admin) || limit == 15
|
return false if current_user&.has_cached_role?(:admin) || limit == 15
|
||||||
|
|
||||||
current_user.rooms.length >= limit
|
current_user.rooms.length >= limit
|
||||||
end
|
end
|
||||||
|
@ -46,7 +46,7 @@ module RoomsHelper
|
||||||
# Get how many rooms need to be deleted to reach allowed room number
|
# Get how many rooms need to be deleted to reach allowed room number
|
||||||
limit = Setting.find_or_create_by!(provider: user_settings_provider).get_value("Room Limit").to_i
|
limit = Setting.find_or_create_by!(provider: user_settings_provider).get_value("Room Limit").to_i
|
||||||
|
|
||||||
return false if current_user&.has_role?(:admin) || limit == 15
|
return false if current_user&.has_cached_role?(:admin) || limit == 15
|
||||||
|
|
||||||
@diff = current_user.rooms.count - limit
|
@diff = current_user.rooms.count - limit
|
||||||
@diff.positive? && current_user.rooms.pluck(:id).index(room.id) + 1 > limit
|
@diff.positive? && current_user.rooms.pluck(:id).index(room.id) + 1 > limit
|
||||||
|
|
|
@ -61,7 +61,7 @@ module SessionsHelper
|
||||||
|
|
||||||
# Retrieves the current user.
|
# Retrieves the current user.
|
||||||
def current_user
|
def current_user
|
||||||
@current_user ||= User.find_by(id: session[:user_id])
|
@current_user ||= User.where(id: session[:user_id]).includes(:roles).first
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_checksum(user_domain, redirect_url, secret)
|
def generate_checksum(user_domain, redirect_url, secret)
|
||||||
|
|
|
@ -31,7 +31,7 @@ module ThemingHelper
|
||||||
|
|
||||||
# Returns the user's provider in the settings context
|
# Returns the user's provider in the settings context
|
||||||
def user_settings_provider
|
def user_settings_provider
|
||||||
if Rails.configuration.loadbalanced_configuration && current_user && !current_user&.has_role?(:super_admin)
|
if Rails.configuration.loadbalanced_configuration && current_user && !current_user&.has_cached_role?(:super_admin)
|
||||||
current_user.provider
|
current_user.provider
|
||||||
elsif Rails.configuration.loadbalanced_configuration
|
elsif Rails.configuration.loadbalanced_configuration
|
||||||
@user_domain
|
@user_domain
|
||||||
|
|
|
@ -214,13 +214,17 @@ class User < ApplicationRecord
|
||||||
|
|
||||||
def admin_of?(user)
|
def admin_of?(user)
|
||||||
if Rails.configuration.loadbalanced_configuration
|
if Rails.configuration.loadbalanced_configuration
|
||||||
if has_role? :super_admin
|
# Pulls in the user roles if they weren't request in the original request
|
||||||
|
# So the has_cached_role? doesn't always return false
|
||||||
|
user.roles
|
||||||
|
if has_cached_role? :super_admin
|
||||||
id != user.id
|
id != user.id
|
||||||
else
|
else
|
||||||
(has_role? :admin) && (id != user.id) && (provider == user.provider) && (!user.has_role? :super_admin)
|
(has_cached_role? :admin) && (id != user.id) && (provider == user.provider) &&
|
||||||
|
(!user.has_cached_role? :super_admin)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
((has_role? :admin) || (has_role? :super_admin)) && (id != user.id)
|
((has_cached_role? :admin) || (has_cached_role? :super_admin)) && (id != user.id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
<div class="d-flex ml-auto">
|
<div class="d-flex ml-auto">
|
||||||
<% if current_user %>
|
<% if current_user %>
|
||||||
|
|
||||||
<% if current_user.has_role? :super_admin %>
|
<% if current_user.has_cached_role? :super_admin %>
|
||||||
<% admins_page = params[:controller] == "admins" && params[:action] == "index" ? "active" : "" %>
|
<% admins_page = params[:controller] == "admins" && params[:action] == "index" ? "active" : "" %>
|
||||||
<%= link_to admins_path, class: "px-3 mx-1 mt-1 header-nav #{admins_page}" do %>
|
<%= link_to admins_path, class: "px-3 mx-1 mt-1 header-nav #{admins_page}" do %>
|
||||||
<i class="fas fa-home pr-1 "></i> <%= t("header.dropdown.home") %>
|
<i class="fas fa-home pr-1 "></i> <%= t("header.dropdown.home") %>
|
||||||
|
@ -56,7 +56,7 @@
|
||||||
<%= link_to edit_user_path(current_user), class: "dropdown-item" do %>
|
<%= link_to edit_user_path(current_user), class: "dropdown-item" do %>
|
||||||
<i class="dropdown-icon fas fa-id-card mr-3"></i><%= t("header.dropdown.settings") %>
|
<i class="dropdown-icon fas fa-id-card mr-3"></i><%= t("header.dropdown.settings") %>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% if current_user.has_role? :admin %>
|
<% if current_user.has_cached_role? :admin %>
|
||||||
<%= link_to admins_path, class: "dropdown-item" do %>
|
<%= link_to admins_path, class: "dropdown-item" do %>
|
||||||
<i class="dropdown-icon fas fa-user-tie mr-3"></i><%= t("header.dropdown.account_settings") %>
|
<i class="dropdown-icon fas fa-user-tie mr-3"></i><%= t("header.dropdown.account_settings") %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
Loading…
Reference in New Issue