Module: Miau

Extended by:
ActiveSupport::Concern
Defined in:
lib/miau.rb,
lib/miau/run.rb,
lib/miau/error.rb,
lib/miau/storage.rb,
lib/miau/version.rb

Defined Under Namespace

Classes: AuthorizationNotPerformedError, Error, NotAuthorizedError, NotDefinedError, OverwriteError, PolicyRun, PolicyStorage

Constant Summary collapse

VERSION =

2024-01-06

"1.1.1"

Instance Method Summary collapse

Instance Method Details

#authorize!(resource = nil, hsh = {}) ⇒ Object

Raises:



20
21
22
23
24
25
26
27
# File 'lib/miau.rb', line 20

def authorize!(resource = nil, hsh = {})
  @_miau_authorization_performed = true
  return true if authorized?(resource, hsh)

  klass, action = klass_action
  msg = "class <#{klass} action <#{action}>"
  raise NotAuthorizedError, msg
end

#authorize_controller!Object

Raises:



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

def authorize_controller!
  name = params[:controller].to_sym
  policy = PolicyStorage.instance.find_or_create_policy(name)
  raise NotDefinedError unless policy&.respond_to?(:controller)

  policy.send(:controller)
end

#authorized?(resource = nil, hsh = {}) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/miau.rb', line 29

def authorized?(resource = nil, hsh = {})
  klass, action = klass_action
  PolicyRun.instance.run(klass, action, miau_user, resource)
end

#miau_authorization_performed?Boolean

Returns:

  • (Boolean)


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

def miau_authorization_performed?
  !!@_miau_authorization_performed
end

#miau_userObject



34
35
36
# File 'lib/miau.rb', line 34

def miau_user
  current_user
end

#verify_authorizedObject



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

def verify_authorized
  raise AuthorizationNotPerformedError unless miau_authorization_performed?
end