Class: Ixtlan::UserManagement::Guard

Inherits:
Object
  • Object
show all
Defined in:
lib/ixtlan/user_management/guard.rb

Constant Summary collapse

METHODS =
{
  :post => 'create',
  :get => 'retrieve',
  :put => 'update',
  :delete => 'delete'
}

Instance Method Summary collapse

Instance Method Details

#all_permissionsObject



63
64
65
# File 'lib/ixtlan/user_management/guard.rb', line 63

def all_permissions
  permissions.values.flatten.select { |pp| pp.parent.nil? }
end

#allow?(resource, method, association = nil) ⇒ Boolean

Returns:

  • (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 = permissions[ 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 = permissions[ 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 = permissions[ 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 permission_for( resource, *associations, &block ) 
  current = permissions_new( 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

#permissionsObject



67
68
69
# File 'lib/ixtlan/user_management/guard.rb', line 67

def permissions
  @permissions ||= {}
end

#permissions_new(resource) ⇒ Object



71
72
73
74
75
# File 'lib/ixtlan/user_management/guard.rb', line 71

def permissions_new( resource )
  pp = Permission.new( :resource => resource )
  ( permissions[ resource ] ||= [] ) << pp
  pp
end