1
0

SubAdminTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. parent::setUp();
  43. $this->users = [];
  44. $this->groups = [];
  45. $this->userManager = \OC::$server->getUserManager();
  46. $this->groupManager = \OC::$server->getGroupManager();
  47. $this->dbConn = \OC::$server->getDatabaseConnection();
  48. $this->eventDispatcher = \OC::$server->get(IEventDispatcher::class);
  49. // Create 3 users and 3 groups
  50. for ($i = 0; $i < 3; $i++) {
  51. $this->users[] = $this->userManager->createUser('user'.$i, 'user');
  52. $this->groups[] = $this->groupManager->createGroup('group'.$i);
  53. }
  54. // Create admin group
  55. if (!$this->groupManager->groupExists('admin')) {
  56. $this->groupManager->createGroup('admin');
  57. }
  58. // Create "orphaned" users and groups (scenario: temporarily disabled
  59. // backend)
  60. $qb = $this->dbConn->getQueryBuilder();
  61. $qb->insert('group_admin')
  62. ->values([
  63. 'gid' => $qb->createNamedParameter($this->groups[0]->getGID()),
  64. 'uid' => $qb->createNamedParameter('orphanedUser')
  65. ])
  66. ->execute();
  67. $qb->insert('group_admin')
  68. ->values([
  69. 'gid' => $qb->createNamedParameter('orphanedGroup'),
  70. 'uid' => $qb->createNamedParameter('orphanedUser')
  71. ])
  72. ->execute();
  73. $qb->insert('group_admin')
  74. ->values([
  75. 'gid' => $qb->createNamedParameter('orphanedGroup'),
  76. 'uid' => $qb->createNamedParameter($this->users[0]->getUID())
  77. ])
  78. ->execute();
  79. }
  80. protected function tearDown(): void {
  81. foreach ($this->users as $user) {
  82. $user->delete();
  83. }
  84. foreach ($this->groups as $group) {
  85. $group->delete();
  86. }
  87. $qb = $this->dbConn->getQueryBuilder();
  88. $qb->delete('group_admin')
  89. ->where($qb->expr()->eq('uid', $qb->createNamedParameter('orphanedUser')))
  90. ->orWhere($qb->expr()->eq('gid', $qb->createNamedParameter('orphanedGroup')))
  91. ->execute();
  92. }
  93. public function testCreateSubAdmin() {
  94. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  95. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  96. // Look for subadmin in the database
  97. $qb = $this->dbConn->getQueryBuilder();
  98. $result = $qb->select(['gid', 'uid'])
  99. ->from('group_admin')
  100. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID())))
  101. ->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID())))
  102. ->execute()
  103. ->fetch();
  104. $this->assertEquals(
  105. [
  106. 'gid' => $this->groups[0]->getGID(),
  107. 'uid' => $this->users[0]->getUID()
  108. ], $result);
  109. // Delete subadmin
  110. $result = $qb->delete('*PREFIX*group_admin')
  111. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID())))
  112. ->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID())))
  113. ->execute();
  114. }
  115. public function testDeleteSubAdmin() {
  116. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  117. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  118. $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
  119. // DB query should be empty
  120. $qb = $this->dbConn->getQueryBuilder();
  121. $result = $qb->select(['gid', 'uid'])
  122. ->from('group_admin')
  123. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID())))
  124. ->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID())))
  125. ->execute()
  126. ->fetch();
  127. $this->assertEmpty($result);
  128. }
  129. public function testGetSubAdminsGroups() {
  130. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  131. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  132. $subAdmin->createSubAdmin($this->users[0], $this->groups[1]);
  133. $result = $subAdmin->getSubAdminsGroups($this->users[0]);
  134. $this->assertContains($this->groups[0], $result);
  135. $this->assertContains($this->groups[1], $result);
  136. $this->assertNotContains($this->groups[2], $result);
  137. $this->assertNotContains(null, $result);
  138. $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
  139. $subAdmin->deleteSubAdmin($this->users[0], $this->groups[1]);
  140. }
  141. public function testGetGroupsSubAdmins() {
  142. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  143. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  144. $subAdmin->createSubAdmin($this->users[1], $this->groups[0]);
  145. $result = $subAdmin->getGroupsSubAdmins($this->groups[0]);
  146. $this->assertContains($this->users[0], $result);
  147. $this->assertContains($this->users[1], $result);
  148. $this->assertNotContains($this->users[2], $result);
  149. $this->assertNotContains(null, $result);
  150. $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
  151. $subAdmin->deleteSubAdmin($this->users[1], $this->groups[0]);
  152. }
  153. public function testGetAllSubAdmin() {
  154. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  155. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  156. $subAdmin->createSubAdmin($this->users[1], $this->groups[1]);
  157. $subAdmin->createSubAdmin($this->users[2], $this->groups[1]);
  158. $result = $subAdmin->getAllSubAdmins();
  159. $this->assertContains(['user' => $this->users[0], 'group' => $this->groups[0]], $result);
  160. $this->assertContains(['user' => $this->users[1], 'group' => $this->groups[1]], $result);
  161. $this->assertContains(['user' => $this->users[2], 'group' => $this->groups[1]], $result);
  162. $this->assertNotContains(['user' => null, 'group' => null], $result);
  163. }
  164. public function testIsSubAdminofGroup() {
  165. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  166. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  167. $this->assertTrue($subAdmin->isSubAdminOfGroup($this->users[0], $this->groups[0]));
  168. $this->assertFalse($subAdmin->isSubAdminOfGroup($this->users[0], $this->groups[1]));
  169. $this->assertFalse($subAdmin->isSubAdminOfGroup($this->users[1], $this->groups[0]));
  170. $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
  171. }
  172. public function testIsSubAdmin() {
  173. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  174. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  175. $this->assertTrue($subAdmin->isSubAdmin($this->users[0]));
  176. $this->assertFalse($subAdmin->isSubAdmin($this->users[1]));
  177. $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
  178. }
  179. public function testIsSubAdminAsAdmin() {
  180. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  181. $this->groupManager->get('admin')->addUser($this->users[0]);
  182. $this->assertTrue($subAdmin->isSubAdmin($this->users[0]));
  183. }
  184. public function testIsUserAccessible() {
  185. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  186. $this->groups[0]->addUser($this->users[1]);
  187. $this->groups[1]->addUser($this->users[1]);
  188. $this->groups[1]->addUser($this->users[2]);
  189. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  190. $subAdmin->createSubAdmin($this->users[2], $this->groups[2]);
  191. $this->assertTrue($subAdmin->isUserAccessible($this->users[0], $this->users[1]));
  192. $this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[2]));
  193. $this->assertFalse($subAdmin->isUserAccessible($this->users[2], $this->users[0]));
  194. $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
  195. $subAdmin->deleteSubAdmin($this->users[2], $this->groups[2]);
  196. }
  197. public function testIsUserAccessibleAsUser() {
  198. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  199. $this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[1]));
  200. }
  201. public function testIsUserAccessibleAdmin() {
  202. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  203. $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
  204. $this->groupManager->get('admin')->addUser($this->users[1]);
  205. $this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[1]));
  206. }
  207. public function testPostDeleteUser() {
  208. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  209. $user = array_shift($this->users);
  210. foreach ($this->groups as $group) {
  211. $subAdmin->createSubAdmin($user, $group);
  212. }
  213. $user->delete();
  214. $this->assertEmpty($subAdmin->getAllSubAdmins());
  215. }
  216. public function testPostDeleteGroup() {
  217. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  218. $group = array_shift($this->groups);
  219. foreach ($this->users as $user) {
  220. $subAdmin->createSubAdmin($user, $group);
  221. }
  222. $group->delete();
  223. $this->assertEmpty($subAdmin->getAllSubAdmins());
  224. }
  225. public function testHooks() {
  226. $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
  227. $test = $this;
  228. $u = $this->users[0];
  229. $g = $this->groups[0];
  230. $count = 0;
  231. $this->eventDispatcher->addListener(SubAdminAddedEvent::class, function (SubAdminAddedEvent $event) use ($test, $u, $g, &$count) {
  232. $test->assertEquals($u->getUID(), $event->getUser()->getUID());
  233. $test->assertEquals($g->getGID(), $event->getGroup()->getGID());
  234. $count++;
  235. });
  236. $this->eventDispatcher->addListener(SubAdminRemovedEvent::class, function ($event) use ($test, $u, $g, &$count) {
  237. $test->assertEquals($u->getUID(), $event->getUser()->getUID());
  238. $test->assertEquals($g->getGID(), $event->getGroup()->getGID());
  239. $count++;
  240. });
  241. $subAdmin->createSubAdmin($u, $g);
  242. $this->assertEquals(1, $count);
  243. $subAdmin->deleteSubAdmin($u, $g);
  244. $this->assertEquals(2, $count);
  245. }
  246. }