Module: Rbacan::Authorization

Extended by:
ActiveSupport::Concern
Defined in:
lib/rbacan/authorization.rb

Instance Method Summary collapse

Instance Method Details

#authorize!(permission, context: nil) ⇒ Object

Calls the unauthorized handler if current_user lacks the given permission.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rbacan/authorization.rb', line 9

def authorize!(permission, context: nil)
  allowed = if Rbacan.legacy_mode
              current_user&.can?(permission.to_s)
            else
              Rbacan.authorized?(
                user: current_user,
                permission: permission,
                context: context || rbacan_authorization_context
              )
            end

  unless allowed
    _handle_unauthorized(permission: permission.to_s)
  end
end

#authorize_role!(role, context: nil) ⇒ Object

Calls the unauthorized handler if current_user lacks the given role.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rbacan/authorization.rb', line 26

def authorize_role!(role, context: nil)
  allowed = if Rbacan.legacy_mode
              current_user&.has_role?(role.to_s)
            else
              Rbacan.authorized?(
                user: current_user,
                role: role,
                context: context || rbacan_authorization_context
              )
            end

  unless allowed
    _handle_unauthorized(role: role.to_s)
  end
end