Principal.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAVACL\Xml\Property;
  4. use Sabre\DAV;
  5. use Sabre\DAV\Browser\HtmlOutputHelper;
  6. use Sabre\DAV\Exception\BadRequest;
  7. use Sabre\Xml\Reader;
  8. use Sabre\Xml\Writer;
  9. /**
  10. * Principal property.
  11. *
  12. * The principal property represents a principal from RFC3744 (ACL).
  13. * The property can be used to specify a principal or pseudo principals.
  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 Principal extends DAV\Xml\Property\Href
  20. {
  21. /**
  22. * To specify a not-logged-in user, use the UNAUTHENTICATED principal.
  23. */
  24. const UNAUTHENTICATED = 1;
  25. /**
  26. * To specify any principal that is logged in, use AUTHENTICATED.
  27. */
  28. const AUTHENTICATED = 2;
  29. /**
  30. * Specific principals can be specified with the HREF.
  31. */
  32. const HREF = 3;
  33. /**
  34. * Everybody, basically.
  35. */
  36. const ALL = 4;
  37. /**
  38. * Principal-type.
  39. *
  40. * Must be one of the UNAUTHENTICATED, AUTHENTICATED or HREF constants.
  41. *
  42. * @var int
  43. */
  44. protected $type;
  45. /**
  46. * Creates the property.
  47. *
  48. * The 'type' argument must be one of the type constants defined in this class.
  49. *
  50. * 'href' is only required for the HREF type.
  51. *
  52. * @param int $type
  53. * @param string|null $href
  54. */
  55. public function __construct($type, $href = null)
  56. {
  57. $this->type = $type;
  58. if (self::HREF === $type && is_null($href)) {
  59. throw new DAV\Exception('The href argument must be specified for the HREF principal type.');
  60. }
  61. if ($href) {
  62. $href = rtrim($href, '/').'/';
  63. parent::__construct($href);
  64. }
  65. }
  66. /**
  67. * Returns the principal type.
  68. *
  69. * @return int
  70. */
  71. public function getType()
  72. {
  73. return $this->type;
  74. }
  75. /**
  76. * The xmlSerialize method is called during xml writing.
  77. *
  78. * Use the $writer argument to write its own xml serialization.
  79. *
  80. * An important note: do _not_ create a parent element. Any element
  81. * implementing XmlSerializable should only ever write what's considered
  82. * its 'inner xml'.
  83. *
  84. * The parent of the current element is responsible for writing a
  85. * containing element.
  86. *
  87. * This allows serializers to be re-used for different element names.
  88. *
  89. * If you are opening new elements, you must also close them again.
  90. */
  91. public function xmlSerialize(Writer $writer)
  92. {
  93. switch ($this->type) {
  94. case self::UNAUTHENTICATED:
  95. $writer->writeElement('{DAV:}unauthenticated');
  96. break;
  97. case self::AUTHENTICATED:
  98. $writer->writeElement('{DAV:}authenticated');
  99. break;
  100. case self::HREF:
  101. parent::xmlSerialize($writer);
  102. break;
  103. case self::ALL:
  104. $writer->writeElement('{DAV:}all');
  105. break;
  106. }
  107. }
  108. /**
  109. * Generate html representation for this value.
  110. *
  111. * The html output is 100% trusted, and no effort is being made to sanitize
  112. * it. It's up to the implementor to sanitize user provided values.
  113. *
  114. * The output must be in UTF-8.
  115. *
  116. * The baseUri parameter is a url to the root of the application, and can
  117. * be used to construct local links.
  118. *
  119. * @return string
  120. */
  121. public function toHtml(HtmlOutputHelper $html)
  122. {
  123. switch ($this->type) {
  124. case self::UNAUTHENTICATED:
  125. return '<em>unauthenticated</em>';
  126. case self::AUTHENTICATED:
  127. return '<em>authenticated</em>';
  128. case self::HREF:
  129. return parent::toHtml($html);
  130. case self::ALL:
  131. return '<em>all</em>';
  132. }
  133. return '<em>unknown</em>';
  134. }
  135. /**
  136. * The deserialize method is called during xml parsing.
  137. *
  138. * This method is called staticly, this is because in theory this method
  139. * may be used as a type of constructor, or factory method.
  140. *
  141. * Often you want to return an instance of the current class, but you are
  142. * free to return other data as well.
  143. *
  144. * Important note 2: You are responsible for advancing the reader to the
  145. * next element. Not doing anything will result in a never-ending loop.
  146. *
  147. * If you just want to skip parsing for this element altogether, you can
  148. * just call $reader->next();
  149. *
  150. * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
  151. * the next element.
  152. *
  153. * @return mixed
  154. */
  155. public static function xmlDeserialize(Reader $reader)
  156. {
  157. $tree = $reader->parseInnerTree()[0];
  158. switch ($tree['name']) {
  159. case '{DAV:}unauthenticated':
  160. return new self(self::UNAUTHENTICATED);
  161. case '{DAV:}authenticated':
  162. return new self(self::AUTHENTICATED);
  163. case '{DAV:}href':
  164. return new self(self::HREF, $tree['value']);
  165. case '{DAV:}all':
  166. return new self(self::ALL);
  167. default:
  168. throw new BadRequest('Unknown or unsupported principal type: '.$tree['name']);
  169. }
  170. }
  171. }