permissions.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @copyright Copyright (c) 2015, ownCloud, Inc.
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. use OC\Files\Cache\Cache;
  27. use OC\Files\Storage\Storage;
  28. use OC\Files\View;
  29. class Test_Files_Sharing_Permissions extends OCA\Files_sharing\Tests\TestCase {
  30. /**
  31. * @var Storage
  32. */
  33. private $sharedStorageRestrictedShare;
  34. /**
  35. * @var Storage
  36. */
  37. private $sharedCacheRestrictedShare;
  38. /**
  39. * @var View
  40. */
  41. private $secondView;
  42. /**
  43. * @var Storage
  44. */
  45. private $ownerStorage;
  46. /**
  47. * @var Storage
  48. */
  49. private $sharedStorage;
  50. /**
  51. * @var Cache
  52. */
  53. private $sharedCache;
  54. /**
  55. * @var Cache
  56. */
  57. private $ownerCache;
  58. protected function setUp() {
  59. parent::setUp();
  60. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  61. // prepare user1's dir structure
  62. $textData = "dummy file data\n";
  63. $this->view->mkdir('container');
  64. $this->view->mkdir('container/shareddir');
  65. $this->view->mkdir('container/shareddir/subdir');
  66. $this->view->mkdir('container/shareddirrestricted');
  67. $this->view->mkdir('container/shareddirrestricted/subdir');
  68. $this->view->file_put_contents('container/shareddir/textfile.txt', $textData);
  69. $this->view->file_put_contents('container/shareddirrestricted/textfile1.txt', $textData);
  70. list($this->ownerStorage, $internalPath) = $this->view->resolvePath('');
  71. $this->ownerCache = $this->ownerStorage->getCache();
  72. $this->ownerStorage->getScanner()->scan('');
  73. // share "shareddir" with user2
  74. $fileinfo = $this->view->getFileInfo('container/shareddir');
  75. \OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  76. self::TEST_FILES_SHARING_API_USER2, 31);
  77. $fileinfo2 = $this->view->getFileInfo('container/shareddirrestricted');
  78. \OCP\Share::shareItem('folder', $fileinfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  79. self::TEST_FILES_SHARING_API_USER2, 7);
  80. // login as user2
  81. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  82. // retrieve the shared storage
  83. $this->secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
  84. list($this->sharedStorage, $internalPath) = $this->secondView->resolvePath('files/shareddir');
  85. list($this->sharedStorageRestrictedShare, $internalPath) = $this->secondView->resolvePath('files/shareddirrestricted');
  86. $this->sharedCache = $this->sharedStorage->getCache();
  87. $this->sharedCacheRestrictedShare = $this->sharedStorageRestrictedShare->getCache();
  88. }
  89. protected function tearDown() {
  90. if ($this->sharedCache) {
  91. $this->sharedCache->clear();
  92. }
  93. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  94. $fileinfo = $this->view->getFileInfo('container/shareddir');
  95. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  96. self::TEST_FILES_SHARING_API_USER2);
  97. $fileinfo2 = $this->view->getFileInfo('container/shareddirrestricted');
  98. \OCP\Share::unshare('folder', $fileinfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  99. self::TEST_FILES_SHARING_API_USER2);
  100. $this->view->deleteAll('container');
  101. $this->ownerCache->clear();
  102. parent::tearDown();
  103. }
  104. /**
  105. * Test that the permissions of shared directory are returned correctly
  106. */
  107. function testGetPermissions() {
  108. $sharedDirPerms = $this->sharedStorage->getPermissions('shareddir');
  109. $this->assertEquals(31, $sharedDirPerms);
  110. $sharedDirPerms = $this->sharedStorage->getPermissions('shareddir/textfile.txt');
  111. $this->assertEquals(31, $sharedDirPerms);
  112. $sharedDirRestrictedPerms = $this->sharedStorageRestrictedShare->getPermissions('shareddirrestricted');
  113. $this->assertEquals(7, $sharedDirRestrictedPerms);
  114. $sharedDirRestrictedPerms = $this->sharedStorageRestrictedShare->getPermissions('shareddirrestricted/textfile.txt');
  115. $this->assertEquals(7, $sharedDirRestrictedPerms);
  116. }
  117. /**
  118. * Test that the permissions of shared directory are returned correctly
  119. */
  120. function testGetDirectoryPermissions() {
  121. $contents = $this->secondView->getDirectoryContent('files/shareddir');
  122. $this->assertEquals('subdir', $contents[0]['name']);
  123. $this->assertEquals(31, $contents[0]['permissions']);
  124. $this->assertEquals('textfile.txt', $contents[1]['name']);
  125. // 27 is correct because create is reserved to folders only - requires more unit tests overall to ensure this
  126. $this->assertEquals(27, $contents[1]['permissions']);
  127. $contents = $this->secondView->getDirectoryContent('files/shareddirrestricted');
  128. $this->assertEquals('subdir', $contents[0]['name']);
  129. $this->assertEquals(7, $contents[0]['permissions']);
  130. $this->assertEquals('textfile1.txt', $contents[1]['name']);
  131. // 3 is correct because create is reserved to folders only
  132. $this->assertEquals(3, $contents[1]['permissions']);
  133. }
  134. }