Skip to content

Instantly share code, notes, and snippets.

@b0c1
Created June 21, 2011 10:41
Show Gist options
  • Save b0c1/1037593 to your computer and use it in GitHub Desktop.
Save b0c1/1037593 to your computer and use it in GitHub Desktop.
Padrino multi role monkey patch
#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
Copy link

Thanks for proposing this. Would you consider providing an example of how one would use this? For both the Admin and non-admin parts of the app?

Thank you!

@b0c1
Copy link
Author

b0c1 commented Nov 10, 2011

I using " register Padrino::Admin::AccessControl" both admin and non admin app.
After that access_control.roles_for will work...

@b0c1
Copy link
Author

b0c1 commented Nov 10, 2011

(In my app the original code included to the boot.rb. Before the Padrino.load! command

@andreimoment
Copy link

My question was about how would one use multi-role admin in the application controllers. Sorry for not being clear.

@andreimoment
Copy link

andreimoment commented Nov 29, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment