1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Exceptions\AccessDeniedException; |
6
|
|
|
|
7
|
|
|
trait Access |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Set an operation as having access using the Settings API. |
11
|
|
|
* |
12
|
|
|
* @param string|array $operation |
13
|
|
|
* @return bool |
14
|
|
|
*/ |
15
|
|
|
public function allowAccess($operation) |
16
|
|
|
{ |
17
|
|
|
foreach ((array) $operation as $op) { |
18
|
|
|
$this->set($op.'.access', true); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
return $this->hasAccessToAll($operation); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Disable the access to a certain operation, or the current one. |
26
|
|
|
* |
27
|
|
|
* @param string|array $operation [description] |
28
|
|
|
* @return [type] [description] |
|
|
|
|
29
|
|
|
*/ |
30
|
|
|
public function denyAccess($operation) |
31
|
|
|
{ |
32
|
|
|
foreach ((array) $operation as $op) { |
33
|
|
|
$this->set($op.'.access', false); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return ! $this->hasAccessToAny($operation); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Check if a operation is allowed for a Crud Panel. Return false if not. |
41
|
|
|
* |
42
|
|
|
* @param string $operation |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function hasAccess($operation) |
46
|
|
|
{ |
47
|
|
|
return $this->get($operation.'.access') ?? false; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Check if any operations are allowed for a Crud Panel. Return false if not. |
52
|
|
|
* |
53
|
|
|
* @param string|array $operation_array |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function hasAccessToAny($operation_array) |
57
|
|
|
{ |
58
|
|
|
foreach ((array) $operation_array as $key => $operation) { |
59
|
|
|
if ($this->get($operation.'.access') == true) { |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Check if all operations are allowed for a Crud Panel. Return false if not. |
69
|
|
|
* |
70
|
|
|
* @param array $operation_array Permissions. |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function hasAccessToAll($operation_array) |
74
|
|
|
{ |
75
|
|
|
foreach ((array) $operation_array as $key => $operation) { |
76
|
|
|
if (! $this->get($operation.'.access')) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Check if a operation is allowed for a Crud Panel. Fail if not. |
86
|
|
|
* |
87
|
|
|
* @param string $operation |
88
|
|
|
* @return bool |
89
|
|
|
* |
90
|
|
|
* @throws \Backpack\CRUD\Exception\AccessDeniedException in case the operation is not enabled |
91
|
|
|
*/ |
92
|
|
|
public function hasAccessOrFail($operation) |
93
|
|
|
{ |
94
|
|
|
if (! $this->get($operation.'.access')) { |
95
|
|
|
throw new AccessDeniedException(trans('backpack::crud.unauthorized_access', ['access' => $operation])); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|