This repository has been archived by the owner on Jan 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract permission denied logic into object
- Loading branch information
Showing
2 changed files
with
46 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module RightOn | ||
class PermissionDeniedResponse | ||
attr_reader :right_allowed, :roles_allowed, :controller_name | ||
|
||
def initialize(params, controller_action_options) | ||
@params = params | ||
@right_allowed = Right.all.detect{|right| right.allowed?(controller_action_options)} | ||
@roles_allowed = @right_allowed.roles if @right_allowed | ||
@controller_name = @params[:controller] unless @right_allowed | ||
end | ||
|
||
def text_message | ||
if @right_allowed | ||
<<-MESSAGE | ||
You are not authorised to perform the requested operation. | ||
Right required: #{@right_allowed} | ||
This right is given to the following roles: #{@roles_allowed.map(&:title).join(", ")}. | ||
Contact your system manager to be given this right. | ||
MESSAGE | ||
else | ||
no_right_for_page | ||
end | ||
end | ||
|
||
def to_json | ||
{ | ||
error: 'Permission Denied', | ||
right_allowed: (@right_allowed ? @right_allowed.name : no_right_for_page), | ||
roles_for_right: (@roles_allowed ? @roles_allowed.map(&:title) : no_roles_for_page) | ||
} | ||
end | ||
|
||
private | ||
|
||
def no_right_for_page | ||
"No right is defined for this page: #{@controller_name}. Contact your system manager to notify this problem." | ||
end | ||
|
||
def no_roles_for_page | ||
'N/A (as no right is assigned for this action)' | ||
end | ||
end | ||
end |