Class: Authorule::Permission
- Inherits:
-
Object
- Object
- Authorule::Permission
- Defined in:
- lib/authorule/permission.rb
Overview
A permission. This is an object that can be used to check if someone has access to a certain permissable.
Note: do not confuse a permission with a PermissionRule or PermissionRuleBase. This class doesn’t indicate that a user has been granted a permission. It simply encapsulates a permission query.
This class should also not be confused with a CustomPermission, which is an application-defined custom permission.
Usage
= Permission.resolve(Campaign, :destroy)
@user.() # Granted that @user < UI::PermissionHolder
Or even simpler:
@user.may?(:destroy, @campaign)
Object resolution
Any object can be converted into a permission, if a suitable Schema can be found. For example, any UI resource can be resolved into a resource permission, but also any resource model class or even resource symbol. This allows for the following equivalent calls:
@user.may?(:destroy, UI.application.resources[:campaign])
@user.may?(:destroy, @campaign)
@user.may?(:destroy, Campaign)
@user.may?(:destroy, :campaign)
The UI library defines a few schemas, for example one for resource permissions, and one for UI space permissions. There is also a custom permission schema - allowing the application designer to define additional permissions. These can be referred to throughout the UI library.
Class Attribute Summary collapse
-
.kind ⇒ Object
readonly
Returns the value of attribute kind.
-
.list_block ⇒ Object
readonly
Returns the value of attribute list_block.
-
.resolve_block ⇒ Object
readonly
Returns the value of attribute resolve_block.
Instance Attribute Summary collapse
-
#action ⇒ Symbol|nil
readonly
The action the user wishes to perform.
-
#available_actions ⇒ Array
readonly
The available actions for the permission.
-
#kind ⇒ Symbol
readonly
The kind of permission.
-
#name ⇒ String
readonly
The name of the permission.
-
#object ⇒ Symbol
readonly
The object of the permission.
Class Method Summary collapse
-
.list(&block) ⇒ Object
Defines a block that lists all suitable permission targets in the application.
-
.register(kind) ⇒ Object
Registers a permission class under a specific kind.
-
.resolve(&block) ⇒ Object
Defines a block that resolves any argument into a suitable permission target.
Instance Method Summary collapse
-
#dependencies ⇒ Object
Resolves dependencies for this permission.
-
#initialize(object, action = nil) ⇒ Permission
constructor
Initializes a new permission.
-
#with_dependencies ⇒ Object
Retrieves an array of permissions consisting of dependencies and the permission itself.
Constructor Details
#initialize(object, action = nil) ⇒ Permission
Initializes a new permission.
49 50 51 52 |
# File 'lib/authorule/permission.rb', line 49 def initialize(object, action = nil) @object = object @action = action.try(:to_sym) end |
Class Attribute Details
.kind ⇒ Object (readonly)
Returns the value of attribute kind.
101 102 103 |
# File 'lib/authorule/permission.rb', line 101 def kind @kind end |
.list_block ⇒ Object (readonly)
Returns the value of attribute list_block.
101 102 103 |
# File 'lib/authorule/permission.rb', line 101 def list_block @list_block end |
.resolve_block ⇒ Object (readonly)
Returns the value of attribute resolve_block.
101 102 103 |
# File 'lib/authorule/permission.rb', line 101 def resolve_block @resolve_block end |
Instance Attribute Details
#action ⇒ Symbol|nil (readonly)
Returns The action the user wishes to perform.
63 64 65 |
# File 'lib/authorule/permission.rb', line 63 def action @action end |
#available_actions ⇒ Array (readonly)
Returns The available actions for the permission.
79 80 81 |
# File 'lib/authorule/permission.rb', line 79 def available_actions [] end |
#kind ⇒ Symbol (readonly)
Returns The kind of permission. This is delegated to the current class.
67 68 69 |
# File 'lib/authorule/permission.rb', line 67 def kind self.class.kind end |
#name ⇒ String (readonly)
Returns The name of the permission. This is delegated to #object.
73 74 75 |
# File 'lib/authorule/permission.rb', line 73 def name object.name end |
#object ⇒ Symbol (readonly)
Returns The object of the permission.
59 60 61 |
# File 'lib/authorule/permission.rb', line 59 def object @object end |
Class Method Details
.list(&block) ⇒ Object
Defines a block that lists all suitable permission targets in the application.
115 116 117 |
# File 'lib/authorule/permission.rb', line 115 def list(&block) @list_block = block end |
.register(kind) ⇒ Object
Registers a permission class under a specific kind.
104 105 106 107 |
# File 'lib/authorule/permission.rb', line 104 def register(kind) Authorule.register kind, self @kind = kind end |
.resolve(&block) ⇒ Object
Defines a block that resolves any argument into a suitable permission target.
110 111 112 |
# File 'lib/authorule/permission.rb', line 110 def resolve(&block) @resolve_block = block end |
Instance Method Details
#dependencies ⇒ Object
Resolves dependencies for this permission. To be implemented by subclasses.
92 93 94 |
# File 'lib/authorule/permission.rb', line 92 def dependencies [] end |
#with_dependencies ⇒ Object
Retrieves an array of permissions consisting of dependencies and the permission itself.
87 88 89 |
# File 'lib/authorule/permission.rb', line 87 def with_dependencies dependencies + [ self ] end |