SubAdminTest.php 10 KB

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