Use CanCanCan for user_roles auth
This commit is contained in:
parent
a790c47923
commit
a3a10237f7
5 changed files with 30 additions and 17 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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}."
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue