Principal.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Principal property
  4. *
  5. * The principal property represents a principal from RFC3744 (ACL).
  6. * The property can be used to specify a principal or pseudo principals.
  7. *
  8. * @package Sabre
  9. * @subpackage DAVACL
  10. * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
  11. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  12. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  13. */
  14. class Sabre_DAVACL_Property_Principal extends Sabre_DAV_Property implements Sabre_DAV_Property_IHref {
  15. /**
  16. * To specify a not-logged-in user, use the UNAUTHENTICTED principal
  17. */
  18. const UNAUTHENTICATED = 1;
  19. /**
  20. * To specify any principal that is logged in, use AUTHENTICATED
  21. */
  22. const AUTHENTICATED = 2;
  23. /**
  24. * Specific principals can be specified with the HREF
  25. */
  26. const HREF = 3;
  27. /**
  28. * Everybody, basically
  29. */
  30. const ALL = 4;
  31. /**
  32. * Principal-type
  33. *
  34. * Must be one of the UNAUTHENTICATED, AUTHENTICATED or HREF constants.
  35. *
  36. * @var int
  37. */
  38. private $type;
  39. /**
  40. * Url to principal
  41. *
  42. * This value is only used for the HREF principal type.
  43. *
  44. * @var string
  45. */
  46. private $href;
  47. /**
  48. * Creates the property.
  49. *
  50. * The 'type' argument must be one of the type constants defined in this class.
  51. *
  52. * 'href' is only required for the HREF type.
  53. *
  54. * @param int $type
  55. * @param string|null $href
  56. */
  57. public function __construct($type, $href = null) {
  58. $this->type = $type;
  59. if ($type===self::HREF && is_null($href)) {
  60. throw new Sabre_DAV_Exception('The href argument must be specified for the HREF principal type.');
  61. }
  62. $this->href = $href;
  63. }
  64. /**
  65. * Returns the principal type
  66. *
  67. * @return int
  68. */
  69. public function getType() {
  70. return $this->type;
  71. }
  72. /**
  73. * Returns the principal uri.
  74. *
  75. * @return string
  76. */
  77. public function getHref() {
  78. return $this->href;
  79. }
  80. /**
  81. * Serializes the property into a DOMElement.
  82. *
  83. * @param Sabre_DAV_Server $server
  84. * @param DOMElement $node
  85. * @return void
  86. */
  87. public function serialize(Sabre_DAV_Server $server, DOMElement $node) {
  88. $prefix = $server->xmlNamespaces['DAV:'];
  89. switch($this->type) {
  90. case self::UNAUTHENTICATED :
  91. $node->appendChild(
  92. $node->ownerDocument->createElement($prefix . ':unauthenticated')
  93. );
  94. break;
  95. case self::AUTHENTICATED :
  96. $node->appendChild(
  97. $node->ownerDocument->createElement($prefix . ':authenticated')
  98. );
  99. break;
  100. case self::HREF :
  101. $href = $node->ownerDocument->createElement($prefix . ':href');
  102. $href->nodeValue = $server->getBaseUri() . $this->href;
  103. $node->appendChild($href);
  104. break;
  105. }
  106. }
  107. /**
  108. * Deserializes a DOM element into a property object.
  109. *
  110. * @param DOMElement $dom
  111. * @return Sabre_DAV_Property_Principal
  112. */
  113. static public function unserialize(DOMElement $dom) {
  114. $parent = $dom->firstChild;
  115. while(!Sabre_DAV_XMLUtil::toClarkNotation($parent)) {
  116. $parent = $parent->nextSibling;
  117. }
  118. switch(Sabre_DAV_XMLUtil::toClarkNotation($parent)) {
  119. case '{DAV:}unauthenticated' :
  120. return new self(self::UNAUTHENTICATED);
  121. case '{DAV:}authenticated' :
  122. return new self(self::AUTHENTICATED);
  123. case '{DAV:}href':
  124. return new self(self::HREF, $parent->textContent);
  125. case '{DAV:}all':
  126. return new self(self::ALL);
  127. default :
  128. throw new Sabre_DAV_Exception_BadRequest('Unexpected element (' . Sabre_DAV_XMLUtil::toClarkNotation($parent) . '). Could not deserialize');
  129. }
  130. }
  131. }