use `has_cached_role?` instead of `has_role?`

This commit is contained in:
Andreas Nedbal 2022-12-18 03:21:25 +01:00 committed by Andreas Nedbal
parent 59f071bc04
commit 5a0e42fe66
9 changed files with 15 additions and 15 deletions

View File

@ -37,7 +37,7 @@ class Ajax::ModerationController < AjaxController
unban = params[:ban] == '0' unban = params[:ban] == '0'
perma = params[:duration].blank? perma = params[:duration].blank?
if !unban && target_user.has_role?(:administrator) if !unban && target_user.has_cached_role?(:administrator)
@response[:status] = :nopriv @response[:status] = :nopriv
@response[:message] = t(".nopriv") @response[:message] = t(".nopriv")
return return
@ -88,7 +88,7 @@ class Ajax::ModerationController < AjaxController
@response[:message] = t(".error") @response[:message] = t(".error")
return unless %w(moderator admin).include? params[:type].downcase return unless %w(moderator admin).include? params[:type].downcase
unless current_user.has_role?(:administrator) unless current_user.has_cached_role?(:administrator)
@response[:status] = :nopriv @response[:status] = :nopriv
@response[:message] = t(".nopriv") @response[:message] = t(".nopriv")
return return

View File

@ -6,7 +6,7 @@ class FeedbackController < ApplicationController
before_action :canny_consent_given?, only: %w[features bugs] before_action :canny_consent_given?, only: %w[features bugs]
def consent def consent
redirect_to feedback_bugs_path if current_user.has_role? :canny_consent redirect_to feedback_bugs_path if current_user.has_cached_role? :canny_consent
end end
def update def update
@ -27,6 +27,6 @@ class FeedbackController < ApplicationController
end end
def canny_consent_given? def canny_consent_given?
redirect_to feedback_consent_path unless current_user.has_role? :canny_consent redirect_to feedback_consent_path unless current_user.has_cached_role? :canny_consent
end end
end end

View File

@ -138,11 +138,11 @@ class User < ApplicationRecord
# @return [Boolean] is the user a moderator? # @return [Boolean] is the user a moderator?
def mod? def mod?
has_role?(:moderator) || has_role?(:administrator) has_cached_role?(:moderator) || has_cached_role?(:administrator)
end end
def admin? def admin?
has_role?(:administrator) has_cached_role?(:administrator)
end end
# region stuff used for reporting/moderation # region stuff used for reporting/moderation

View File

@ -14,7 +14,7 @@
%a{ href: question_path(i.question.user.screen_name, i.question.id) } %a{ href: question_path(i.question.user.screen_name, i.question.id) }
= t(".answers", count: i.question.answer_count) = t(".answers", count: i.question.answer_count)
.answerbox__question-text= question_markdown i.question.content .answerbox__question-text= question_markdown i.question.content
- if i.question.user_id != current_user.id || current_user.has_role?(:administrator) - if i.question.user_id != current_user.id || current_user.has_cached_role?(:administrator)
.pull-right .pull-right
.btn-group .btn-group
%button.btn.btn-default.btn-sm.dropdown-toggle{ data: { toggle: :dropdown }, aria: { expanded: false } } %button.btn.btn-default.btn-sm.dropdown-toggle{ data: { toggle: :dropdown }, aria: { expanded: false } }

View File

@ -8,7 +8,7 @@
%span{ aria: { hidden: true } } × %span{ aria: { hidden: true } } ×
%span.sr-only= t("voc.close") %span.sr-only= t("voc.close")
%ul.list-group %ul.list-group
- if current_user.has_role?(:administrator) - if current_user.has_cached_role?(:administrator)
= render "modal/privileges/item", privilege: "moderator", description: t(".role.moderator"), user: user = render "modal/privileges/item", privilege: "moderator", description: t(".role.moderator"), user: user
= render "modal/privileges/item", privilege: "admin", description: t(".role.admin"), user: user = render "modal/privileges/item", privilege: "admin", description: t(".role.admin"), user: user
.modal-footer .modal-footer

View File

@ -2,7 +2,7 @@
description ||= "" description ||= ""
role_mapping = { admin: "administrator" } role_mapping = { admin: "administrator" }
requires_role = %w[admin moderator].include?(privilege) requires_role = %w[admin moderator].include?(privilege)
checked = requires_role ? user.has_role?(role_mapping.fetch(privilege, privilege).to_sym) : user.public_send("#{privilege}?") checked = requires_role ? user.has_cached_role?(role_mapping.fetch(privilege, privilege).to_sym) : user.public_send("#{privilege}?")
%li.list-group-item{ id: "privilege-#{privilege}" } %li.list-group-item{ id: "privilege-#{privilege}" }
.media .media
.pull-left .pull-left

View File

@ -19,7 +19,7 @@
%a.dropdown-item{ href: moderation_reports_path } %a.dropdown-item{ href: moderation_reports_path }
%i.fa.fa-fw.fa-gavel %i.fa.fa-fw.fa-gavel
= t(".moderation") = t(".moderation")
- if current_user.has_role?(:administrator) - if current_user.has_cached_role?(:administrator)
%a.dropdown-item{ href: admin_dashboard_path } %a.dropdown-item{ href: admin_dashboard_path }
%i.fa.fa-fw.fa-cogs %i.fa.fa-fw.fa-cogs
= t(".admin") = t(".admin")

View File

@ -32,7 +32,7 @@
%a.dropdown-item{ href: '#', data: { target: '#modal-privileges', toggle: :modal } } %a.dropdown-item{ href: '#', data: { target: '#modal-privileges', toggle: :modal } }
%i.fa.fa-wrench %i.fa.fa-wrench
= raw t(".privilege", user: user.screen_name) = raw t(".privilege", user: user.screen_name)
- unless user.has_role?(:administrator) - unless user.has_cached_role?(:administrator)
%a.dropdown-item{ href: '#', data: { target: '#modal-ban', toggle: :modal } } %a.dropdown-item{ href: '#', data: { target: '#modal-ban', toggle: :modal } }
%i.fa.fa-ban %i.fa.fa-ban
= t(".ban") = t(".ban")
@ -40,7 +40,7 @@
%a.dropdown-item{ href: mod_inbox_index_path(user: user.screen_name) } %a.dropdown-item{ href: mod_inbox_index_path(user: user.screen_name) }
%i.fa.fa-inbox %i.fa.fa-inbox
= t(".view_inbox") = t(".view_inbox")
- if current_user.has_role? :administrator - if current_user.has_cached_role? :administrator
%a.dropdown-item{ href: rails_admin_path_for_resource(user), target: '_blank' } %a.dropdown-item{ href: rails_admin_path_for_resource(user), target: '_blank' }
%i.fa.fa-cogs %i.fa.fa-cogs
= t("voc.view_in_rails_admin") = t("voc.view_in_rails_admin")

View File

@ -17,12 +17,12 @@
- if user.following? current_user - if user.following? current_user
.badge.badge-light .badge.badge-light
= t(".badge.follows_you") = t(".badge.follows_you")
- if user_signed_in? && current_user.has_role?(:administrator) - if user_signed_in? && current_user.has_cached_role?(:administrator)
- if user.has_role?(:administrator) - if user.has_cached_role?(:administrator)
%span.badge.badge-danger %span.badge.badge-danger
%i.fa.fa-fw.fa-flask %i.fa.fa-fw.fa-flask
= t(".badge.admin") = t(".badge.admin")
- if user.has_role?(:moderator) - if user.has_cached_role?(:moderator)
%span.badge.badge-success %span.badge.badge-success
%i.fa.fa-fw.fa-users %i.fa.fa-fw.fa-users
= t(".badge.moderator") = t(".badge.moderator")