Module: Moat

Defined in:
lib/moat.rb,
lib/moat/rspec.rb,
lib/moat/version.rb

Defined Under Namespace

Modules: RSpec Classes: ActionNotFoundError, Error, NotAuthorizedError, NotFoundError, PolicyNotAppliedError, PolicyNotFoundError

Constant Summary collapse

POLICY_CLASS_SUFFIX =
"Policy".freeze
VERSION =
"0.5".freeze

Instance Method Summary collapse

Instance Method Details

#authorize(resource, action = "#{action_name}?", user: moat_user, policy: find_policy(resource)) ⇒ Object


46
47
48
49
50
51
52
# File 'lib/moat.rb', line 46

def authorize(resource, action = "#{action_name}?", user: moat_user, policy: find_policy(resource))
  if authorized?(resource, action, user: user, policy: policy)
    resource
  else
    fail NotAuthorizedError, action: action, resource: resource, policy: policy, user: user
  end
end

#authorized?(resource, action = "#{action_name}?", user: moat_user, policy: find_policy(resource)) ⇒ Boolean

Returns:

  • (Boolean)

42
43
44
# File 'lib/moat.rb', line 42

def authorized?(resource, action = "#{action_name}?", user: moat_user, policy: find_policy(resource))
  !!apply_policy(resource, action, user: user, policy: policy::Authorization)
end

#moat_userObject


54
55
56
# File 'lib/moat.rb', line 54

def moat_user
  current_user
end

#policy_filter(scope, action = action_name, user: moat_user, policy: find_policy(scope)) ⇒ Object


38
39
40
# File 'lib/moat.rb', line 38

def policy_filter(scope, action = action_name, user: moat_user, policy: find_policy(scope))
  apply_policy(scope, action, user: user, policy: policy::Filter)
end

#skip_verify_policy_appliedObject Also known as: policy_applied


62
63
64
# File 'lib/moat.rb', line 62

def skip_verify_policy_applied
  @_moat_policy_applied = true
end

#verify_policy_appliedObject


58
59
60
# File 'lib/moat.rb', line 58

def verify_policy_applied
  fail PolicyNotAppliedError unless @_moat_policy_applied
end