Module: Checken::User

Defined in:
lib/checken/user.rb

Instance Method Summary collapse

Instance Method Details

#can?(*args) ⇒ Boolean



29
30
31
32
33
34
# File 'lib/checken/user.rb', line 29

def can?(*args)
  check_permission!(*args)
  true
rescue Checken::PermissionDeniedError => e
  false
end

#check_permission!(permission_path, object_or_options = {}, options_when_object_provided = {}) ⇒ Boolean

Can the user perform the given action?



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/checken/user.rb', line 9

def check_permission!(permission_path, object_or_options = {}, options_when_object_provided = {})
  if object_or_options.is_a?(Hash)
    object = nil
    options = object_or_options
  else
    object = object_or_options
    options = options_when_object_provided
  end

  schema = options.delete(:schema) || Checken.current_schema || Checken::Schema.instance

  if schema.nil?
    raise Error, "Could not determine a schema. Make sure you set Checken.current_schema or pass :schema to can? methods."
  end

  strict = options.delete(:strict) { true }
  user_proxy = schema.config.user_proxy_class.new(self)
  schema.check_permission!(permission_path, user_proxy, object, strict: strict)
end