Created
June 21, 2011 10:41
-
-
Save b0c1/1037593 to your computer and use it in GitHub Desktop.
Padrino multi role monkey patch
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
#put into the padrino boot.rb file before Padrino.load! | |
module Padrino | |
module Admin | |
module AccessControl | |
class Base | |
## | |
# Return an array of project_modules | |
# | |
def project_modules(account) | |
roles = account.roles if account.roles.kind_of?(Array) rescue [] | |
roles = [account.role.to_sym] if roles.empty? rescue [] | |
roles = :any if roles.empty? rescue :any | |
authorizations = @authorizations.find_all { |auth| (auth.roles & roles).empty? } | |
authorizations.collect(&:project_modules).flatten.uniq | |
end | |
## | |
# Return true if the given account is allowed to see the given path. | |
# | |
def allowed?(account=nil, path=nil) | |
path = "/" if path.blank? | |
roles = account.roles if account.roles.kind_of?(Array) rescue [] | |
roles = [account.role.to_sym] if roles.empty? rescue [] | |
roles = :any if roles.empty? rescue :any | |
authorizations = @authorizations.find_all { |auth| auth.roles.include?(:any) } | |
allowed_paths = authorizations.collect(&:allowed).flatten.uniq | |
denied_paths = authorizations.collect(&:denied).flatten.uniq | |
if account | |
denied_paths.clear | |
authorizations = @authorizations.find_all { |auth| (auth.roles & roles).empty? } | |
allowed_paths += authorizations.collect(&:allowed).flatten.uniq | |
authorizations = @authorizations.find_all { |auth| !(auth.roles & roles).empty? && !auth.roles.include?(:any) } | |
denied_paths += authorizations.collect(&:allowed).flatten.uniq | |
denied_paths += authorizations.collect(&:denied).flatten.uniq | |
end | |
return true if allowed_paths.any? { |p| path =~ /^#{p}/ } | |
return false if denied_paths.any? { |p| path =~ /^#{p}/ } | |
true | |
end | |
end | |
end | |
end | |
end |
andreimoment
commented
Nov 29, 2011
via email
Thank you! Making the obvious obvious for newbies :)
…On Wed, Nov 9, 2011 at 11:55 PM, b0c1 ***@***.*** wrote:
I using " register Padrino::Admin::AccessControl" both admin and non admin app.
After that access_control.roles_for will work...
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1037593
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment