1 | <?php |
||
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() |
||
79 | |||
80 | /** |
||
81 | * HTTP forbidden response code |
||
82 | * |
||
83 | * @return int|null |
||
84 | */ |
||
85 | protected function errorCodeForbidden() |
||
89 | |||
90 | /** |
||
91 | * @return string |
||
92 | */ |
||
93 | protected function messageForbidden(): string |
||
97 | |||
98 | /** |
||
99 | * @throws ForbiddenHttpException |
||
100 | */ |
||
101 | protected function handleForbiddenResponse() |
||
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: