You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.1 KiB
49 lines
1.1 KiB
<?php
|
|
namespace App\Traits;
|
|
|
|
trait Authorizable
|
|
{
|
|
private $abilities = [
|
|
'index' => 'view',
|
|
'edit' => 'edit',
|
|
'show' => 'view',
|
|
'update' => 'edit',
|
|
'create' => 'add',
|
|
'store' => 'add',
|
|
'destroy' => 'delete'
|
|
];
|
|
|
|
/**
|
|
* Override of callAction to perform the authorization before
|
|
*
|
|
* @param $method
|
|
* @param $parameters
|
|
* @return mixed
|
|
*/
|
|
public function callAction($method, $parameters)
|
|
{
|
|
if( $ability = $this->getAbility($method) ) {
|
|
$this->authorize($ability);
|
|
}
|
|
|
|
return parent::callAction($method, $parameters);
|
|
}
|
|
|
|
public function getAbility($method)
|
|
{
|
|
$routeName = explode('.', \Request::route()->getName());
|
|
$action = array_get($this->getAbilities(), $method);
|
|
|
|
return $action ? $action . '_' . $routeName[0] : null;
|
|
}
|
|
|
|
private function getAbilities()
|
|
{
|
|
return $this->abilities;
|
|
}
|
|
|
|
public function setAbilities($abilities)
|
|
{
|
|
$this->abilities = $abilities;
|
|
}
|
|
}
|
|
|