SubAdminTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test;
  8. use OCP\EventDispatcher\IEventDispatcher;
  9. use OCP\Group\Events\SubAdminAddedEvent;
  10. use OCP\Group\Events\SubAdminRemovedEvent;
  11. /**
  12. * @group DB
  13. */
  14. class SubAdminTest extends \Test\TestCase {
  15. /** @var \OCP\IUserManager */
  16. private $userManager;
  17. /** @var \OCP\IGroupManager */
  18. private $groupManager;
  19. /** @var \OCP\IDBConnection */
  20. private $dbConn;
  21. /** @var IEventDispatcher */
  22. private $eventDispatcher;
  23. /** @var \OCP\IUser[] */
  24. private $users;
  25. /** @var \OCP\IGroup[] */
  26. private $groups;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->users = [];
  30. $this->groups = [];
  31. $this->userManager = \OC::$server->getUserManager();
  32. $this->groupManager = \OC::$server->getGroupManager();
  33. $this->dbConn = \OC::$server->getDatabaseConnection();
  34. $this->eventDispatcher = \OC::$server->get(IEventDispatcher::class);
  35. // Create 3 users and 3 groups
  36. for ($i = 0; $i < 3; $i++) {
  37. $this->users[] = $this->userManager->createUser('user'.$i, 'user');
  38. $this->groups[] = $this->groupManager->createGroup('group'.$i);
  39. }
  40. // Create admin group
  41. if (!$this->groupManager->groupExists('admin')) {
  42. $this->groupManager->createGroup('admin');
  43. }
  44. // Create "orphaned" users and groups (scenario: temporarily disabled
  45. // backend)
  46. $qb = $this->dbConn->getQueryBuilder();
  47. $qb->insert('group_admin')
  48. ->values([
  49. 'gid' => $qb->createNamedParameter($this->groups[0]->getGID()),
  50. 'uid' => $qb->createNamedParameter('orphanedUser')
  51. ])
  52. ->execute();
  53. $qb->insert('group_admin')
  54. ->values([
  55. 'gid' => $qb->createNamedParameter('orphanedGroup'),
  56. 'uid' => $qb->createNamedParameter('orphanedUser')
  57. ])
  58. ->execute();
  59. $qb->insert('group_admin')
  60. ->values([
  61. 'gid' => $qb->createNamedParameter('orphanedGroup'),
  62. 'uid' => $qb->createNamedParameter($this->users[0]->getUID())
  63. ])
  64. ->execute();
  65. }
  66. protected function tearDown(): void {
  67. foreach ($this->users as $user) {
  68. $user->delete();
  69. }
  70. foreach ($this->groups as $group) {
  71. $group->delete();
  72. }
  73. $qb = $this->dbConn->getQueryBuilder();
  74. $qb->delete('group_admin')
  75. ->where($qb->expr()->eq('uid', $qb->createNamedParameter('orphanedUser')))
  76. ->orWhere($qb->expr()->eq('gid', $qb->createNamedParameter('orphanedGroup')))
  77. ->execute();
  78. }
  79. public function testCreateSubAdmin(): void {
  80. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  81. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  82. // Look for subadmin in the database
  83. $qb = $this->dbConn->getQueryBuilder();
  84. $result = $qb->select(['gid', 'uid'])
  85. ->from('group_admin')
  86. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID())))
  87. ->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID())))
  88. ->execute()
  89. ->fetch();
  90. $this->assertEquals(
  91. [
  92. 'gid' => $this->groups[0]->getGID(),
  93. 'uid' => $this->users[0]->getUID()
  94. ], $result);
  95. // Delete subadmin
  96. $result = $qb->delete('*PREFIX*group_admin')
  97. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID())))
  98. ->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID())))
  99. ->execute();
  100. }
  101. public function testDeleteSubAdmin(): void {
  102. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  103. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  104. $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
  105. // DB query should be empty
  106. $qb = $this->dbConn->getQueryBuilder();
  107. $result = $qb->select(['gid', 'uid'])
  108. ->from('group_admin')
  109. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID())))
  110. ->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID())))
  111. ->execute()
  112. ->fetch();
  113. $this->assertEmpty($result);
  114. }
  115. public function testGetSubAdminsGroups(): void {
  116. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  117. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  118. $subAdmin->createSubAdmin($this->users[0], $this->groups[1]);
  119. $result = $subAdmin->getSubAdminsGroups($this->users[0]);
  120. $this->assertContains($this->groups[0], $result);
  121. $this->assertContains($this->groups[1], $result);
  122. $this->assertNotContains($this->groups[2], $result);
  123. $this->assertNotContains(null, $result);
  124. $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
  125. $subAdmin->deleteSubAdmin($this->users[0], $this->groups[1]);
  126. }
  127. public function testGetGroupsSubAdmins(): void {
  128. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  129. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  130. $subAdmin->createSubAdmin($this->users[1], $this->groups[0]);
  131. $result = $subAdmin->getGroupsSubAdmins($this->groups[0]);
  132. $this->assertContains($this->users[0], $result);
  133. $this->assertContains($this->users[1], $result);
  134. $this->assertNotContains($this->users[2], $result);
  135. $this->assertNotContains(null, $result);
  136. $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
  137. $subAdmin->deleteSubAdmin($this->users[1], $this->groups[0]);
  138. }
  139. public function testGetAllSubAdmin(): void {
  140. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  141. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  142. $subAdmin->createSubAdmin($this->users[1], $this->groups[1]);
  143. $subAdmin->createSubAdmin($this->users[2], $this->groups[1]);
  144. $result = $subAdmin->getAllSubAdmins();
  145. $this->assertContains(['user' => $this->users[0], 'group' => $this->groups[0]], $result);
  146. $this->assertContains(['user' => $this->users[1], 'group' => $this->groups[1]], $result);
  147. $this->assertContains(['user' => $this->users[2], 'group' => $this->groups[1]], $result);
  148. $this->assertNotContains(['user' => null, 'group' => null], $result);
  149. }
  150. public function testIsSubAdminofGroup(): void {
  151. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  152. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  153. $this->assertTrue($subAdmin->isSubAdminOfGroup($this->users[0], $this->groups[0]));
  154. $this->assertFalse($subAdmin->isSubAdminOfGroup($this->users[0], $this->groups[1]));
  155. $this->assertFalse($subAdmin->isSubAdminOfGroup($this->users[1], $this->groups[0]));
  156. $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
  157. }
  158. public function testIsSubAdmin(): void {
  159. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  160. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  161. $this->assertTrue($subAdmin->isSubAdmin($this->users[0]));
  162. $this->assertFalse($subAdmin->isSubAdmin($this->users[1]));
  163. $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
  164. }
  165. public function testIsSubAdminAsAdmin(): void {
  166. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  167. $this->groupManager->get('admin')->addUser($this->users[0]);
  168. $this->assertTrue($subAdmin->isSubAdmin($this->users[0]));
  169. }
  170. public function testIsUserAccessible(): void {
  171. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  172. $this->groups[0]->addUser($this->users[1]);
  173. $this->groups[1]->addUser($this->users[1]);
  174. $this->groups[1]->addUser($this->users[2]);
  175. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  176. $subAdmin->createSubAdmin($this->users[2], $this->groups[2]);
  177. $this->assertTrue($subAdmin->isUserAccessible($this->users[0], $this->users[1]));
  178. $this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[2]));
  179. $this->assertFalse($subAdmin->isUserAccessible($this->users[2], $this->users[0]));
  180. $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
  181. $subAdmin->deleteSubAdmin($this->users[2], $this->groups[2]);
  182. }
  183. public function testIsUserAccessibleAsUser(): void {
  184. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  185. $this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[1]));
  186. }
  187. public function testIsUserAccessibleAdmin(): void {
  188. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  189. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  190. $this->groupManager->get('admin')->addUser($this->users[1]);
  191. $this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[1]));
  192. }
  193. public function testPostDeleteUser(): void {
  194. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  195. $user = array_shift($this->users);
  196. foreach ($this->groups as $group) {
  197. $subAdmin->createSubAdmin($user, $group);
  198. }
  199. $user->delete();
  200. $this->assertEmpty($subAdmin->getAllSubAdmins());
  201. }
  202. public function testPostDeleteGroup(): void {
  203. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  204. $group = array_shift($this->groups);
  205. foreach ($this->users as $user) {
  206. $subAdmin->createSubAdmin($user, $group);
  207. }
  208. $group->delete();
  209. $this->assertEmpty($subAdmin->getAllSubAdmins());
  210. }
  211. public function testHooks(): void {
  212. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  213. $test = $this;
  214. $u = $this->users[0];
  215. $g = $this->groups[0];
  216. $count = 0;
  217. $this->eventDispatcher->addListener(SubAdminAddedEvent::class, function (SubAdminAddedEvent $event) use ($test, $u, $g, &$count) {
  218. $test->assertEquals($u->getUID(), $event->getUser()->getUID());
  219. $test->assertEquals($g->getGID(), $event->getGroup()->getGID());
  220. $count++;
  221. });
  222. $this->eventDispatcher->addListener(SubAdminRemovedEvent::class, function ($event) use ($test, $u, $g, &$count) {
  223. $test->assertEquals($u->getUID(), $event->getUser()->getUID());
  224. $test->assertEquals($g->getGID(), $event->getGroup()->getGID());
  225. $count++;
  226. });
  227. $subAdmin->createSubAdmin($u, $g);
  228. $this->assertEquals(1, $count);
  229. $subAdmin->deleteSubAdmin($u, $g);
  230. $this->assertEquals(2, $count);
  231. }
  232. }