SubAdminTest.php 11 KB

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