/** * Set the mail address of a user * * @NoAdminRequired * @NoSubadminRequired * * @param string $id * @param string $mailAddress * @return DataResponse */ public function setMailAddress($id, $mailAddress) { $userId = $this->userSession->getUser()->getUID(); $user = $this->userManager->get($id); if ($userId !== $id && !$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) { return new DataResponse(array('status' => 'error', 'data' => array('message' => (string) $this->l10n->t('Forbidden'))), Http::STATUS_FORBIDDEN); } if ($mailAddress !== '' && !$this->mailer->validateMailAddress($mailAddress)) { return new DataResponse(array('status' => 'error', 'data' => array('message' => (string) $this->l10n->t('Invalid mail address'))), Http::STATUS_UNPROCESSABLE_ENTITY); } if (!$user) { return new DataResponse(array('status' => 'error', 'data' => array('message' => (string) $this->l10n->t('Invalid user'))), Http::STATUS_UNPROCESSABLE_ENTITY); } // this is the only permission a backend provides and is also used // for the permission of setting a email address if (!$user->canChangeDisplayName()) { return new DataResponse(array('status' => 'error', 'data' => array('message' => (string) $this->l10n->t('Unable to change mail address'))), Http::STATUS_FORBIDDEN); } // delete user value if email address is empty if ($mailAddress === '') { $this->config->deleteUserValue($id, 'settings', 'email'); } else { $this->config->setUserValue($id, 'settings', 'email', $mailAddress); } return new DataResponse(array('status' => 'success', 'data' => array('username' => $id, 'mailAddress' => $mailAddress, 'message' => (string) $this->l10n->t('Email saved'))), Http::STATUS_OK); }
/** * returns the available groups * @param string $search a search string * @return \OCP\IGroup[] */ protected function getGroups($search = '') { if ($this->isAdmin) { return $this->groupManager->search($search); } else { $userObject = $this->userSession->getUser(); if ($userObject !== null) { $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); } else { $groups = []; } return $groups; } }
/** * Set the displayName of a user * * @NoAdminRequired * @NoSubadminRequired * * @param string $username * @param string $displayName * @return DataResponse */ public function setDisplayName($username, $displayName) { $currentUser = $this->userSession->getUser(); if ($username === null) { $username = $currentUser->getUID(); } $user = $this->userManager->get($username); if ($user === null || !$user->canChangeDisplayName() || !$this->groupManager->isAdmin($currentUser->getUID()) && !$this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $user) && $currentUser !== $user) { return new DataResponse(['status' => 'error', 'data' => ['message' => $this->l10n->t('Authentication error')]]); } if ($user->setDisplayName($displayName)) { return new DataResponse(['status' => 'success', 'data' => ['message' => $this->l10n->t('Your full name has been changed.'), 'username' => $username, 'displayName' => $displayName]]); } else { return new DataResponse(['status' => 'error', 'data' => ['message' => $this->l10n->t('Unable to change full name'), 'displayName' => $user->getDisplayName()]]); } }
/** * @param array $parameters * @return OC_OCS_Result */ public function getSubAdminsOfGroup($parameters) { $group = $parameters['groupid']; // Check group exists $targetGroup = $this->groupManager->get($group); if ($targetGroup === null) { return new OC_OCS_Result(null, 101, 'Group does not exist'); } $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup); // New class returns IUser[] so convert back $uids = []; foreach ($subadmins as $user) { $uids[] = $user->getUID(); } return new OC_OCS_Result($uids); }
/** * Get the groups a user is a subadmin of * * @param array $parameters * @return OC_OCS_Result */ public function getUserSubAdminGroups($parameters) { $user = $this->userManager->get($parameters['userid']); // Check if the user exists if ($user === null) { return new OC_OCS_Result(null, 101, 'User does not exist'); } // Get the subadmin groups $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user); foreach ($groups as $key => $group) { $groups[$key] = $group->getGID(); } if (!$groups) { return new OC_OCS_Result(null, 102, 'Unknown error occurred'); } else { return new OC_OCS_Result($groups); } }
/** * Count all unique users visible for the current admin/subadmin. * * @NoAdminRequired * * @return DataResponse */ public function stats() { $userCount = 0; if ($this->isAdmin) { $countByBackend = $this->userManager->countUsers(); if (!empty($countByBackend)) { foreach ($countByBackend as $count) { $userCount += $count; } } } else { $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser()); $uniqueUsers = []; foreach ($groups as $group) { foreach ($group->getUsers() as $uid => $displayName) { $uniqueUsers[$uid] = true; } } $userCount = count($uniqueUsers); } return new DataResponse(['totalUsers' => $userCount]); }
public function testGetSubAdminsOfGroup() { $user1 = $this->generateUsers(); $user2 = $this->generateUsers(); $this->userSession->setUser($user1); $this->groupManager->get('admin')->addUser($user1); $group1 = $this->groupManager->createGroup($this->getUniqueID()); $this->groupManager->getSubAdmin()->createSubAdmin($user2, $group1); $result = $this->api->getSubAdminsOfGroup(['groupid' => $group1->getGID()]); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); $data = $result->getData(); $this->assertEquals($user2->getUID(), reset($data)); $group1->delete(); $user1 = $this->generateUsers(); $this->userSession->setUser($user1); $this->groupManager->get('admin')->addUser($user1); $result = $this->api->getSubAdminsOfGroup(['groupid' => $this->getUniqueID()]); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); $this->assertEquals(101, $result->getStatusCode()); }