1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Exceptions\AccessDeniedException; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
|
8
|
|
|
trait Access |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Set an operation as having access using the Settings API. |
12
|
|
|
*/ |
13
|
|
|
public function allowAccess(array|string $operation): bool |
14
|
|
|
{ |
15
|
|
|
foreach ((array) $operation as $op) { |
16
|
|
|
$this->set($op.'.access', true); |
|
|
|
|
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
return $this->hasAccessToAll($operation); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Disable the access to a certain operation, or the current one. |
24
|
|
|
*/ |
25
|
|
|
public function denyAccess(array|string $operation): bool |
26
|
|
|
{ |
27
|
|
|
foreach ((array) $operation as $op) { |
28
|
|
|
$this->set($op.'.access', false); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return ! $this->hasAccessToAny($operation); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Check if a operation is allowed for a Crud Panel. Return false if not. |
36
|
|
|
*/ |
37
|
|
|
public function hasAccess(string $operation, $entry = null): bool |
38
|
|
|
{ |
39
|
|
|
$condition = $this->get($operation.'.access'); |
|
|
|
|
40
|
|
|
|
41
|
|
|
if (is_callable($condition)) { |
42
|
|
|
// supply the current entry, if $entry is missing |
43
|
|
|
// this also makes sure the entry is null when missing |
44
|
|
|
$entry ??= $this->getCurrentEntry() ?: null; |
|
|
|
|
45
|
|
|
|
46
|
|
|
return $condition($entry); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $condition ?? false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Check if any operations are allowed for a Crud Panel. Return false if not. |
54
|
|
|
*/ |
55
|
|
|
public function hasAccessToAny(array|string $operation_array, ?Model $entry = null): bool |
56
|
|
|
{ |
57
|
|
|
foreach ((array) $operation_array as $key => $operation) { |
58
|
|
|
if ($this->hasAccess($operation, $entry) == true) { |
|
|
|
|
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Check if all operations are allowed for a Crud Panel. Return false if not. |
68
|
|
|
*/ |
69
|
|
|
public function hasAccessToAll(array|string $operation_array, ?Model $entry = null): bool |
70
|
|
|
{ |
71
|
|
|
foreach ((array) $operation_array as $key => $operation) { |
72
|
|
|
if (! $this->hasAccess($operation, $entry)) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Check if a operation is allowed for a Crud Panel. Fail if not. |
82
|
|
|
* |
83
|
|
|
* @throws \Backpack\CRUD\Exception\AccessDeniedException in case the operation is not enabled |
84
|
|
|
*/ |
85
|
|
|
public function hasAccessOrFail(string $operation, ?Model $entry = null): bool |
86
|
|
|
{ |
87
|
|
|
if (! $this->hasAccess($operation, $entry)) { |
88
|
|
|
throw new AccessDeniedException(trans('backpack::crud.unauthorized_access', ['access' => $operation])); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get an operation's access condition, if set. A condition |
96
|
|
|
* can be anything, but usually a boolean or a callable. |
97
|
|
|
*/ |
98
|
|
|
public function getAccessCondition(string $operation): bool|callable|null |
99
|
|
|
{ |
100
|
|
|
return $this->get($operation.'.access'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set the condition under which an operation is allowed for a Crud Panel. |
105
|
|
|
*/ |
106
|
|
|
public function setAccessCondition(array|string $operation, bool|callable|null $condition): void |
107
|
|
|
{ |
108
|
|
|
foreach ((array) $operation as $op) { |
109
|
|
|
$this->set($op.'.access', $condition); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Check if an operation has an access condition already set. |
115
|
|
|
* A condition can be anything, but usually a boolean or a callable. |
116
|
|
|
*/ |
117
|
|
|
public function hasAccessCondition(string $operation): bool |
118
|
|
|
{ |
119
|
|
|
return $this->get($operation.'.access') !== null; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Remove the access to all available operations. |
124
|
|
|
*/ |
125
|
|
|
public function denyAllAccess(): void |
126
|
|
|
{ |
127
|
|
|
$this->denyAccess($this->getAvailableOperationsList()); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|