SubAdminTest.php 11 KB

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