PHP Class Authority\Authority

Show file Open project: machuga/authority Class Usage Examples

Protected Properties

Property Type Description
$aliases List of aliases for groups of actions
$currentUser Current user in the application for rules to apply to
$dispatcher Dispatcher for events
$rules Collection of rules

Public Methods

Method Description
__construct ( mixed $currentUser, mixed $dispatcher = null ) Authority constructor
addAlias ( string $name, array $actions ) : authority\RuleAlias Define new alias for an action
addRule ( boolean $allow, array | string $actions, mixed $resource, Closure | null $condition = null ) : array | Rule Define rule for a given action and resource
allow ( array | string $actions, mixed $resource, Closure | null $condition = null ) : array | Rule Define privilege for a given action and resource
can ( $action, $resource, $resourceValue = null ) : boolean Determine if current user can access the given action and resource
cannot ( $action, $resource, $resourceValue = null ) : boolean Determine if current user cannot access the given action and resource Returns negation of can()
deny ( array | string $actions, mixed $resource, Closure | null $condition = null ) : array | Rule Define restriction for a given action and resource
dispatch ( string $eventName, mixed $payload = [] ) : mixed | null Fires event from current dispatcher
getAlias ( $name ) : authority\RuleAlias | null Returns a RuleAlias for a given action name
getAliases ( ) : array Returns all aliases
getAliasesForAction ( $action ) : array Returns all actions a given action applies to
getCurrentUser ( ) : mixed Returns current user
getRules ( ) : RuleRepository Returns the current rule set
getRulesFor ( $action, $resource ) : RuleRepository Returns all rules relevant to the given action and resource
setCurrentUser ( mixed $currentUser ) : void Set current user
setDispatcher ( mixed $dispatcher ) : void Set dispatcher
user ( ) : mixed Returns current user - alias of getCurrentUser()

Method Details

__construct() public method

Authority constructor
public __construct ( mixed $currentUser, mixed $dispatcher = null )
$currentUser mixed Current user in the application
$dispatcher mixed Dispatcher used for firing events

addAlias() public method

Define new alias for an action
public addAlias ( string $name, array $actions ) : authority\RuleAlias
$name string Name of action
$actions array Actions that $name aliases
return authority\RuleAlias

addRule() public method

Define rule for a given action and resource
public addRule ( boolean $allow, array | string $actions, mixed $resource, Closure | null $condition = null ) : array | Rule
$allow boolean True if privilege, false if restriction
$actions array | string Action for the rule
$resource mixed Resource for the rule
$condition Closure | null Optional condition for the rule
return array | Rule

allow() public method

Define privilege for a given action and resource
public allow ( array | string $actions, mixed $resource, Closure | null $condition = null ) : array | Rule
$actions array | string Action for the rule
$resource mixed Resource for the rule
$condition Closure | null Optional condition for the rule
return array | Rule

can() public method

Determine if current user can access the given action and resource
public can ( $action, $resource, $resourceValue = null ) : boolean
return boolean

cannot() public method

Determine if current user cannot access the given action and resource Returns negation of can()
public cannot ( $action, $resource, $resourceValue = null ) : boolean
return boolean

deny() public method

Define restriction for a given action and resource
public deny ( array | string $actions, mixed $resource, Closure | null $condition = null ) : array | Rule
$actions array | string Action for the rule
$resource mixed Resource for the rule
$condition Closure | null Optional condition for the rule
return array | Rule

dispatch() public method

Fires event from current dispatcher
public dispatch ( string $eventName, mixed $payload = [] ) : mixed | null
$eventName string
$payload mixed
return mixed | null

getAlias() public method

Returns a RuleAlias for a given action name
public getAlias ( $name ) : authority\RuleAlias | null
return authority\RuleAlias | null

getAliases() public method

Returns all aliases
public getAliases ( ) : array
return array

getAliasesForAction() public method

Returns all actions a given action applies to
public getAliasesForAction ( $action ) : array
return array

getCurrentUser() public method

Returns current user
public getCurrentUser ( ) : mixed
return mixed

getRules() public method

Returns the current rule set
public getRules ( ) : RuleRepository
return RuleRepository

getRulesFor() public method

Returns all rules relevant to the given action and resource
public getRulesFor ( $action, $resource ) : RuleRepository
return RuleRepository

setCurrentUser() public method

Set current user
public setCurrentUser ( mixed $currentUser ) : void
$currentUser mixed Current user in the application
return void

setDispatcher() public method

Set dispatcher
public setDispatcher ( mixed $dispatcher ) : void
$dispatcher mixed Dispatcher to fire events
return void

user() public method

Returns current user - alias of getCurrentUser()
public user ( ) : mixed
return mixed

Property Details

$aliases protected property

List of aliases for groups of actions
protected $aliases

$currentUser protected property

Current user in the application for rules to apply to
protected $currentUser

$dispatcher protected property

Dispatcher for events
protected $dispatcher

$rules protected property

Collection of rules
protected $rules