Class: Ixtlan::UserManagement::Guard
- Inherits:
-
Object
- Object
- Ixtlan::UserManagement::Guard
- Defined in:
- lib/ixtlan/user_management/guard.rb
Constant Summary collapse
- METHODS =
{ :post => 'create', :get => 'retrieve', :put => 'update', :delete => 'delete' }
Instance Method Summary collapse
- #all_permissions ⇒ Object
- #allow?(resource, method, association = nil) ⇒ Boolean
- #associations(resource, method = nil) ⇒ Object
- #check_parent(resource, parent_resource) ⇒ Object
- #permission_for(resource, *associations, &block) ⇒ Object
- #permissions ⇒ Object
- #permissions_new(resource) ⇒ Object
Instance Method Details
#all_permissions ⇒ Object
63 64 65 |
# File 'lib/ixtlan/user_management/guard.rb', line 63 def .values.flatten.select { |pp| pp.parent.nil? } end |
#allow?(resource, method, association = nil) ⇒ Boolean
32 33 34 35 36 37 38 39 |
# File 'lib/ixtlan/user_management/guard.rb', line 32 def allow?( resource, method, association = nil ) perms = [ resource ] || [] perms.one? do |perm| ( perm.associations.nil? || perm.associations.include?( association.to_s ) ) && perm.allow?( METHODS[ method ], association ) end end |
#associations(resource, method = nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ixtlan/user_management/guard.rb', line 41 def associations( resource, method = nil ) perms = [ resource ] || [] asso = perms.select { |perm| perm.associations } if asso.empty? nil else asso.collect{ |perm| perm.associations }.flatten end # TODO method end |
#check_parent(resource, parent_resource) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ixtlan/user_management/guard.rb', line 52 def check_parent( resource, parent_resource ) perms = [ resource ] if perms perms.each do |perm| if perm.parent && perm.parent.resource != parent_resource raise 'parent resource is not guarded' end end end end |
#permission_for(resource, *associations, &block) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ixtlan/user_management/guard.rb', line 77 def ( resource, *associations, &block ) current = ( resource ) associations.flatten! if associations.first.is_a? Permission current.parent = associations.first associations = associations[ 1..-1 ] end current.associations = associations unless associations.empty? block.call current if block current end |
#permissions ⇒ Object
67 68 69 |
# File 'lib/ixtlan/user_management/guard.rb', line 67 def @permissions ||= {} end |
#permissions_new(resource) ⇒ Object
71 72 73 74 75 |
# File 'lib/ixtlan/user_management/guard.rb', line 71 def ( resource ) pp = Permission.new( :resource => resource ) ( [ resource ] ||= [] ) << pp pp end |