Class: Authorule::Permission

Inherits:
Object
  • Object
show all
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 = Permission.resolve(Campaign, :destroy)
@user.has_permission?(permission) # 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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, action = nil) ⇒ Permission

Initializes a new permission.

Parameters:

  • object

    The object of the permission.

  • action (Symbol|nil) (defaults to: nil)

    The action the user wishes to perform.



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

.kindObject (readonly)

Returns the value of attribute kind.



101
102
103
# File 'lib/authorule/permission.rb', line 101

def kind
  @kind
end

.list_blockObject (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_blockObject (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

#actionSymbol|nil (readonly)

Returns The action the user wishes to perform.

Returns:

  • (Symbol|nil)

    The action the user wishes to perform.



63
64
65
# File 'lib/authorule/permission.rb', line 63

def action
  @action
end

#available_actionsArray (readonly)

Returns The available actions for the permission.

Returns:

  • (Array)

    The available actions for the permission.



79
80
81
# File 'lib/authorule/permission.rb', line 79

def available_actions
  []
end

#kindSymbol (readonly)

Returns The kind of permission. This is delegated to the current class.

Returns:

  • (Symbol)

    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

#nameString (readonly)

Returns The name of the permission. This is delegated to #object.

Returns:

  • (String)

    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

#objectSymbol (readonly)

Returns The object of the permission.

Returns:

  • (Symbol)

    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

#dependenciesObject

Resolves dependencies for this permission. To be implemented by subclasses.



92
93
94
# File 'lib/authorule/permission.rb', line 92

def dependencies
  []
end

#with_dependenciesObject

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