1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* BEdita, API-first content management framework |
6
|
|
|
* Copyright 2022 Atlas Srl, Chialab Srl |
7
|
|
|
* |
8
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
10
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
14
|
|
|
*/ |
15
|
|
|
namespace BEdita\WebTools\View\Helper; |
16
|
|
|
|
17
|
|
|
use Authentication\View\Helper\IdentityHelper as AuthenticationIdentityHelper; |
18
|
|
|
use BadMethodCallException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Extends IdentityHelper allowing to delegate methods to the identity. |
22
|
|
|
*/ |
23
|
|
|
class IdentityHelper extends AuthenticationIdentityHelper |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Configuration options |
27
|
|
|
* |
28
|
|
|
* - `identityAttribute` - The request attribute which holds the identity. |
29
|
|
|
* - `delagateMethods` - Methods delegated to identity. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected array $_defaultConfig = [ |
34
|
|
|
'identityAttribute' => 'identity', |
35
|
|
|
'delegateMethods' => [ |
36
|
|
|
'hasRole', |
37
|
|
|
], |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Delegate methods to identity. |
42
|
|
|
* |
43
|
|
|
* @param string $method The method being invoked. |
44
|
|
|
* @param array $args The arguments for the method. |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
|
|
public function __call(string $method, array $args): mixed |
48
|
|
|
{ |
49
|
|
|
if (!in_array($method, (array)$this->getConfig('delegateMethods'))) { |
50
|
|
|
throw new BadMethodCallException("Cannot call `{$method}`. Make sure to add it to `delegateMethods`."); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (!is_object($this->_identity)) { |
54
|
|
|
throw new BadMethodCallException("Cannot call `{$method}` on stored identity since it is not an object."); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$call = [$this->_identity, $method]; |
58
|
|
|
|
59
|
|
|
return $call(...$args); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|