|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\User\Acl; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
// From Pimple |
|
8
|
|
|
use Pimple\Container; |
|
9
|
|
|
|
|
10
|
|
|
// From 'charcoal-core' |
|
11
|
|
|
use Charcoal\Model\AbstractModel; |
|
12
|
|
|
|
|
13
|
|
|
// From 'charcoal-translator' |
|
14
|
|
|
use Charcoal\Translator\TranslatorAwareTrait; |
|
15
|
|
|
|
|
16
|
|
|
// From 'charcoal-object' |
|
17
|
|
|
use Charcoal\Object\CategorizableInterface; |
|
18
|
|
|
use Charcoal\Object\CategorizableTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* A permission is a simple string, that can be read with additional data (name + category) from storage. |
|
22
|
|
|
*/ |
|
23
|
|
|
class Permission extends AbstractModel implements CategorizableInterface |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
use CategorizableTrait; |
|
26
|
|
|
use TranslatorAwareTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string|null |
|
30
|
|
|
*/ |
|
31
|
|
|
private $ident; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var \Charcoal\Translator\Translation|null |
|
35
|
|
|
*/ |
|
36
|
|
|
private $name; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Permission can be used as a string (ident). |
|
40
|
|
|
* |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __toString() |
|
44
|
|
|
{ |
|
45
|
|
|
if ($this->ident === null) { |
|
46
|
|
|
return ''; |
|
47
|
|
|
} |
|
48
|
|
|
return $this->ident; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function key() |
|
55
|
|
|
{ |
|
56
|
|
|
return 'ident'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $ident The permission identifier. |
|
61
|
|
|
* @throws InvalidArgumentException If the ident is not a string. |
|
62
|
|
|
* @return self |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setIdent($ident) |
|
65
|
|
|
{ |
|
66
|
|
|
if (!is_string($ident)) { |
|
67
|
|
|
throw new InvalidArgumentException( |
|
68
|
|
|
'Permission ident needs to be a string' |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
$this->ident = $ident; |
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return string|null |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getIdent() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->ident; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param mixed $name The permission name / label. |
|
85
|
|
|
* @return self |
|
86
|
|
|
*/ |
|
87
|
|
|
public function setName($name) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->name = $this->translator()->translation($name); |
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return \Charcoal\Translator\Translation|null |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getName() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->name; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param Container $container Pimple DI container. |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function setDependencies(Container $container) |
|
106
|
|
|
{ |
|
107
|
|
|
parent::setDependencies($container); |
|
108
|
|
|
$this->setTranslator($container['translator']); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|