Module: Kankri::PrivilegeSubject

Defined in:
lib/kankri/privilege_subject.rb

Overview

Mixin that allows classes to require privileges from a PrivilegeSet

This expects the including class to define a method, 'privilege_key', which identifies the object in the privilege set.

Instance Method Summary collapse

Instance Method Details

#can?(operation, privilege_set) ⇒ Boolean

Checks whether a privilege is granted for this object

This looks in the privilege set under the key returned by #privilege_key.

Examples:

Checks for a privilege that is in the set for this object.

subject.can?(:get, privilege_set)
#=> true

Checks for a privilege that is not in the set for this object.

subject.can?(:get, privilege_set)
#=> false


28
29
30
# File 'lib/kankri/privilege_subject.rb', line 28

def can?(operation, privilege_set)
  privilege_set.has?(operation, privilege_key)
end

#fail_if_cannot(operation, privilege_set) ⇒ void

This method returns an undefined value.

Raises an exception if a privilege is not granted for this object

This looks in the privilege set under the key returned by #privilege_key.

Examples:

Requires a privilege to continue.

subject.require(:get, privilege_set)


43
44
45
# File 'lib/kankri/privilege_subject.rb', line 43

def fail_if_cannot(operation, privilege_set)
  privilege_set.require(operation, privilege_key)
end