CurrentUserPrivilegeSet.php 3.9 KB

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