HomeCollection.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAVACL\FS;
  4. use Sabre\DAVACL\AbstractPrincipalCollection;
  5. use Sabre\DAVACL\ACLTrait;
  6. use Sabre\DAVACL\IACL;
  7. use Sabre\DAVACL\PrincipalBackend\BackendInterface;
  8. use Sabre\Uri;
  9. /**
  10. * This collection contains a collection for every principal.
  11. * It is similar to /home on many unix systems.
  12. *
  13. * The per-user collections can only be accessed by the user who owns the
  14. * collection.
  15. *
  16. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  17. * @author Evert Pot (http://evertpot.com/)
  18. * @license http://sabre.io/license/ Modified BSD License
  19. */
  20. class HomeCollection extends AbstractPrincipalCollection implements IACL
  21. {
  22. use ACLTrait;
  23. /**
  24. * Name of this collection.
  25. *
  26. * @var string
  27. */
  28. public $collectionName = 'home';
  29. /**
  30. * Path to where the users' files are actually stored.
  31. *
  32. * @var string
  33. */
  34. protected $storagePath;
  35. /**
  36. * Creates the home collection.
  37. *
  38. * @param string $storagePath where the actual files are stored
  39. * @param string $principalPrefix list of principals to iterate
  40. */
  41. public function __construct(BackendInterface $principalBackend, $storagePath, $principalPrefix = 'principals')
  42. {
  43. parent::__construct($principalBackend, $principalPrefix);
  44. $this->storagePath = $storagePath;
  45. }
  46. /**
  47. * Returns the name of the node.
  48. *
  49. * This is used to generate the url.
  50. *
  51. * @return string
  52. */
  53. public function getName()
  54. {
  55. return $this->collectionName;
  56. }
  57. /**
  58. * Returns a principals' collection of files.
  59. *
  60. * The passed array contains principal information, and is guaranteed to
  61. * at least contain a uri item. Other properties may or may not be
  62. * supplied by the authentication backend.
  63. *
  64. * @return \Sabre\DAV\INode
  65. */
  66. public function getChildForPrincipal(array $principalInfo)
  67. {
  68. $owner = $principalInfo['uri'];
  69. $acl = [
  70. [
  71. 'privilege' => '{DAV:}all',
  72. 'principal' => '{DAV:}owner',
  73. 'protected' => true,
  74. ],
  75. ];
  76. list(, $principalBaseName) = Uri\split($owner);
  77. $path = $this->storagePath.'/'.$principalBaseName;
  78. if (!is_dir($path)) {
  79. mkdir($path, 0777, true);
  80. }
  81. return new Collection(
  82. $path,
  83. $acl,
  84. $owner
  85. );
  86. }
  87. /**
  88. * Returns a list of ACE's for this node.
  89. *
  90. * Each ACE has the following properties:
  91. * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
  92. * currently the only supported privileges
  93. * * 'principal', a url to the principal who owns the node
  94. * * 'protected' (optional), indicating that this ACE is not allowed to
  95. * be updated.
  96. *
  97. * @return array
  98. */
  99. public function getACL()
  100. {
  101. return [
  102. [
  103. 'principal' => '{DAV:}authenticated',
  104. 'privilege' => '{DAV:}read',
  105. 'protected' => true,
  106. ],
  107. ];
  108. }
  109. }