1 | <?php |
||
19 | final class GrantsTree |
||
20 | { |
||
21 | use \arc\traits\Proxy { |
||
22 | \arc\traits\Proxy::__construct as private ProxyConstruct; |
||
23 | } |
||
24 | |||
25 | private $tree = null; |
||
26 | private $user = null; |
||
27 | private $groups = array(); |
||
28 | |||
29 | /** |
||
30 | * @param \arc\tree\NamedNode $tree The tree storage for event listeners. |
||
31 | * @param string $user |
||
32 | * @param array $groups |
||
33 | */ |
||
34 | 2 | public function __construct( $tree, $user, $groups = array() ) |
|
41 | |||
42 | /** |
||
43 | * Change to a different node |
||
44 | * @param $path |
||
45 | * @return GrantsTree |
||
46 | */ |
||
47 | 1 | public function cd($path) |
|
51 | |||
52 | /** |
||
53 | * Switch default user to check and set grants. |
||
54 | * @param $user |
||
55 | * @param array $groups |
||
56 | * @return GrantsTree this |
||
57 | */ |
||
58 | 1 | public function switchUser( $user, $groups = [] ) |
|
62 | |||
63 | /** |
||
64 | * Set new grants for the current user. |
||
65 | * @param null $grants |
||
66 | * @return GrantsTree this |
||
67 | */ |
||
68 | 1 | public function set($grants = null) |
|
69 | { |
||
70 | 1 | if ( isset( $grants ) ) { |
|
71 | 1 | if ( !isset($this->tree->nodeValue['users'])) { |
|
72 | 1 | $this->tree->nodeValue['users'] = []; |
|
73 | } |
||
74 | 1 | $this->tree->nodeValue['users'][$this->user ] = ' ' . trim( $grants ) . ' '; |
|
75 | } else { |
||
76 | unset( $this->tree->nodeValue['users'][$this->user ] ); |
||
77 | } |
||
78 | 1 | return $this; |
|
79 | } |
||
80 | |||
81 | |||
82 | /** |
||
83 | * Deprecated. Use set() instead. |
||
84 | * @param null $grants |
||
85 | */ |
||
86 | 1 | public function setUserGrants( $grants = null ){ |
|
89 | |||
90 | /** |
||
91 | * Deprecated. Use setForGroup instead. |
||
92 | * @param $group |
||
93 | * @param null $grants |
||
94 | */ |
||
95 | public function setGroupGrants( $group, $grants = null ){ |
||
96 | $this->setForGroup($group, $grants); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Set new grants for the given user. |
||
101 | * @param $user |
||
102 | * @param null $grants |
||
103 | * @return GrantsTree this |
||
104 | */ |
||
105 | public function setForUser( $user, $grants = null ) { |
||
109 | |||
110 | /** |
||
111 | * Set new grants for the given group. |
||
112 | * @param $group |
||
113 | * @param null $grants |
||
114 | * @return GrantsTree this |
||
115 | */ |
||
116 | public function setForGroup( $group, $grants = null ) { |
||
117 | if ( isset( $grants ) ) { |
||
118 | if ( !isset($this->tree->nodeValue['groups'])) { |
||
119 | $this->tree->nodeValue['groups'] = []; |
||
120 | } |
||
121 | $this->tree->nodeValue['groups'][$group ] = ' ' . trim( $grants ) . ' '; |
||
122 | } else { |
||
123 | unset( $this->tree->nodeValue['groups'][$group ] ); |
||
124 | } |
||
125 | return $this; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Return the grants for the current user. |
||
130 | * @return mixed |
||
131 | */ |
||
132 | public function grants() { |
||
135 | |||
136 | /** |
||
137 | * Return the grants for a specific user. |
||
138 | * @param $user |
||
139 | * @return mixed |
||
140 | */ |
||
141 | public function grantsForUser($user, $groups = []) { |
||
144 | |||
145 | /** |
||
146 | * Returns an array with the grants for all users. |
||
147 | * @return array |
||
148 | */ |
||
149 | public function grantsForAllUsers() { |
||
152 | |||
153 | /** |
||
154 | * Return the grants for a specific group. |
||
155 | * @param $group |
||
156 | * @return mixed |
||
157 | */ |
||
158 | public function grantsForGroup($group) { |
||
161 | |||
162 | /** |
||
163 | * Returns an array with the grants for all groups. |
||
164 | * @return array |
||
165 | */ |
||
166 | public function grantsForAllGroups() { |
||
169 | |||
170 | /** |
||
171 | * @param $grant |
||
172 | * @return bool |
||
173 | */ |
||
174 | 2 | public function check($grant) |
|
186 | |||
187 | /** |
||
188 | * Check grant for a specific user. |
||
189 | * @param $user |
||
190 | * @param $grant |
||
191 | * @return bool |
||
192 | */ |
||
193 | public function checkForUser($grant, $user, $groups = []) { |
||
196 | |||
197 | /** |
||
198 | * @return string |
||
199 | */ |
||
200 | 2 | private function fetchGrants() |
|
232 | } |
||
233 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.