1*a1a3b679SAndreas Boehler<?php 2*a1a3b679SAndreas Boehler 3*a1a3b679SAndreas Boehlernamespace Sabre\DAVACL\Xml\Property; 4*a1a3b679SAndreas Boehler 5*a1a3b679SAndreas Boehleruse Sabre\DAV\Browser\HtmlOutput; 6*a1a3b679SAndreas Boehleruse Sabre\DAV\Browser\HtmlOutputHelper; 7*a1a3b679SAndreas Boehleruse Sabre\Xml\Element; 8*a1a3b679SAndreas Boehleruse Sabre\Xml\Reader; 9*a1a3b679SAndreas Boehleruse Sabre\Xml\Writer; 10*a1a3b679SAndreas Boehler 11*a1a3b679SAndreas Boehler/** 12*a1a3b679SAndreas Boehler * CurrentUserPrivilegeSet 13*a1a3b679SAndreas Boehler * 14*a1a3b679SAndreas Boehler * This class represents the current-user-privilege-set property. When 15*a1a3b679SAndreas Boehler * requested, it contain all the privileges a user has on a specific node. 16*a1a3b679SAndreas Boehler * 17*a1a3b679SAndreas Boehler * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/). 18*a1a3b679SAndreas Boehler * @author Evert Pot (http://evertpot.com/) 19*a1a3b679SAndreas Boehler * @license http://sabre.io/license/ Modified BSD License 20*a1a3b679SAndreas Boehler */ 21*a1a3b679SAndreas Boehlerclass CurrentUserPrivilegeSet implements Element, HtmlOutput { 22*a1a3b679SAndreas Boehler 23*a1a3b679SAndreas Boehler /** 24*a1a3b679SAndreas Boehler * List of privileges 25*a1a3b679SAndreas Boehler * 26*a1a3b679SAndreas Boehler * @var array 27*a1a3b679SAndreas Boehler */ 28*a1a3b679SAndreas Boehler private $privileges; 29*a1a3b679SAndreas Boehler 30*a1a3b679SAndreas Boehler /** 31*a1a3b679SAndreas Boehler * Creates the object 32*a1a3b679SAndreas Boehler * 33*a1a3b679SAndreas Boehler * Pass the privileges in clark-notation 34*a1a3b679SAndreas Boehler * 35*a1a3b679SAndreas Boehler * @param array $privileges 36*a1a3b679SAndreas Boehler */ 37*a1a3b679SAndreas Boehler function __construct(array $privileges) { 38*a1a3b679SAndreas Boehler 39*a1a3b679SAndreas Boehler $this->privileges = $privileges; 40*a1a3b679SAndreas Boehler 41*a1a3b679SAndreas Boehler } 42*a1a3b679SAndreas Boehler 43*a1a3b679SAndreas Boehler /** 44*a1a3b679SAndreas Boehler * The xmlSerialize metod is called during xml writing. 45*a1a3b679SAndreas Boehler * 46*a1a3b679SAndreas Boehler * Use the $writer argument to write its own xml serialization. 47*a1a3b679SAndreas Boehler * 48*a1a3b679SAndreas Boehler * An important note: do _not_ create a parent element. Any element 49*a1a3b679SAndreas Boehler * implementing XmlSerializble should only ever write what's considered 50*a1a3b679SAndreas Boehler * its 'inner xml'. 51*a1a3b679SAndreas Boehler * 52*a1a3b679SAndreas Boehler * The parent of the current element is responsible for writing a 53*a1a3b679SAndreas Boehler * containing element. 54*a1a3b679SAndreas Boehler * 55*a1a3b679SAndreas Boehler * This allows serializers to be re-used for different element names. 56*a1a3b679SAndreas Boehler * 57*a1a3b679SAndreas Boehler * If you are opening new elements, you must also close them again. 58*a1a3b679SAndreas Boehler * 59*a1a3b679SAndreas Boehler * @param Writer $writer 60*a1a3b679SAndreas Boehler * @return void 61*a1a3b679SAndreas Boehler */ 62*a1a3b679SAndreas Boehler function xmlSerialize(Writer $writer) { 63*a1a3b679SAndreas Boehler 64*a1a3b679SAndreas Boehler foreach ($this->privileges as $privName) { 65*a1a3b679SAndreas Boehler 66*a1a3b679SAndreas Boehler $writer->startElement('{DAV:}privilege'); 67*a1a3b679SAndreas Boehler $writer->writeElement($privName); 68*a1a3b679SAndreas Boehler $writer->endElement(); 69*a1a3b679SAndreas Boehler 70*a1a3b679SAndreas Boehler } 71*a1a3b679SAndreas Boehler 72*a1a3b679SAndreas Boehler 73*a1a3b679SAndreas Boehler } 74*a1a3b679SAndreas Boehler 75*a1a3b679SAndreas Boehler /** 76*a1a3b679SAndreas Boehler * Returns true or false, whether the specified principal appears in the 77*a1a3b679SAndreas Boehler * list. 78*a1a3b679SAndreas Boehler * 79*a1a3b679SAndreas Boehler * @param string $privilegeName 80*a1a3b679SAndreas Boehler * @return bool 81*a1a3b679SAndreas Boehler */ 82*a1a3b679SAndreas Boehler function has($privilegeName) { 83*a1a3b679SAndreas Boehler 84*a1a3b679SAndreas Boehler return in_array($privilegeName, $this->privileges); 85*a1a3b679SAndreas Boehler 86*a1a3b679SAndreas Boehler } 87*a1a3b679SAndreas Boehler 88*a1a3b679SAndreas Boehler /** 89*a1a3b679SAndreas Boehler * Returns the list of privileges. 90*a1a3b679SAndreas Boehler * 91*a1a3b679SAndreas Boehler * @return array 92*a1a3b679SAndreas Boehler */ 93*a1a3b679SAndreas Boehler function getValue() { 94*a1a3b679SAndreas Boehler 95*a1a3b679SAndreas Boehler return $this->privileges; 96*a1a3b679SAndreas Boehler 97*a1a3b679SAndreas Boehler } 98*a1a3b679SAndreas Boehler 99*a1a3b679SAndreas Boehler /** 100*a1a3b679SAndreas Boehler * The deserialize method is called during xml parsing. 101*a1a3b679SAndreas Boehler * 102*a1a3b679SAndreas Boehler * This method is called statictly, this is because in theory this method 103*a1a3b679SAndreas Boehler * may be used as a type of constructor, or factory method. 104*a1a3b679SAndreas Boehler * 105*a1a3b679SAndreas Boehler * Often you want to return an instance of the current class, but you are 106*a1a3b679SAndreas Boehler * free to return other data as well. 107*a1a3b679SAndreas Boehler * 108*a1a3b679SAndreas Boehler * You are responsible for advancing the reader to the next element. Not 109*a1a3b679SAndreas Boehler * doing anything will result in a never-ending loop. 110*a1a3b679SAndreas Boehler * 111*a1a3b679SAndreas Boehler * If you just want to skip parsing for this element altogether, you can 112*a1a3b679SAndreas Boehler * just call $reader->next(); 113*a1a3b679SAndreas Boehler * 114*a1a3b679SAndreas Boehler * $reader->parseInnerTree() will parse the entire sub-tree, and advance to 115*a1a3b679SAndreas Boehler * the next element. 116*a1a3b679SAndreas Boehler * 117*a1a3b679SAndreas Boehler * @param Reader $reader 118*a1a3b679SAndreas Boehler * @return mixed 119*a1a3b679SAndreas Boehler */ 120*a1a3b679SAndreas Boehler static function xmlDeserialize(Reader $reader) { 121*a1a3b679SAndreas Boehler 122*a1a3b679SAndreas Boehler $result = []; 123*a1a3b679SAndreas Boehler 124*a1a3b679SAndreas Boehler $tree = $reader->parseInnerTree(['{DAV:}privilege' => 'Sabre\\Xml\\Element\\Elements']); 125*a1a3b679SAndreas Boehler foreach ($tree as $element) { 126*a1a3b679SAndreas Boehler if ($element['name'] !== '{DAV:}privilege') { 127*a1a3b679SAndreas Boehler continue; 128*a1a3b679SAndreas Boehler } 129*a1a3b679SAndreas Boehler $result[] = $element['value'][0]; 130*a1a3b679SAndreas Boehler } 131*a1a3b679SAndreas Boehler return new self($result); 132*a1a3b679SAndreas Boehler 133*a1a3b679SAndreas Boehler } 134*a1a3b679SAndreas Boehler 135*a1a3b679SAndreas Boehler /** 136*a1a3b679SAndreas Boehler * Generate html representation for this value. 137*a1a3b679SAndreas Boehler * 138*a1a3b679SAndreas Boehler * The html output is 100% trusted, and no effort is being made to sanitize 139*a1a3b679SAndreas Boehler * it. It's up to the implementor to sanitize user provided values. 140*a1a3b679SAndreas Boehler * 141*a1a3b679SAndreas Boehler * The output must be in UTF-8. 142*a1a3b679SAndreas Boehler * 143*a1a3b679SAndreas Boehler * The baseUri parameter is a url to the root of the application, and can 144*a1a3b679SAndreas Boehler * be used to construct local links. 145*a1a3b679SAndreas Boehler * 146*a1a3b679SAndreas Boehler * @param HtmlOutputHelper $html 147*a1a3b679SAndreas Boehler * @return string 148*a1a3b679SAndreas Boehler */ 149*a1a3b679SAndreas Boehler function toHtml(HtmlOutputHelper $html) { 150*a1a3b679SAndreas Boehler 151*a1a3b679SAndreas Boehler return implode( 152*a1a3b679SAndreas Boehler ', ', 153*a1a3b679SAndreas Boehler array_map([$html, 'xmlName'], $this->getValue()) 154*a1a3b679SAndreas Boehler ); 155*a1a3b679SAndreas Boehler 156*a1a3b679SAndreas Boehler } 157*a1a3b679SAndreas Boehler 158*a1a3b679SAndreas Boehler 159*a1a3b679SAndreas Boehler} 160