|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Spiral Framework. |
|
4
|
|
|
* |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
* @author Anton Titov (Wolfy-J) |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Spiral\Vault; |
|
10
|
|
|
|
|
11
|
|
|
use Psr\Http\Message\UriInterface; |
|
12
|
|
|
use Spiral\Core\Component; |
|
13
|
|
|
use Spiral\Core\Exceptions\ControllerException; |
|
14
|
|
|
use Spiral\Core\HMVC\CoreInterface; |
|
15
|
|
|
use Spiral\Security\Traits\GuardedTrait; |
|
16
|
|
|
use Spiral\Vault\Configs\VaultConfig; |
|
17
|
|
|
use Spiral\Vault\Exceptions\VaultException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Vault Core provides ability to whitelist controllers, map their short names and aliases into |
|
21
|
|
|
* specific class and automatically check Actor permission to execute any of controller actions. |
|
22
|
|
|
*/ |
|
23
|
|
|
class Vault extends Component implements CoreInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use GuardedTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var VaultConfig |
|
29
|
|
|
*/ |
|
30
|
|
|
private $config = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var VaultRoute |
|
34
|
|
|
*/ |
|
35
|
|
|
private $route; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var CoreInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $app = null; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param VaultConfig $config |
|
44
|
|
|
* @param VaultRoute $route |
|
45
|
|
|
* @param CoreInterface $app User application. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(VaultConfig $config, VaultRoute $route, CoreInterface $app) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->config = $config; |
|
50
|
|
|
$this->route = $route; |
|
51
|
|
|
$this->app = $app; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return VaultConfig |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getConfig(): VaultConfig |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->config; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function callAction( |
|
66
|
|
|
string $controller, |
|
67
|
|
|
string $action = null, |
|
68
|
|
|
array $parameters = [], |
|
69
|
|
|
array $scope = [] |
|
70
|
|
|
) { |
|
71
|
|
|
if (!$this->config->hasController($controller)) { |
|
72
|
|
|
throw new ControllerException( |
|
73
|
|
|
"Undefined vault controller '{$controller}'", |
|
74
|
|
|
ControllerException::NOT_FOUND |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$actionPermission = "{$this->config->guardNamespace()}.{$controller}"; |
|
79
|
|
|
|
|
80
|
|
|
if (!$this->getGuard()->allows($actionPermission, compact('action'))) { |
|
81
|
|
|
throw new ControllerException( |
|
82
|
|
|
"Unreachable vault controller '{$controller}'", |
|
83
|
|
|
ControllerException::FORBIDDEN |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
//Delegate controller call to real application |
|
88
|
|
|
return $this->app->callAction( |
|
89
|
|
|
$this->config->controllerClass($controller), |
|
90
|
|
|
$action, |
|
91
|
|
|
$parameters, |
|
92
|
|
|
$scope + [Vault::class => $this] |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get vault specific uri. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $target Target controller and action in a form of "controller::action" or |
|
100
|
|
|
* "controller:action" or "controller". |
|
101
|
|
|
* @param array|mixed $parameters |
|
102
|
|
|
* |
|
103
|
|
|
* @return UriInterface |
|
104
|
|
|
* |
|
105
|
|
|
* @throws VaultException |
|
106
|
|
|
*/ |
|
107
|
|
|
public function uri(string $target, $parameters = []): UriInterface |
|
108
|
|
|
{ |
|
109
|
|
|
if (strpos($target, ':') !== false) { |
|
110
|
|
|
list($controller, $action) = explode(':', $target); |
|
111
|
|
|
} else { |
|
112
|
|
|
$controller = $target; |
|
113
|
|
|
$action = $parameters['action'] ?? null; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if (!$this->config->hasController($controller)) { |
|
117
|
|
|
throw new VaultException("Unable to generate uri, undefined controller '{$controller}'"); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $this->route->withDefaults(compact('controller', 'action'))->uri($parameters); |
|
121
|
|
|
} |
|
122
|
|
|
} |