diff --git a/app/controllers/admins_controller.rb b/app/controllers/admins_controller.rb index 00ab91bc..aa4977d3 100644 --- a/app/controllers/admins_controller.rb +++ b/app/controllers/admins_controller.rb @@ -162,7 +162,7 @@ class AdminsController < ApplicationController private def find_user - @user = User.find_by!(uid: params[:user_uid]) + @user = User.where(uid: params[:user_uid]).includes(:roles).first end def find_setting @@ -176,10 +176,10 @@ class AdminsController < ApplicationController # Gets the list of users based on your configuration def user_list - initial_list = if current_user.has_role? :super_admin - User.where.not(id: current_user.id) + initial_list = if current_user.has_cached_role? :super_admin + User.where.not(id: current_user.id).includes(:roles) 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 list = @role.present? ? initial_list.with_role(@role.to_sym) : initial_list diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 70df16a3..fc86a331 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -116,7 +116,7 @@ class ApplicationController < ActionController::Base # Checks to make sure that the admin has changed his password from the default 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) 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 def check_user_role - if current_user&.has_role? :denied + if current_user&.has_cached_role? :denied session.delete(:user_id) 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) redirect_to root_path, flash: { alert: I18n.t("registration.approval.fail") } end diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index fde621b1..e8e23540 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -274,7 +274,7 @@ class RoomsController < ApplicationController end 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 def auth_required @@ -287,7 +287,7 @@ class RoomsController < ApplicationController # Does not apply to admin # 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 end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index dd89d633..0224d905 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -185,7 +185,7 @@ class UsersController < ApplicationController private def find_user - @user = User.find_by!(uid: params[:user_uid]) + @user = User.where(uid: params[:user_uid]).includes(:roles).first end def ensure_unauthenticated diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index d947f54b..2f64206f 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -106,7 +106,7 @@ module ApplicationHelper # Returns the page that the logo redirects to when clicked on def home_page 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 end end diff --git a/app/helpers/rooms_helper.rb b/app/helpers/rooms_helper.rb index 009fd77d..26bfaf65 100644 --- a/app/helpers/rooms_helper.rb +++ b/app/helpers/rooms_helper.rb @@ -37,7 +37,7 @@ module RoomsHelper # Does not apply to admin or users that aren't signed in # 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 end @@ -46,7 +46,7 @@ module RoomsHelper # 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 - 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.positive? && current_user.rooms.pluck(:id).index(room.id) + 1 > limit diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb index c9ab3525..60652d4c 100644 --- a/app/helpers/sessions_helper.rb +++ b/app/helpers/sessions_helper.rb @@ -61,7 +61,7 @@ module SessionsHelper # Retrieves the 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 def generate_checksum(user_domain, redirect_url, secret) diff --git a/app/helpers/theming_helper.rb b/app/helpers/theming_helper.rb index e3f073fb..05479980 100644 --- a/app/helpers/theming_helper.rb +++ b/app/helpers/theming_helper.rb @@ -31,7 +31,7 @@ module ThemingHelper # Returns the user's provider in the settings context 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 elsif Rails.configuration.loadbalanced_configuration @user_domain diff --git a/app/models/user.rb b/app/models/user.rb index f7af9a35..ba914f9b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -214,13 +214,17 @@ class User < ApplicationRecord def admin_of?(user) 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 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 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 diff --git a/app/views/shared/_header.html.erb b/app/views/shared/_header.html.erb index 1228e2b4..3182142f 100755 --- a/app/views/shared/_header.html.erb +++ b/app/views/shared/_header.html.erb @@ -23,7 +23,7 @@