From a3a10237f7cbd4586086133a926dc4dd9fd5b7bf Mon Sep 17 00:00:00 2001 From: Andy Allan Date: Wed, 28 Nov 2018 17:20:13 +0100 Subject: [PATCH] Use CanCanCan for user_roles auth --- app/abilities/ability.rb | 1 + app/controllers/user_roles_controller.rb | 15 +++--------- config/locales/en.yml | 1 - test/abilities/abilities_test.rb | 24 +++++++++++++++++++ .../controllers/user_roles_controller_test.rb | 6 ++--- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/app/abilities/ability.rb b/app/abilities/ability.rb index f2486d2b4..707272182 100644 --- a/app/abilities/ability.rb +++ b/app/abilities/ability.rb @@ -34,6 +34,7 @@ class Ability can [:hide, :hidecomment], [DiaryEntry, DiaryComment] can [:index, :show, :resolve, :ignore, :reopen], Issue can :create, IssueComment + can [:grant, :revoke], UserRole end end diff --git a/app/controllers/user_roles_controller.rb b/app/controllers/user_roles_controller.rb index 732a2bb2c..fe4c855e3 100644 --- a/app/controllers/user_roles_controller.rb +++ b/app/controllers/user_roles_controller.rb @@ -2,9 +2,10 @@ class UserRolesController < ApplicationController layout "site" before_action :authorize_web - before_action :require_user + + authorize_resource + before_action :lookup_user - before_action :require_administrator before_action :require_valid_role before_action :not_in_role, :only => [:grant] before_action :in_role, :only => [:revoke] @@ -26,16 +27,6 @@ class UserRolesController < ApplicationController private - ## - # require that the user is an administrator, or fill out a helpful error message - # and return them to theuser page. - def require_administrator - unless current_user.administrator? - flash[:error] = t "user_role.filter.not_an_administrator" - redirect_to user_path(@user) - end - end - ## # require that the given role is valid. the role is a URL # parameter, so should always be present. diff --git a/config/locales/en.yml b/config/locales/en.yml index 2777c1df3..92d56919c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2218,7 +2218,6 @@ en: with your ID in your user settings. user_role: filter: - not_an_administrator: "Only administrators can perform user role management, and you are not an administrator." not_a_role: "The string `%{role}' is not a valid role." already_has_role: "The user already has role %{role}." doesnt_have_role: "The user does not have role %{role}." diff --git a/test/abilities/abilities_test.rb b/test/abilities/abilities_test.rb index 9444a45f5..b951e23e5 100644 --- a/test/abilities/abilities_test.rb +++ b/test/abilities/abilities_test.rb @@ -38,6 +38,14 @@ class GuestAbilityTest < AbilityTest assert ability.cannot?(action, Note), "should not be able to #{action} Notes" end end + + test "user roles permissions for a guest" do + ability = Ability.new nil + + [:grant, :revoke].each do |action| + assert ability.cannot?(action, UserRole), "should not be able to #{action} UserRoles" + end + end end class UserAbilityTest < AbilityTest @@ -87,6 +95,14 @@ class ModeratorAbilityTest < AbilityTest assert ability.can?(action, Note), "should be able to #{action} Notes" end end + + test "User Roles permissions" do + ability = Ability.new create(:moderator_user) + + [:grant, :revoke].each do |action| + assert ability.cannot?(action, UserRole), "should not be able to #{action} UserRoles" + end + end end class AdministratorAbilityTest < AbilityTest @@ -100,4 +116,12 @@ class AdministratorAbilityTest < AbilityTest assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComment" end end + + test "User Roles permissions for an administrator" do + ability = Ability.new create(:administrator_user) + + [:grant, :revoke].each do |action| + assert ability.can?(action, UserRole), "should be able to #{action} UserRoles" + end + end end diff --git a/test/controllers/user_roles_controller_test.rb b/test/controllers/user_roles_controller_test.rb index e9aadcc3a..ba3d795de 100644 --- a/test/controllers/user_roles_controller_test.rb +++ b/test/controllers/user_roles_controller_test.rb @@ -31,8 +31,7 @@ class UserRolesControllerTest < ActionController::TestCase # Granting should still fail post :grant, :params => { :display_name => target_user.display_name, :role => "moderator" } - assert_redirected_to user_path(target_user) - assert_equal "Only administrators can perform user role management, and you are not an administrator.", flash[:error] + assert_redirected_to :controller => :errors, :action => :forbidden # Login as an administrator session[:user] = administrator_user.id @@ -92,8 +91,7 @@ class UserRolesControllerTest < ActionController::TestCase # Revoking should still fail post :revoke, :params => { :display_name => target_user.display_name, :role => "moderator" } - assert_redirected_to user_path(target_user) - assert_equal "Only administrators can perform user role management, and you are not an administrator.", flash[:error] + assert_redirected_to :controller => :errors, :action => :forbidden # Login as an administrator session[:user] = administrator_user.id -- 2.43.2