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(array|string $operation) : bool |
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(array|string $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
|
|
|
* @param \Model $entry |
|
|
|
|
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
public function hasAccess(string $operation, $entry = null) : bool |
47
|
|
|
{ |
48
|
|
|
$condition = $this->get($operation.'.access'); |
|
|
|
|
49
|
|
|
|
50
|
|
|
if (is_callable($condition)) { |
51
|
|
|
$entry = $entry ?? $this->getCurrentEntry(); |
|
|
|
|
52
|
|
|
return $condition($entry); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $condition ?? false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Check if any operations are allowed for a Crud Panel. Return false if not. |
60
|
|
|
* |
61
|
|
|
* @param string|array $operation_array |
62
|
|
|
* @param \Model $entry |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function hasAccessToAny(array|string $operation_array, $entry = null) : bool |
66
|
|
|
{ |
67
|
|
|
foreach ((array) $operation_array as $key => $operation) { |
68
|
|
|
if ($this->hasAccess($operation, $entry) == true) { |
|
|
|
|
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Check if all operations are allowed for a Crud Panel. Return false if not. |
78
|
|
|
* |
79
|
|
|
* @param array $operation_array Permissions. |
80
|
|
|
* @param \Model $entry |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function hasAccessToAll(array|string $operation_array, $entry = null) : bool |
84
|
|
|
{ |
85
|
|
|
foreach ((array) $operation_array as $key => $operation) { |
86
|
|
|
if (! $this->hasAccess($operation, $entry)) { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Check if a operation is allowed for a Crud Panel. Fail if not. |
96
|
|
|
* |
97
|
|
|
* @param string $operation |
98
|
|
|
* @param \Model $entry |
99
|
|
|
* @return bool |
100
|
|
|
* |
101
|
|
|
* @throws \Backpack\CRUD\Exception\AccessDeniedException in case the operation is not enabled |
102
|
|
|
*/ |
103
|
|
|
public function hasAccessOrFail(string $operation, $entry = null) : bool |
104
|
|
|
{ |
105
|
|
|
if (! $this->hasAccess($operation, $entry)) { |
106
|
|
|
throw new AccessDeniedException(trans('backpack::crud.unauthorized_access', ['access' => $operation])); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get an operation's access condition, if set. A condition |
114
|
|
|
* can be anything, but usually a boolean or a callable. |
115
|
|
|
* |
116
|
|
|
* @param string $operation |
117
|
|
|
* @return bool|callable|null |
118
|
|
|
*/ |
119
|
|
|
public function getAccessCondition(string $operation) : bool|callable|null |
120
|
|
|
{ |
121
|
|
|
return $this->get($operation.'.access'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set the condition under which an operation is allowed for a Crud Panel. |
126
|
|
|
* |
127
|
|
|
* @param string|array $operation |
128
|
|
|
* @param bool|callable|null $condition |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
public function setAccessCondition(array|string $operation, bool|callable|null $condition) : void |
132
|
|
|
{ |
133
|
|
|
foreach ((array) $operation as $op) { |
134
|
|
|
$this->set($op.'.access', $condition); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Check if an operation has an access condition already set. |
140
|
|
|
* A condition can be anything, but usually a boolean or a callable. |
141
|
|
|
* |
142
|
|
|
* @param string $operation |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function hasAccessCondition(string $operation) : bool |
146
|
|
|
{ |
147
|
|
|
return $this->get($operation.'.access') !== null; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|