protected function setUp() { parent::setUp(); $this->userManager = \OC::$server->getUserManager(); $this->groupManager = \OC::$server->getGroupManager(); $this->groupManager->createGroup('admin'); }
/** * @param string $id * @return DataResponse */ public function create($id) { if ($this->groupManager->groupExists($id)) { return new DataResponse(array('message' => (string) $this->l10n->t('Group already exists.')), Http::STATUS_CONFLICT); } if ($this->groupManager->createGroup($id)) { return new DataResponse(array('groupname' => $id), Http::STATUS_CREATED); } return new DataResponse(array('status' => 'error', 'data' => array('message' => (string) $this->l10n->t('Unable to add group.'))), Http::STATUS_FORBIDDEN); }
protected function execute(InputInterface $input, OutputInterface $output) { $uid = $input->getArgument('uid'); if ($this->userManager->userExists($uid)) { $output->writeln('<error>The user "' . $uid . '" already exists.</error>'); return 1; } if ($input->getOption('password-from-env')) { $password = getenv('OC_PASS'); if (!$password) { $output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>'); return 1; } } elseif ($input->isInteractive()) { /** @var $dialog \Symfony\Component\Console\Helper\DialogHelper */ $dialog = $this->getHelperSet()->get('dialog'); $password = $dialog->askHiddenResponse($output, '<question>Enter password: </question>', false); $confirm = $dialog->askHiddenResponse($output, '<question>Confirm password: </question>', false); if ($password !== $confirm) { $output->writeln("<error>Passwords did not match!</error>"); return 1; } } else { $output->writeln("<error>Interactive input or --password-from-env is needed for entering a password!</error>"); return 1; } $user = $this->userManager->createUser($input->getArgument('uid'), $password); if ($user instanceof IUser) { $output->writeln('<info>The user "' . $user->getUID() . '" was created successfully</info>'); } else { $output->writeln('<error>An error occurred while creating the user</error>'); return 1; } if ($input->getOption('display-name')) { $user->setDisplayName($input->getOption('display-name')); $output->writeln('Display name set to "' . $user->getDisplayName() . '"'); } foreach ($input->getOption('group') as $groupName) { $group = $this->groupManager->get($groupName); if (!$group) { $this->groupManager->createGroup($groupName); $group = $this->groupManager->get($groupName); $output->writeln('Created group "' . $group->getGID() . '"'); } $group->addUser($user); $output->writeln('User "' . $user->getUID() . '" added to group "' . $group->getGID() . '"'); } }
/** * @NoAdminRequired * * @param string $username * @param string $password * @param array $groups * @param string $email * @return DataResponse */ public function create($username, $password, array $groups = array(), $email = '') { if ($email !== '' && !$this->mail->validateAddress($email)) { return new DataResponse(array('message' => (string) $this->l10n->t('Invalid mail address')), Http::STATUS_UNPROCESSABLE_ENTITY); } if (!$this->isAdmin) { $userId = $this->userSession->getUser()->getUID(); if (!empty($groups)) { foreach ($groups as $key => $group) { if (!$this->subAdminFactory->isGroupAccessible($userId, $group)) { unset($groups[$key]); } } } if (empty($groups)) { $groups = $this->subAdminFactory->getSubAdminsOfGroups($userId); } } if ($this->userManager->userExists($username)) { return new DataResponse(array('message' => (string) $this->l10n->t('A user with that name already exists.')), Http::STATUS_CONFLICT); } try { $user = $this->userManager->createUser($username, $password); } catch (\Exception $exception) { return new DataResponse(array('message' => (string) $this->l10n->t('Unable to create user.')), Http::STATUS_FORBIDDEN); } if ($user instanceof User) { if ($groups !== null) { foreach ($groups as $groupName) { $group = $this->groupManager->get($groupName); if (empty($group)) { $group = $this->groupManager->createGroup($groupName); } $group->addUser($user); } } /** * Send new user mail only if a mail is set */ if ($email !== '') { $this->config->setUserValue($username, 'settings', 'email', $email); // data for the mail template $mailData = array('username' => $username, 'url' => $this->urlGenerator->getAbsoluteURL('/')); $mail = new TemplateResponse('settings', 'email.new_user', $mailData, 'blank'); $mailContent = $mail->render(); $mail = new TemplateResponse('settings', 'email.new_user_plain_text', $mailData, 'blank'); $plainTextMailContent = $mail->render(); $subject = $this->l10n->t('Your %s account was created', [$this->defaults->getName()]); try { $this->mail->send($email, $username, $subject, $mailContent, $this->fromMailAddress, $this->defaults->getName(), 1, $plainTextMailContent); } catch (\Exception $e) { $this->log->error("Can't send new user mail to {$email}: " . $e->getMessage(), array('app' => 'settings')); } } // fetch users groups $userGroups = $this->groupManager->getUserGroupIds($user); return new DataResponse($this->formatUserForIndex($user, $userGroups), Http::STATUS_CREATED); } return new DataResponse(array('message' => (string) $this->l10n->t('Unable to create user.')), Http::STATUS_FORBIDDEN); }
public function testDeleteGroup() { $group = $this->groupManager->createGroup($this->getUniqueId()); $result = $this->api->deleteGroup(['groupid' => $group->getGID()]); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); $this->assertFalse($this->groupManager->groupExists($group->getGID())); }
public function setup() { $this->users = []; $this->groups = []; $this->userManager = \OC::$server->getUserManager(); $this->groupManager = \OC::$server->getGroupManager(); $this->dbConn = \OC::$server->getDatabaseConnection(); // Create 3 users and 3 groups for ($i = 0; $i < 3; $i++) { $this->users[] = $this->userManager->createUser('user' . $i, 'user'); $this->groups[] = $this->groupManager->createGroup('group' . $i); } // Create admin group if (!$this->groupManager->groupExists('admin')) { $this->groupManager->createGroup('admin'); } }
public function setup() { $this->users = []; $this->groups = []; $this->userManager = \OC::$server->getUserManager(); $this->groupManager = \OC::$server->getGroupManager(); $this->dbConn = \OC::$server->getDatabaseConnection(); // Create 3 users and 3 groups for ($i = 0; $i < 3; $i++) { $this->users[] = $this->userManager->createUser('user' . $i, 'user'); $this->groups[] = $this->groupManager->createGroup('group' . $i); } // Create admin group if (!$this->groupManager->groupExists('admin')) { $this->groupManager->createGroup('admin'); } // Create "orphaned" users and groups (scenario: temporarily disabled // backend) $qb = $this->dbConn->getQueryBuilder(); $qb->insert('group_admin')->values(['gid' => $qb->createNamedParameter($this->groups[0]->getGID()), 'uid' => $qb->createNamedParameter('orphanedUser')])->execute(); $qb->insert('group_admin')->values(['gid' => $qb->createNamedParameter('orphanedGroup'), 'uid' => $qb->createNamedParameter('orphanedUser')])->execute(); $qb->insert('group_admin')->values(['gid' => $qb->createNamedParameter('orphanedGroup'), 'uid' => $qb->createNamedParameter($this->users[0]->getUID())])->execute(); }
/** * creates a new group */ public function addGroup($parameters) { // Validate name $groupid = isset($_POST['groupid']) ? $_POST['groupid'] : ''; if (preg_match('/[^a-zA-Z0-9 _\\.@\\-]/', $groupid) || empty($groupid)) { \OCP\Util::writeLog('provisioning_api', 'Attempt made to create group using invalid characters.', \OCP\Util::ERROR); return new OC_OCS_Result(null, 101, 'Invalid group name'); } // Check if it exists if ($this->groupManager->groupExists($groupid)) { return new OC_OCS_Result(null, 102); } $this->groupManager->createGroup($groupid); return new OC_OCS_Result(null, 100); }
/** * creates a new group * * @param array $parameters * @return OC_OCS_Result */ public function addGroup($parameters) { // Validate name $groupId = $this->request->getParam('groupid', ''); if (empty($groupId)) { \OCP\Util::writeLog('provisioning_api', 'Group name not supplied', \OCP\Util::ERROR); return new OC_OCS_Result(null, 101, 'Invalid group name'); } // Check if it exists if ($this->groupManager->groupExists($groupId)) { return new OC_OCS_Result(null, 102); } $this->groupManager->createGroup($groupId); return new OC_OCS_Result(null, 100); }
public function testSubAdminOfGroupAlreadySubAdmin() { $user1 = $this->generateUsers(); $user2 = $this->generateUsers(); $this->userSession->setUser($user1); $this->groupManager->get('admin')->addUser($user1); $group1 = $this->groupManager->createGroup($this->getUniqueID()); //Make user2 subadmin of group1 $_POST['groupid'] = $group1->getGID(); $result = $this->api->addSubAdmin(['userid' => $user2->getUID()]); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); //Make user2 subadmin of group1 again $_POST['groupid'] = $group1->getGID(); $result = $this->api->addSubAdmin(['userid' => $user2->getUID()]); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); $group1->delete(); }