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