Class: Authz::Role

Inherits:
Authz.selfself::ApplicationRecord
  • Object
show all
Defined in:
app/models/authz/role.rb

Overview

Represents a role within the application

Instance Method Summary collapse

Instance Method Details

#cached_granted_keyword_for(scopable) ⇒ String

Cached version of #granted_keyword_for

Parameters:

  • scopable (Scopable)

    the scoping module for which the keyword is going to be found

Returns:

  • (String)

    the applicable keywords according to the role’s scoping rule for the given scopable

Raises:

  • if the role does not have a scoping rule for the given scopable

See Also:



70
71
72
73
74
# File 'app/models/authz/role.rb', line 70

def cached_granted_keyword_for(scopable)
  Authz.cache.fetch([cache_key, scopable.to_s]) do
    granted_keyword_for(scopable)
  end
end

#cached_has_permission?(controller_name, action_name) ⇒ Boolean

Cached version of has_permission?

Parameters:

  • controller_name (String)
  • action_name (String)

Returns:

  • (Boolean)

See Also:



49
50
51
52
53
# File 'app/models/authz/role.rb', line 49

def cached_has_permission?(controller_name, action_name)
  Authz.cache.fetch([cache_key, controller_name, action_name]) do
    has_permission?(controller_name, action_name)
  end
end

#granted_keyword_for(scopable) ⇒ String

Returns the applicable keywords according to the role’s scoping rule for the given scopable.

Parameters:

  • scopable (Scopable)

    the scoping module for which the keyword is going to be found

Returns:

  • (String)

    the applicable keywords according to the role’s scoping rule for the given scopable

Raises:

  • if the role does not have a scoping rule for the given scopable



61
62
63
# File 'app/models/authz/role.rb', line 61

def granted_keyword_for(scopable)
  scoping_rules.find_by!(scopable: scopable.to_s).keyword
end

#has_permission?(controller_name, action_name) ⇒ Boolean

Returns true if the role has access to the given controller action.

Parameters:

  • controller_name (String)
  • action_name (String)

Returns:

  • (Boolean)

    true if the role has access to the given controller action



42
43
44
# File 'app/models/authz/role.rb', line 42

def has_permission?(controller_name, action_name)
  controller_actions.exists?(controller: controller_name, action: action_name)
end