1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Integrations\Traits; |
4
|
|
|
|
5
|
|
|
use Integrations\Exceptions\AccessDeniedException; |
6
|
|
|
|
7
|
|
|
trait Access |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Set an operation as having access using the Settings API. |
11
|
|
|
* |
12
|
|
|
* @param string $operation |
13
|
|
|
* |
14
|
|
|
* @return bool |
15
|
|
|
*/ |
16
|
|
|
public function allowAccess($operation) |
17
|
|
|
{ |
18
|
|
|
foreach ((array) $operation as $op) { |
19
|
|
|
$this->set($op.'.access', true); |
|
|
|
|
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
return $this->hasAccessToAll($operation); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Disable the access to a certain operation, or the current one. |
27
|
|
|
* |
28
|
|
|
* @param bool $operation [description] |
29
|
|
|
* |
30
|
|
|
* @return [type] [description] |
|
|
|
|
31
|
|
|
*/ |
32
|
|
|
public function denyAccess($operation) |
33
|
|
|
{ |
34
|
|
|
foreach ((array) $operation as $op) { |
35
|
|
|
$this->set($op.'.access', false); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return ! $this->hasAccessToAny($operation); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Check if a operation is allowed for a Crud Panel. Return false if not. |
43
|
|
|
* |
44
|
|
|
* @param string $operation |
45
|
|
|
* |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
public function hasAccess($operation) |
49
|
|
|
{ |
50
|
|
|
return $this->get($operation.'.access') ?? false; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Check if any operations are allowed for a Crud Panel. Return false if not. |
55
|
|
|
* |
56
|
|
|
* @param array $operation_array |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function hasAccessToAny($operation_array) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
foreach ((array) $operation_array as $key => $operation) { |
63
|
|
|
if ($this->get($operation.'.access') == true) { |
|
|
|
|
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Check if all operations are allowed for a Crud Panel. Return false if not. |
73
|
|
|
* |
74
|
|
|
* @param array $operation_array Permissions. |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function hasAccessToAll($operation_array) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
foreach ((array) $operation_array as $key => $operation) { |
81
|
|
|
if (! $this->get($operation.'.access')) { |
|
|
|
|
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Check if a operation is allowed for a Crud Panel. Fail if not. |
91
|
|
|
* |
92
|
|
|
* @param string $operation |
93
|
|
|
* |
94
|
|
|
* @throws \Backpack\CRUD\Exception\AccessDeniedException in case the operation is not enabled |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function hasAccessOrFail($operation) |
99
|
|
|
{ |
100
|
|
|
if (! $this->get($operation.'.access')) { |
|
|
|
|
101
|
|
|
throw new AccessDeniedException(trans('backpack::crud.unauthorized_access', ['access' => $operation])); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.