Class: Ixtlan::UserManagement::Permission

Inherits:
Object
  • Object
show all
Includes:
Virtus
Defined in:
lib/ixtlan/user_management/permission_model.rb

Constant Summary collapse

METHODS =
[ :allow_retrieve, 
:allow_create, 
:allow_update, 
:allow_delete ]

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ixtlan/user_management/permission_model.rb', line 96

def method_missing( method, *args )
  if METHODS.include?( method )
    a = Action.new( :name => method.to_s.sub( /allow_/, '' ) )
    set_asso( a, *args )
    self.actions << a
    self
  elsif METHODS.include?( method.to_s.sub( /_verb$/, '' ).to_sym )
    verb = args.first
    args = args[ 1..-1 ]
    a = Action.new( :name => method.to_s.gsub( /allow_|_verb/, '' ),
                    :verb => verb )
    set_asso( a, *args )
    self.actions << a
    self
  else
    super
  end
end

Instance Method Details

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

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
# File 'lib/ixtlan/user_management/permission_model.rb', line 55

def allow?( method, association = nil )
  action = self.actions.detect { |a| a.name == method }
  if self.allow 
    action != nil && ( action.associations.nil? || action.associations.include?( association ) )
  else
    action.nil? #TODO associations
  end
end

#allow_all(*associations) ⇒ Object



64
65
66
67
68
69
# File 'lib/ixtlan/user_management/permission_model.rb', line 64

def allow_all( *associations )
  self.allow = false
  self.actions.clear
  set_asso( self, *associations )
  self
end

#allow_mutate(*associations) ⇒ Object



89
90
91
92
93
94
# File 'lib/ixtlan/user_management/permission_model.rb', line 89

def allow_mutate( *associations )
  allow_retrieve( *associations )
  allow_create( *associations )
  allow_update( *associations )
  self
end

#deny_allObject



83
84
85
86
87
# File 'lib/ixtlan/user_management/permission_model.rb', line 83

def deny_all
  self.allow = true
  self.actions.clear
  self
end

#parent=(pt) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/ixtlan/user_management/permission_model.rb', line 46

def parent=( pt )
  super
  if pt
    pt.children << self
  elsif parent
    parent.children.delete self
  end
end

#set_asso(obj, *associations) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ixtlan/user_management/permission_model.rb', line 71

def set_asso( obj, *associations )
  case associations.first
  when NilClass
    # leave default
  when Array
    # assume empty
    obj.associations = associations.flatten
  else
    obj.associations = associations
  end
end