|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** Created by PhpStorm, User: jonphipps, Date: 2017-05-30, Time: 11:57 AM */ |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Http\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
|
9
|
|
|
|
|
10
|
|
|
trait UsesPolicies |
|
11
|
|
|
{ |
|
12
|
|
|
use AuthorizesRequests; |
|
13
|
|
|
|
|
14
|
|
|
/** bulk authorize based on policies |
|
15
|
|
|
*/ |
|
16
|
|
|
public function authorizeAll(): void |
|
17
|
|
|
{ |
|
18
|
|
|
if (! auth()->check()) { |
|
19
|
|
|
return; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
$model = $this->crud->getModel(); |
|
|
|
|
|
|
23
|
|
|
$authArray = [ |
|
24
|
|
|
'list' => 'view', |
|
25
|
|
|
'create' => 'create', |
|
26
|
|
|
'edit' => 'update', |
|
27
|
|
|
'update' => 'update', |
|
28
|
|
|
'destroy' => 'delete', |
|
29
|
|
|
'delete' => 'delete', |
|
30
|
|
|
'show' => 'view', |
|
31
|
|
|
'details_row' => 'view', |
|
32
|
|
|
]; |
|
33
|
|
|
$done = []; |
|
34
|
|
|
foreach ($authArray as $key => $ability) { |
|
35
|
|
|
$this->crud->denyAccess([$ability]); |
|
36
|
|
|
if (\in_array($ability, $done, true)) { |
|
37
|
|
|
$this->crud->allowAccess([$key]); |
|
38
|
|
|
continue; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
switch ($ability) { |
|
42
|
|
|
case 'view': |
|
43
|
|
|
if (auth()->user()->can($ability, $model)) { |
|
44
|
|
|
$this->crud->allowAccess([$key]); |
|
45
|
|
|
} |
|
46
|
|
|
break; |
|
47
|
|
|
case 'create': |
|
48
|
|
|
if (auth()->user()->can($ability, \get_class($model))) { |
|
49
|
|
|
$this->crud->allowAccess([$key]); |
|
50
|
|
|
} |
|
51
|
|
|
break; |
|
52
|
|
|
default: |
|
53
|
|
|
if (auth()->user()->can($ability, $model)) { |
|
54
|
|
|
$this->crud->allowAccess([$key]); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
$done[] = $ability; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
|
63
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
|
64
|
|
|
*/ |
|
65
|
|
|
public function create() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->policyAuthorize('create', \get_class($this->crud->getModel())); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
return parent::create(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param $id |
|
74
|
|
|
* |
|
75
|
|
|
* @return |
|
76
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
|
77
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
|
78
|
|
|
*/ |
|
79
|
|
|
public function destroy($id) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->policyAuthorize('delete', $this->crud->getModel(), $id); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
return parent::destroy($id); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param $id |
|
88
|
|
|
* |
|
89
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
|
90
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
|
91
|
|
|
*/ |
|
92
|
|
|
public function edit($id) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->policyAuthorize('update', $this->crud->getModel(), $id); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
return parent::edit($id); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
|
101
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
|
102
|
|
|
*/ |
|
103
|
|
|
public function index() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->policyAuthorize('list', \get_class($this->crud->getModel())); |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
return parent::index(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function list() |
|
111
|
|
|
{ |
|
112
|
|
|
$this->index(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param $id |
|
117
|
|
|
* |
|
118
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
|
119
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
|
120
|
|
|
*/ |
|
121
|
|
|
public function show($id) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->policyAuthorize('show', $this->crud->getModel(), $id); |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
return parent::show($id); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Determines the access to allow based on policy. |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $ability The ability to validate |
|
132
|
|
|
* @param string|Model $class The instance of a Model class to check against |
|
133
|
|
|
* @param int|null $id The id of the individual to check against |
|
134
|
|
|
* |
|
135
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
|
136
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function policyAuthorize($ability, $class, $id = null): void |
|
139
|
|
|
{ |
|
140
|
|
|
//if the controller had pre-authorized access then bail |
|
141
|
|
|
if ($this->crud->hasAccess($ability)) { |
|
|
|
|
|
|
142
|
|
|
return; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
//the 'model' will either be a valid instance or the class name |
|
146
|
|
|
$model = $id !== null ? $class->findOrFail($id) : $class; |
|
147
|
|
|
|
|
148
|
|
|
//deny access to the ability by default |
|
149
|
|
|
$this->crud->denyAccess([$ability]); |
|
150
|
|
|
//let the gate decide -- if there's a user and the user is authorized |
|
151
|
|
|
$this->authorize($ability, $model); |
|
152
|
|
|
//if we get this far, then the gate has allowed access and we pass the authorization on to backpack |
|
153
|
|
|
$this->crud->allowAccess([$ability]); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|