can [:hide, :hidecomment], [DiaryEntry, DiaryComment]
can [:index, :show, :resolve, :ignore, :reopen], Issue
can :create, IssueComment
+ can [:grant, :revoke], UserRole
end
end
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]
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.
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}."
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
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
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
# 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
# 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