Class: Grant::Grantor

Inherits:
Object
  • Object
show all
Includes:
Status
Defined in:
lib/grant/grantor.rb

Instance Method Summary collapse

Methods included from Status

disable_grant, do_as, enable_grant, grant_disabled?, grant_enabled?, switch_to_monothread, switch_to_multithread, with_grant, without_grant

Methods included from Status::MonoThread

included

Constructor Details

#initialize(action, &callback) ⇒ Grantor

Returns a new instance of Grantor.



8
9
10
11
# File 'lib/grant/grantor.rb', line 8

def initialize(action, &callback)
  @action = action
  @callback = callback
end

Instance Method Details

#authorize!(model, user = Grant::User.current_user) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/grant/grantor.rb', line 13

def authorize!(model, user=Grant::User.current_user)
  unless grant_disabled?
    without_grant do
      raise Grant::Error.new(user, @action, model) unless @callback.call(user, model, @action)
    end
  end
end

#authorized?(model, user = Grant::User.current_user) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/grant/grantor.rb', line 21

def authorized?(model, user=Grant::User.current_user)
  without_grant do
    @callback.call(user, model, @action)
  end
end