Class: Checkin::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/checkin/rule.rb

Overview

Author

Maurizio Casimirri ([email protected])

Copyright

Copyright © 2012 Maurizio Casimirri

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, *args) ⇒ Rule

Returns a new instance of Rule.



26
27
28
29
30
31
32
33
# File 'lib/checkin/rule.rb', line 26

def initialize(kind, *args)
  opts = args.extract_options!
  @kind = kind
  @actions_or_attributes = (opts[:actions_or_attributes] || []).map {|a| "#{a}".to_sym }
  @roles = (opts[:roles] || []).map {|r| "#{r}".to_sym }
  @resources = (opts[:resources] || []).map {|r| "#{r}".singularize.to_sym }
  @scope = opts[:scope].present? ? opts[:scope].to_sym : nil
end

Instance Attribute Details

#actions_or_attributesObject

Returns the value of attribute actions_or_attributes.



24
25
26
# File 'lib/checkin/rule.rb', line 24

def actions_or_attributes
  @actions_or_attributes
end

#kindObject

Returns the value of attribute kind.



24
25
26
# File 'lib/checkin/rule.rb', line 24

def kind
  @kind
end

#resourcesObject

Returns the value of attribute resources.



24
25
26
# File 'lib/checkin/rule.rb', line 24

def resources
  @resources
end

#rolesObject

Returns the value of attribute roles.



24
25
26
# File 'lib/checkin/rule.rb', line 24

def roles
  @roles
end

#scopeObject

Returns the value of attribute scope.



24
25
26
# File 'lib/checkin/rule.rb', line 24

def scope
  @scope
end

Instance Method Details

#check(subject, action_or_attribute, object_or_resource) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/checkin/rule.rb', line 35

def check(subject, action_or_attribute, object_or_resource)
  scope   = subject.scope
  action  = "#{action}".to_sym
  
  object_or_resource = object_or_resource[:for] if object_or_resource.is_a?(Hash)
  object   = ( object_or_resource.is_a?(Symbol) || object_or_resource.is_a?(String)) ? nil : object_or_resource 
  resource = object ? object.class.name.demodulize.underscore.to_sym  : "#{object_or_resource}".singularize.to_sym

  if affect_scope?(scope) && affect_role?(subject, object) && affect_action_or_attribute?(action_or_attribute) && affect_resource?(resource)
     deny? ? :denied : :allowed
  else
     :skip
  end
end

#to_sObject



50
51
52
53
54
55
56
57
# File 'lib/checkin/rule.rb', line 50

def to_s
  formatted_roles = roles.map {|r| ":#{r}" }.join(", ") if roles.any?
  formatted_actions = ":to => [" + actions_or_attributes.map {|a| ":#{a}" }.join(", ") + "]" if actions_or_attributes.any?
  formatted_scope = ":scope => :#{scope}" if scope
  formatted_resources = ":resources => [" + actions_or_attributes.map {|r| ":#{r}" }.join(", ") + "]" if resources.any?
  args = [formatted_roles, formatted_actions, formatted_resources, formatted_scope].compact.join(", ")
  "#{kind}(#{args})"
end