Module: SimonSays::Authorizer

Extended by:
ActiveSupport::Concern
Defined in:
lib/simon_says/authorizer.rb

Defined Under Namespace

Modules: ClassMethods Classes: Denied

Instance Method Summary collapse

Instance Method Details

#authorize(required = nil, options) ⇒ Object

Internal authorize instance method



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/simon_says/authorizer.rb', line 147

def authorize(required = nil, options)
  if through = options[:through]
    name = through.to_s.singularize.to_sym
  else
    name = options[:resource]
  end

  attr = Roleable.registry[name]

  required ||= options[attr.to_sym]
  required = [required] unless Array === required

  record = instance_variable_get("@#{name}")

  if record.nil? # must be devise scope
    record = send("current_#{name}")
    send "authenticate_#{name}!"
  end

  actual = record.send(attr)

  # actual roles must have at least
  # one required role (array intersection)
  ((required & actual).size > 0).tap do |res|
    raise Denied.new(attr, required, actual) unless res
  end
end

#find_resource(resource, options = {}) ⇒ Object

Internal find_resource instance method



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/simon_says/authorizer.rb', line 123

def find_resource(resource, options = {})
  resource = resource.to_s

  scope, query = resource_scope_and_query(resource, options)
  through = options[:through] ? options[:through].to_s : nil

  assoc = through || (options[:from] ? resource.pluralize : nil)
  scope = scope.send(assoc) if assoc && scope.respond_to?(assoc)

  record = scope.where(query).first!

  if through
    instance_variable_set "@#{through.singularize}", record
    record = record.send(resource)
  end

  instance_variable_set "@#{resource}", record
end