1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* RBAC implementation for HiPanel |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hipanel-rbac |
6
|
|
|
* @package hipanel-rbac |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2016-2020, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\rbac\console; |
12
|
|
|
|
13
|
|
|
use hipanel\rbac\AuthManager; |
14
|
|
|
use hipanel\rbac\RbacIniterInterface; |
15
|
|
|
use Laminas\Code\Generator\FileGenerator; |
16
|
|
|
use Laminas\Code\Generator\ValueGenerator; |
17
|
|
|
|
18
|
|
|
final class MetadataGenerator |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var AuthManager |
22
|
|
|
*/ |
23
|
|
|
private $auth; |
24
|
|
|
/** |
25
|
|
|
* @var RbacIniterInterface |
26
|
|
|
*/ |
27
|
|
|
private $initer; |
28
|
|
|
|
29
|
|
|
public function __construct(AuthManager $authManager, RbacIniterInterface $initer) |
30
|
|
|
{ |
31
|
|
|
$this->auth = $authManager; |
32
|
|
|
$this->initer = $initer; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function dump(string $path): void |
36
|
|
|
{ |
37
|
|
|
$metadata = $this->initer->getMetadata(); |
|
|
|
|
38
|
|
|
$this->generateRoleDescriptions($metadata); |
39
|
|
|
$this->generatePermissionsDescriptions($metadata); |
40
|
|
|
|
41
|
|
|
$generator = FileGenerator::fromArray([ |
42
|
|
|
'body' => 'return ' . (new ValueGenerator($metadata, ValueGenerator::TYPE_ARRAY_SHORT))->generate() . ';', |
43
|
|
|
]); |
44
|
|
|
file_put_contents($path, $generator->generate(), LOCK_EX); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function generateRoleDescriptions(array &$metadata): void |
48
|
|
|
{ |
49
|
|
|
$descriptor = new RoleDescriptionGenerator(); |
50
|
|
|
foreach ($this->auth->getRoles() as $name => $role) { |
51
|
|
|
if (isset($metadata[$name])) { |
52
|
|
|
continue; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$metadata[$name] = ['description' => $descriptor($name)]; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function generatePermissionsDescriptions(array &$metadata): void |
60
|
|
|
{ |
61
|
|
|
$descriptor = new PermissionDescriptionGenerator(); |
62
|
|
|
$permissions = $this->auth->getPermissions(); |
63
|
|
|
ksort($permissions); |
64
|
|
|
foreach ($permissions as $name => $permission) { |
65
|
|
|
if (isset($metadata[$name])) { |
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$metadata[$name] = ['description' => $descriptor($name)]; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|