# To skip this in a subclassed controller:
# skip_before_filter :check_authorized
#
# Aaa! To many indentions!
#
def check_authorization
return true if current_user_super_user?
unless
current_user.roles.detect do |role|
role.rights.detect do |right|
if right.controller == controller_name
right.actions.split(' ').detect do |action|
(action == "all") or (action == action_name)
end
end
end
end
flash[:notice] = "You are not authorized to view the page you requested"
request.env["HTTP_REFERER"] ? (redirect_to :back) : (redirect_to home_path)
return false
end
end
And here is the refactoring. Comical function names!
# To skip this in a subclassed controller:
# skip_before_filter :check_authorized
#
def check_authorization
return true if current_user_super_user?
unless current_user_has_rights?
flash[:notice] = "You are not authorized to view the page you requested"
request.env["HTTP_REFERER"] ? (redirect_to :back) : (redirect_to home_path)
return false
end
end
def current_user_has_rights?
current_user.roles.detect do |role|
current_user_has_rights_as role
end
end
def current_user_has_rights_as(role)
role.rights.detect do |right|
next if right.controller != controller_name
current_user_has_rights_to_action(rights.actions)
end
end
def current_user_has_rights_to_action(actions)
actions.split(' ').detect do |action|
(action == "all") or (action == action_name)
end
end
Of course, I was refactoring it because it was broken and I couldn't make heads or tails of who was winning in what seemed like an argument of code. So I broke it up and made everyone do their little thing in words I might remember when I come back to this code. And now we have peace.