ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Acl.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Sabre\DAV;
11 
28 class Acl implements Element, HtmlOutput {
29 
35  protected $privileges;
36 
43  protected $prefixBaseUrl;
44 
62  function __construct(array $privileges, $prefixBaseUrl = true) {
63 
64  $this->privileges = $privileges;
65  $this->prefixBaseUrl = $prefixBaseUrl;
66 
67  }
68 
74  function getPrivileges() {
75 
76  return $this->privileges;
77 
78  }
79 
99  function xmlSerialize(Writer $writer) {
100 
101  foreach ($this->privileges as $ace) {
102 
103  $this->serializeAce($writer, $ace);
104 
105  }
106 
107  }
108 
124 
125  ob_start();
126  echo "<table>";
127  echo "<tr><th>Principal</th><th>Privilege</th><th></th></tr>";
128  foreach ($this->privileges as $privilege) {
129 
130  echo '<tr>';
131  // if it starts with a {, it's a special principal
132  if ($privilege['principal'][0] === '{') {
133  echo '<td>', $html->xmlName($privilege['principal']), '</td>';
134  } else {
135  echo '<td>', $html->link($privilege['principal']), '</td>';
136  }
137  echo '<td>', $html->xmlName($privilege['privilege']), '</td>';
138  echo '<td>';
139  if (!empty($privilege['protected'])) echo '(protected)';
140  echo '</td>';
141  echo '</tr>';
142 
143  }
144  echo "</table>";
145  return ob_get_clean();
146 
147  }
148 
170  static function xmlDeserialize(Reader $reader) {
171 
172  $elementMap = [
173  '{DAV:}ace' => 'Sabre\Xml\Element\KeyValue',
174  '{DAV:}privilege' => 'Sabre\Xml\Element\Elements',
175  '{DAV:}principal' => 'Sabre\DAVACL\Xml\Property\Principal',
176  ];
177 
178  $privileges = [];
179 
180  foreach ((array)$reader->parseInnerTree($elementMap) as $element) {
181 
182  if ($element['name'] !== '{DAV:}ace') {
183  continue;
184  }
185  $ace = $element['value'];
186 
187  if (empty($ace['{DAV:}principal'])) {
188  throw new DAV\Exception\BadRequest('Each {DAV:}ace element must have one {DAV:}principal element');
189  }
190  $principal = $ace['{DAV:}principal'];
191 
192  switch ($principal->getType()) {
193  case Principal::HREF :
194  $principal = $principal->getHref();
195  break;
197  $principal = '{DAV:}authenticated';
198  break;
200  $principal = '{DAV:}unauthenticated';
201  break;
202  case Principal::ALL :
203  $principal = '{DAV:}all';
204  break;
205 
206  }
207 
208  $protected = array_key_exists('{DAV:}protected', $ace);
209 
210  if (!isset($ace['{DAV:}grant'])) {
211  throw new DAV\Exception\NotImplemented('Every {DAV:}ace element must have a {DAV:}grant element. {DAV:}deny is not yet supported');
212  }
213  foreach ($ace['{DAV:}grant'] as $elem) {
214  if ($elem['name'] !== '{DAV:}privilege') {
215  continue;
216  }
217 
218  foreach ($elem['value'] as $priv) {
219  $privileges[] = [
220  'principal' => $principal,
221  'protected' => $protected,
222  'privilege' => $priv,
223  ];
224  }
225 
226  }
227 
228  }
229 
230  return new self($privileges);
231 
232  }
233 
241  private function serializeAce(Writer $writer, array $ace) {
242 
243  $writer->startElement('{DAV:}ace');
244 
245  switch ($ace['principal']) {
246  case '{DAV:}authenticated' :
247  $principal = new Principal(Principal::AUTHENTICATED);
248  break;
249  case '{DAV:}unauthenticated' :
250  $principal = new Principal(Principal::UNAUTHENTICATED);
251  break;
252  case '{DAV:}all' :
253  $principal = new Principal(Principal::ALL);
254  break;
255  default:
256  $principal = new Principal(Principal::HREF, $ace['principal']);
257  break;
258  }
259 
260  $writer->writeElement('{DAV:}principal', $principal);
261  $writer->startElement('{DAV:}grant');
262  $writer->startElement('{DAV:}privilege');
263 
264  $writer->writeElement($ace['privilege']);
265 
266  $writer->endElement(); // privilege
267  $writer->endElement(); // grant
268 
269  if (!empty($ace['protected'])) {
270  $writer->writeElement('{DAV:}protected');
271  }
272 
273  $writer->endElement(); // ace
274 
275  }
276 
277 }
parseInnerTree(array $elementMap=null)
Parses all elements below the current element.
Definition: Reader.php:129
const ALL
Everybody, basically.
Definition: Principal.php:41
getPrivileges()
Returns the list of privileges for this property.
Definition: Acl.php:74
startElement($name)
Opens a new element.
Definition: Writer.php:121
serializeAce(Writer $writer, array $ace)
Serializes a single access control entry.
Definition: Acl.php:241
__construct(array $privileges, $prefixBaseUrl=true)
Constructor.
Definition: Acl.php:62
const UNAUTHENTICATED
To specify a not-logged-in user, use the UNAUTHENTICATED principal.
Definition: Principal.php:26
The Reader class expands upon PHP&#39;s built-in XMLReader.
Definition: Reader.php:20
const AUTHENTICATED
To specify any principal that is logged in, use AUTHENTICATED.
Definition: Principal.php:31
link($url, $label=null)
Generates a full -tag.
WebDAV properties that implement this interface are able to generate their own html output for the br...
Definition: HtmlOutput.php:16
toHtml(HtmlOutputHelper $html)
Generate html representation for this value.
Definition: Acl.php:123
xmlName($element)
This method takes an xml element in clark-notation, and turns it into a shortened version with a pref...
This is the XML element interface.
Definition: Element.php:18
static xmlDeserialize(Reader $reader)
The deserialize method is called during xml parsing.
Definition: Acl.php:170
const HREF
Specific principals can be specified with the HREF.
Definition: Principal.php:36
xmlSerialize(Writer $writer)
The xmlSerialize method is called during xml writing.
Definition: Acl.php:99
This class represents the {DAV:}acl property.
Definition: Acl.php:28
$html
Definition: example_001.php:87
writeElement($name, $content=null)
Write a full element tag and it&#39;s contents.
Definition: Writer.php:189
The XML Writer class.
Definition: Writer.php:31
This class provides a few utility functions for easily generating HTML for the browser plugin...