|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE |
|
6
|
|
|
* @link https://github.com/flipboxfactory/craft-ember/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\craft\ember\actions; |
|
10
|
|
|
|
|
11
|
|
|
use yii\web\ForbiddenHttpException; |
|
12
|
|
|
use yii\web\UnauthorizedHttpException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @author Flipbox Factory <[email protected]> |
|
16
|
|
|
* @since 2.0.0 |
|
17
|
|
|
* |
|
18
|
|
|
* @property int $errorCodeForbidden |
|
19
|
|
|
* @property string $messageForbidden |
|
20
|
|
|
*/ |
|
21
|
|
|
trait CheckAccessTrait |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var null|callable |
|
25
|
|
|
*/ |
|
26
|
|
|
public $checkAccess = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param mixed ...$params |
|
30
|
|
|
* @return mixed |
|
31
|
|
|
* @throws ForbiddenHttpException |
|
32
|
|
|
*/ |
|
33
|
|
|
public function checkAccess(...$params) |
|
34
|
|
|
{ |
|
35
|
|
|
if ($this->checkAccess) { |
|
36
|
|
|
if (call_user_func_array($this->checkAccess, $params) === false) { |
|
37
|
|
|
/** @noinspection PhpVoidFunctionResultUsedInspection */ |
|
38
|
|
|
return $this->handleForbiddenResponse(); |
|
39
|
|
|
}; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return true; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* HTTP forbidden response code |
|
47
|
|
|
* |
|
48
|
|
|
* @return int |
|
49
|
|
|
* |
|
50
|
|
|
* @deprecated |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function statusCodeUnauthorized(): int |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->statusCodeUnauthorized ?? 403; |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return string |
|
59
|
|
|
* |
|
60
|
|
|
* @deprecated |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function messageUnauthorized(): string |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->messageUnauthorized ?? 'Unable to perform action.'; |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @throws UnauthorizedHttpException |
|
69
|
|
|
* |
|
70
|
|
|
* @deprecated |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function handleUnauthorizedResponse() |
|
73
|
|
|
{ |
|
74
|
|
|
throw new UnauthorizedHttpException( |
|
75
|
|
|
$this->messageUnauthorized(), |
|
|
|
|
|
|
76
|
|
|
$this->statusCodeUnauthorized() |
|
|
|
|
|
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* HTTP forbidden response code |
|
82
|
|
|
* |
|
83
|
|
|
* @return int|null |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function errorCodeForbidden() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->errorCodeForbidden; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function messageForbidden(): string |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->messageForbidden ?? 'Unable to perform action.'; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @throws ForbiddenHttpException |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function handleForbiddenResponse() |
|
102
|
|
|
{ |
|
103
|
|
|
throw new ForbiddenHttpException( |
|
104
|
|
|
$this->messageForbidden(), |
|
105
|
|
|
$this->errorCodeForbidden() |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: