Class: Checkin::Subject

Inherits:
Object
  • Object
show all
Includes:
Dsl::Permissions, Dsl::Roles
Defined in:
lib/checkin/subject.rb

Instance Method Summary collapse

Methods included from Dsl::Roles

#role?, #roles

Constructor Details

#initialize(subject_model, scope = {}) ⇒ Subject

Returns a new instance of Subject.



30
31
32
33
# File 'lib/checkin/subject.rb', line 30

def initialize(subject_model, scope = {})
  @subject_model = subject_model
  @scope = scope[:scope]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mid, *args) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/checkin/subject.rb', line 116

def method_missing(mid, *args)
  missing_method = mid.to_s
  prefixed_with_can = (missing_method =~ /^can_/) && (missing_method =~ /\?$/)

  if prefixed_with_can
    action = missing_method.gsub(/^can_/, "").gsub(/\?$/, "")
    self.can?(action, *args)

  elsif self.respond_to?(:"is_role_method?") && self.is_role_method?(missing_method)
    role = self.find_role_by_method(missing_method)
    self.check_role(role, *args)

  elsif @subject_model && @subject_model.respond_to?(missing_method)
    @subject_model.send(missing_method, *args)

  else
    raise NoMethodError.new("undefined method `#{missing_method}' for #{self.class.name}")
  end
end

Instance Method Details

#allowed_to_set?(attribute, on = {}) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/checkin/subject.rb', line 67

def allowed_to_set?(attribute, on = {})

  object = on[:on]
  if @explain
     Rails.logger.info " + allowed_to_set?(:#{attribute}, on => #{object})"
  end
        
  self.class.attribute_rules.each do|rule|
    result = rule.check(self, :"#{attribute}", object)

    if @explain
       Rails.logger.info  ["    - #{rule}".ljust(65), ":#{result}"].join(" => ")
    end

    case result
      when :denied
        return false
      when :allowed
        return true
      else
    end
  end

  true
end

#can?(action, object_or_resource) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/checkin/subject.rb', line 43

def can?(action, object_or_resource)

  if @explain
     Rails.logger.info " + can?(:#{action}, #{object_or_resource})"
  end
        
  self.class.rules.each do|rule|
    result = rule.check(self, :"#{action}", object_or_resource)

    if @explain
      Rails.logger.info ["    - #{rule}".ljust(65), ":#{result}"].join(" => ")
    end

    case result
      when :denied
        return false
      when :allowed
        return true
      else
    end
  end
  true
end

#checkin!(action, object_or_resource) ⇒ Object



101
102
103
# File 'lib/checkin/subject.rb', line 101

def checkin!(action, object_or_resource)
  raise Checkin::AccessDenied.new(self, action, object_or_resource) unless self.can?(action, object_or_resource)
end

#delete_denied_params(action, object_or_resource, resource_params) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/checkin/subject.rb', line 105

def delete_denied_params(action, object_or_resource, resource_params)
  to_be_deleted = []
  resource_params.keys.each {|key|
    to_be_deleted.push(key) unless self.allowed_to_set?(key, :on => object_or_resource)
  }
  to_be_deleted.each do |key_to_delete|
    resource_params.delete(key_to_delete)
  end
  resource_params
end

#explain!Object



93
94
95
# File 'lib/checkin/subject.rb', line 93

def explain!
  @explain = true
end

#scopeObject



39
40
41
# File 'lib/checkin/subject.rb', line 39

def scope
  :"#{@scope}"
end

#stop_explaining!Object



97
98
99
# File 'lib/checkin/subject.rb', line 97

def stop_explaining!
  @explain = false
end

#subject_modelObject



35
36
37
# File 'lib/checkin/subject.rb', line 35

def subject_model
  @subject_model
end