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
|
|
|
final class PermissionDescriptionGenerator |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $module; |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $operation; |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $rawName; |
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
private $allows; |
31
|
|
|
|
32
|
|
|
public function __invoke(string $name): string |
33
|
|
|
{ |
34
|
|
|
$this->parse($name); |
35
|
|
|
|
36
|
|
|
return strtr('{allowance}{operation}', [ |
37
|
|
|
'{allowance}' => $this->formatAllowance(), |
38
|
|
|
'{operation}' => $this->formatOperation(), |
39
|
|
|
]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function parse(string $name): void |
43
|
|
|
{ |
44
|
|
|
$this->module = null; |
45
|
|
|
$this->rawName = $name; |
46
|
|
|
$this->allows = strpos($name, 'deny:') === false; |
47
|
|
|
if (!$this->allows) { |
48
|
|
|
[, $name] = explode(':', $name, 2); |
49
|
|
|
} |
50
|
|
|
$this->operation = $name; |
51
|
|
|
if (strpos($name, '.') !== false) { |
52
|
|
|
[$this->module, $this->operation] = explode('.', $name, 2); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function formatAllowance(): string |
57
|
|
|
{ |
58
|
|
|
if (!$this->allows) { |
59
|
|
|
return 'Prohibits '; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return 'Allows '; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function formatOperation(): string |
66
|
|
|
{ |
67
|
|
|
if (preg_match('/^[a-z]+$/', $this->operation)) { |
68
|
|
|
$exceptions = [ |
69
|
|
|
'createing' => 'creating', |
70
|
|
|
'updateing' => 'updating', |
71
|
|
|
'deleteing' => 'deleting', |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
$result = strtr($this->operation . 'ing', $exceptions); |
75
|
|
|
|
76
|
|
|
if ($this->module) { |
77
|
|
|
return $result . ' of the ' . $this->module; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $result; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($this->module) { |
84
|
|
|
return $this->operation . ' operation on the ' . $this->module; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->operation . ' operation'; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|