Class: CanTango::Permits::Permit

Inherits:
Object
  • Object
show all
Includes:
Api::Attributes, CanTango::PermitEngine::Compatibility, CanTango::PermitEngine::RoleMatcher, CanTango::PermitEngine::Util, Rules
Defined in:
lib/cantango/permits/permit.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CanTango::PermitEngine::RoleMatcher

#role_group_match?, #role_match?

Methods included from CanTango::PermitEngine::Compatibility

#rules_1_4, #rules_1_5

Methods included from CanTango::PermitEngine::Util

#localhost_manager?, #permit_name, #role

Methods included from Rules

#can, #cannot

Methods included from Rules::RuleClass

#rule_class

Methods included from Rules::Dsl

included, #scope

Constructor Details

#initialize(ability) ⇒ Permit

creates the permit



16
17
18
# File 'lib/cantango/permits/permit.rb', line 16

def initialize ability
  @ability  = ability
end

Instance Attribute Details

#abilityObject (readonly)

Returns the value of attribute ability.



8
9
10
# File 'lib/cantango/permits/permit.rb', line 8

def ability
  @ability
end

Class Method Details

.account_name(clazz) ⇒ Object



25
26
27
28
# File 'lib/cantango/permits/permit.rb', line 25

def self. clazz
  return nil if clazz.name == clazz.name.demodulize
  clazz.name.gsub(/::.*/,'').gsub(/(.*)Permits/, '\1').underscore.to_sym
end

.first_name(clazz) ⇒ Object



20
21
22
# File 'lib/cantango/permits/permit.rb', line 20

def self.first_name clazz
  clazz.to_s.gsub(/^([A-Za-z]+).*/, '\1').underscore.to_sym # first part of class name
end

Instance Method Details

#any(reg_exp) ⇒ Object



39
40
41
# File 'lib/cantango/permits/permit.rb', line 39

def any reg_exp
  config.models.by_reg_exp reg_exp
end

#category(label) ⇒ Object



35
36
37
# File 'lib/cantango/permits/permit.rb', line 35

def category label
  config.models.by_category label
end

#executeObject

executes the permit



31
32
33
# File 'lib/cantango/permits/permit.rb', line 31

def execute
  executor.execute!
end

#licenses(*names) ⇒ Object



100
101
102
103
104
# File 'lib/cantango/permits/permit.rb', line 100

def licenses *names
  names.to_strings.each do |name|
    try_license name
  end
end

#localhost?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/cantango/permits/permit.rb', line 55

def localhost?
  config.localhost_list.include? request.host
end

#optionsObject



43
44
45
# File 'lib/cantango/permits/permit.rb', line 43

def options
  ability.options
end

#permit?Boolean

In a specific Role based Permit you can use

def permit? user, options = {}
  return if !super(user, :in_role)
  ... permission logic follows

This will call the Permit::Base#permit? instance method (the method below) It will only return true if the user matches the role of the Permit class and the options passed in is set to :in_role

If these confitions are not met, it will return false and thus the outer permit will not run the permission logic to follow

Normally super for #permit? should not be called except for this case, or if subclassing another Permit than Permit::Base

Returns:

  • (Boolean)


94
95
96
97
98
# File 'lib/cantango/permits/permit.rb', line 94

def permit?
  static_rules
  permit_rules
  dynamic_rules
end

#publichost?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/cantango/permits/permit.rb', line 59

def publichost?
  !localhost?
end

#rulesObject



75
76
77
# File 'lib/cantango/permits/permit.rb', line 75

def rules
  ability.send :rules
end

#subjectObject



63
64
65
# File 'lib/cantango/permits/permit.rb', line 63

def subject
  ability.subject
end

#userObject



67
68
69
# File 'lib/cantango/permits/permit.rb', line 67

def user
  ability.user
end

#user_accountObject



71
72
73
# File 'lib/cantango/permits/permit.rb', line 71

def 
  ability.
end