-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathAuthorizable.php
59 lines (50 loc) · 1.32 KB
/
Authorizable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace App;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
trait Authorizable
{
/**
* List of default method names of the Controllers and the related permission.
*/
private $abilities = [
'index' => 'view',
'index_data' => 'view',
'index_list' => 'view',
'edit' => 'edit',
'show' => 'view',
'update' => 'edit',
'create' => 'add',
'store' => 'add',
'destroy' => 'delete',
'restore' => 'restore',
'trashed' => 'restore',
];
/**
* Override of callAction to perform the authorization before.
*
* @return mixed
*/
public function callAction($method, $parameters)
{
if ($ability = $this->getAbility($method)) {
Gate::authorize($ability);
}
return parent::callAction($method, $parameters);
}
public function getAbility($method)
{
$routeName = explode('.', Route::currentRouteName());
$action = Arr::get($this->getAbilities(), $method);
return $action ? $action.'_'.$routeName[1] : null;
}
public function setAbilities($abilities)
{
$this->abilities = $abilities;
}
private function getAbilities()
{
return $this->abilities;
}
}