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

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