-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathOrmResolver.php
More file actions
183 lines (162 loc) · 5.75 KB
/
OrmResolver.php
File metadata and controls
183 lines (162 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 1.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Authorization\Policy;
use Authorization\Policy\Exception\MissingPolicyException;
use Cake\Core\App;
use Cake\Core\ContainerInterface;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\QueryInterface;
use Cake\Datasource\RepositoryInterface;
use RuntimeException;
/**
* Policy resolver that applies conventions based policy classes
* for CakePHP ORM Tables, Entities and Queries.
*/
class OrmResolver implements ResolverInterface
{
/**
* Application namespace.
*
* @var string
*/
protected string $appNamespace = 'App';
/**
* Plugin name overrides.
*
* @var array<string, string>
*/
protected array $overrides = [];
/**
* The DIC instance from the application
*
* @var \Cake\Core\ContainerInterface|null
*/
protected ?ContainerInterface $container;
/**
* Constructor
*
* @param string $appNamespace The application namespace
* @param array<string, string> $overrides A list of plugin name overrides.
* @param \Cake\Core\ContainerInterface|null $container The DIC instance from the application
*/
public function __construct(
string $appNamespace = 'App',
array $overrides = [],
?ContainerInterface $container = null,
) {
$this->appNamespace = $appNamespace;
$this->overrides = $overrides;
$this->container = $container;
}
/**
* Get a policy for an ORM Table, Entity or Query.
*
* @param mixed $resource The resource.
* @return mixed
* @throws \Authorization\Policy\Exception\MissingPolicyException When a policy for the
* resource has not been defined or cannot be resolved.
*/
public function getPolicy(mixed $resource): mixed
{
if ($resource instanceof EntityInterface) {
return $this->getEntityPolicy($resource);
}
if ($resource instanceof RepositoryInterface) {
return $this->getRepositoryPolicy($resource);
}
if ($resource instanceof QueryInterface) {
$repo = $resource->getRepository();
if ($repo === null) {
throw new RuntimeException('No repository set for the query.');
}
return $this->getRepositoryPolicy($repo);
}
throw new MissingPolicyException([get_debug_type($resource)]);
}
/**
* Get a policy for an entity
*
* @param \Cake\Datasource\EntityInterface $entity The entity to get a policy for
* @return mixed
*/
protected function getEntityPolicy(EntityInterface $entity): mixed
{
$class = get_class($entity);
$entityNamespace = '\Model\Entity\\';
$namespace = str_replace('\\', '/', substr($class, 0, (int)strpos($class, $entityNamespace)));
$name = substr($class, strpos($class, $entityNamespace) + strlen($entityNamespace));
return $this->findPolicy($class, $name, $namespace);
}
/**
* Get a policy for a table
*
* @param \Cake\Datasource\RepositoryInterface $table The table/repository to get a policy for.
* @return mixed
*/
protected function getRepositoryPolicy(RepositoryInterface $table): mixed
{
$class = get_class($table);
$tableNamespace = '\Model\Table\\';
$namespace = str_replace('\\', '/', substr($class, 0, (int)strpos($class, $tableNamespace)));
$name = substr($class, strpos($class, $tableNamespace) + strlen($tableNamespace));
return $this->findPolicy($class, $name, $namespace);
}
/**
* Locate a policy class using conventions
*
* @param string $class The full class name.
* @param string $name The name suffix of the resource.
* @param string $namespace The namespace to find the policy in.
* @throws \Authorization\Policy\Exception\MissingPolicyException When a policy for the
* resource has not been defined.
* @return mixed
*/
protected function findPolicy(string $class, string $name, string $namespace): mixed
{
$namespace = $this->getNamespace($namespace);
$policyClass = null;
// plugin entities can have application overrides defined.
if ($namespace !== $this->appNamespace) {
$policyClass = App::className($name, 'Policy\\' . $namespace, 'Policy');
}
// Check the application/plugin.
if ($policyClass === null) {
$policyClass = App::className($namespace . '.' . $name, 'Policy', 'Policy');
}
if ($policyClass === null) {
throw new MissingPolicyException([$class]);
}
if ($this->container && $this->container->has($policyClass)) {
$policy = $this->container->get($policyClass);
} else {
$policy = new $policyClass();
}
return $policy;
}
/**
* Returns plugin namespace override if exists.
*
* @param string $namespace The namespace to find the policy in.
* @return string
*/
protected function getNamespace(string $namespace): string
{
if (isset($this->overrides[$namespace])) {
return $this->overrides[$namespace];
}
return $namespace;
}
}