Acl.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAVACL\Xml\Property;
  4. use Sabre\DAV;
  5. use Sabre\DAV\Browser\HtmlOutput;
  6. use Sabre\DAV\Browser\HtmlOutputHelper;
  7. use Sabre\Xml\Element;
  8. use Sabre\Xml\Reader;
  9. use Sabre\Xml\Writer;
  10. /**
  11. * This class represents the {DAV:}acl property.
  12. *
  13. * The {DAV:}acl property is a full list of access control entries for a
  14. * resource.
  15. *
  16. * {DAV:}acl is used as a WebDAV property, but it is also used within the body
  17. * of the ACL request.
  18. *
  19. * See:
  20. * http://tools.ietf.org/html/rfc3744#section-5.5
  21. *
  22. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  23. * @author Evert Pot (http://evertpot.com/)
  24. * @license http://sabre.io/license/ Modified BSD License
  25. */
  26. class Acl implements Element, HtmlOutput
  27. {
  28. /**
  29. * List of privileges.
  30. *
  31. * @var array
  32. */
  33. protected $privileges;
  34. /**
  35. * Whether or not the server base url is required to be prefixed when
  36. * serializing the property.
  37. *
  38. * @var bool
  39. */
  40. protected $prefixBaseUrl;
  41. /**
  42. * Constructor.
  43. *
  44. * This object requires a structure similar to the return value from
  45. * Sabre\DAVACL\Plugin::getACL().
  46. *
  47. * Each privilege is a an array with at least a 'privilege' property, and a
  48. * 'principal' property. A privilege may have a 'protected' property as
  49. * well.
  50. *
  51. * The prefixBaseUrl should be set to false, if the supplied principal urls
  52. * are already full urls. If this is kept to true, the servers base url
  53. * will automatically be prefixed.
  54. *
  55. * @param bool $prefixBaseUrl
  56. */
  57. public function __construct(array $privileges, $prefixBaseUrl = true)
  58. {
  59. $this->privileges = $privileges;
  60. $this->prefixBaseUrl = $prefixBaseUrl;
  61. }
  62. /**
  63. * Returns the list of privileges for this property.
  64. *
  65. * @return array
  66. */
  67. public function getPrivileges()
  68. {
  69. return $this->privileges;
  70. }
  71. /**
  72. * The xmlSerialize method is called during xml writing.
  73. *
  74. * Use the $writer argument to write its own xml serialization.
  75. *
  76. * An important note: do _not_ create a parent element. Any element
  77. * implementing XmlSerializable should only ever write what's considered
  78. * its 'inner xml'.
  79. *
  80. * The parent of the current element is responsible for writing a
  81. * containing element.
  82. *
  83. * This allows serializers to be re-used for different element names.
  84. *
  85. * If you are opening new elements, you must also close them again.
  86. */
  87. public function xmlSerialize(Writer $writer)
  88. {
  89. foreach ($this->privileges as $ace) {
  90. $this->serializeAce($writer, $ace);
  91. }
  92. }
  93. /**
  94. * Generate html representation for this value.
  95. *
  96. * The html output is 100% trusted, and no effort is being made to sanitize
  97. * it. It's up to the implementor to sanitize user provided values.
  98. *
  99. * The output must be in UTF-8.
  100. *
  101. * The baseUri parameter is a url to the root of the application, and can
  102. * be used to construct local links.
  103. *
  104. * @return string
  105. */
  106. public function toHtml(HtmlOutputHelper $html)
  107. {
  108. ob_start();
  109. echo '<table>';
  110. echo '<tr><th>Principal</th><th>Privilege</th><th></th></tr>';
  111. foreach ($this->privileges as $privilege) {
  112. echo '<tr>';
  113. // if it starts with a {, it's a special principal
  114. if ('{' === $privilege['principal'][0]) {
  115. echo '<td>', $html->xmlName($privilege['principal']), '</td>';
  116. } else {
  117. echo '<td>', $html->link($privilege['principal']), '</td>';
  118. }
  119. echo '<td>', $html->xmlName($privilege['privilege']), '</td>';
  120. echo '<td>';
  121. if (!empty($privilege['protected'])) {
  122. echo '(protected)';
  123. }
  124. echo '</td>';
  125. echo '</tr>';
  126. }
  127. echo '</table>';
  128. return ob_get_clean();
  129. }
  130. /**
  131. * The deserialize method is called during xml parsing.
  132. *
  133. * This method is called statically, this is because in theory this method
  134. * may be used as a type of constructor, or factory method.
  135. *
  136. * Often you want to return an instance of the current class, but you are
  137. * free to return other data as well.
  138. *
  139. * Important note 2: You are responsible for advancing the reader to the
  140. * next element. Not doing anything will result in a never-ending loop.
  141. *
  142. * If you just want to skip parsing for this element altogether, you can
  143. * just call $reader->next();
  144. *
  145. * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
  146. * the next element.
  147. *
  148. * @return mixed
  149. */
  150. public static function xmlDeserialize(Reader $reader)
  151. {
  152. $elementMap = [
  153. '{DAV:}ace' => 'Sabre\Xml\Element\KeyValue',
  154. '{DAV:}privilege' => 'Sabre\Xml\Element\Elements',
  155. '{DAV:}principal' => 'Sabre\DAVACL\Xml\Property\Principal',
  156. ];
  157. $privileges = [];
  158. foreach ((array) $reader->parseInnerTree($elementMap) as $element) {
  159. if ('{DAV:}ace' !== $element['name']) {
  160. continue;
  161. }
  162. $ace = $element['value'];
  163. if (empty($ace['{DAV:}principal'])) {
  164. throw new DAV\Exception\BadRequest('Each {DAV:}ace element must have one {DAV:}principal element');
  165. }
  166. $principal = $ace['{DAV:}principal'];
  167. switch ($principal->getType()) {
  168. case Principal::HREF:
  169. $principal = $principal->getHref();
  170. break;
  171. case Principal::AUTHENTICATED:
  172. $principal = '{DAV:}authenticated';
  173. break;
  174. case Principal::UNAUTHENTICATED:
  175. $principal = '{DAV:}unauthenticated';
  176. break;
  177. case Principal::ALL:
  178. $principal = '{DAV:}all';
  179. break;
  180. }
  181. $protected = array_key_exists('{DAV:}protected', $ace);
  182. if (!isset($ace['{DAV:}grant'])) {
  183. throw new DAV\Exception\NotImplemented('Every {DAV:}ace element must have a {DAV:}grant element. {DAV:}deny is not yet supported');
  184. }
  185. foreach ($ace['{DAV:}grant'] as $elem) {
  186. if ('{DAV:}privilege' !== $elem['name']) {
  187. continue;
  188. }
  189. foreach ($elem['value'] as $priv) {
  190. $privileges[] = [
  191. 'principal' => $principal,
  192. 'protected' => $protected,
  193. 'privilege' => $priv,
  194. ];
  195. }
  196. }
  197. }
  198. return new self($privileges);
  199. }
  200. /**
  201. * Serializes a single access control entry.
  202. */
  203. private function serializeAce(Writer $writer, array $ace)
  204. {
  205. $writer->startElement('{DAV:}ace');
  206. switch ($ace['principal']) {
  207. case '{DAV:}authenticated':
  208. $principal = new Principal(Principal::AUTHENTICATED);
  209. break;
  210. case '{DAV:}unauthenticated':
  211. $principal = new Principal(Principal::UNAUTHENTICATED);
  212. break;
  213. case '{DAV:}all':
  214. $principal = new Principal(Principal::ALL);
  215. break;
  216. default:
  217. $principal = new Principal(Principal::HREF, $ace['principal']);
  218. break;
  219. }
  220. $writer->writeElement('{DAV:}principal', $principal);
  221. $writer->startElement('{DAV:}grant');
  222. $writer->startElement('{DAV:}privilege');
  223. $writer->writeElement($ace['privilege']);
  224. $writer->endElement(); // privilege
  225. $writer->endElement(); // grant
  226. if (!empty($ace['protected'])) {
  227. $writer->writeElement('{DAV:}protected');
  228. }
  229. $writer->endElement(); // ace
  230. }
  231. }