本文整理汇总了PHP中OCP\IUserManager::createUser方法的典型用法代码示例。如果您正苦于以下问题:PHP IUserManager::createUser方法的具体用法?PHP IUserManager::createUser怎么用?PHP IUserManager::createUser使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OCP\IUserManager
的用法示例。
在下文中一共展示了IUserManager::createUser方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: generateUsers
/**
* Generates a temp user
* @param int $num number of users to generate
* @return IUser[]|Iuser
*/
protected function generateUsers($num = 1)
{
$users = array();
for ($i = 0; $i < $num; $i++) {
$user = $this->userManager->createUser($this->getUniqueID(), 'password');
$this->users[] = $user;
$users[] = $user;
}
return count($users) == 1 ? reset($users) : $users;
}
示例2: execute
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() . '"');
}
$groups = $input->getOption('group');
if (!empty($groups)) {
// Make sure we init the Filesystem for the user, in case we need to
// init some group shares.
Filesystem::init($user->getUID(), '');
}
foreach ($groups 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() . '"');
}
}
示例3: create
/**
* @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);
}
示例4: testRun
public function testRun()
{
//Add test user
$user1 = $this->userManager->createUser('test1', 'test1');
$userFolder = $this->rootFolder->getUserFolder('test1');
$fileId = $userFolder->getId();
//Now insert cyclic share
$qb = $this->connection->getQueryBuilder();
$qb->insert('share')->values(['share_type' => $qb->createNamedParameter(0), 'share_with' => $qb->createNamedParameter('foo'), 'uid_owner' => $qb->createNamedParameter('owner'), 'item_type' => $qb->createNamedParameter('file'), 'item_source' => $qb->createNamedParameter($fileId), 'item_target' => $qb->createNamedParameter('/target'), 'file_source' => $qb->createNamedParameter($fileId), 'file_target' => $qb->createNamedParameter('/target'), 'permissions' => $qb->createNamedParameter(1)]);
$qb->execute();
//Add test user
$user2 = $this->userManager->createUser('test2', 'test2');
$userFolder = $this->rootFolder->getUserFolder('test2');
$folder = $userFolder->newFolder('foo');
$fileId = $folder->getId();
//Now insert cyclic share
$qb = $this->connection->getQueryBuilder();
$qb->insert('share')->values(['share_type' => $qb->createNamedParameter(0), 'share_with' => $qb->createNamedParameter('foo'), 'uid_owner' => $qb->createNamedParameter('owner'), 'item_type' => $qb->createNamedParameter('file'), 'item_source' => $qb->createNamedParameter($fileId), 'item_target' => $qb->createNamedParameter('/target'), 'file_source' => $qb->createNamedParameter($fileId), 'file_target' => $qb->createNamedParameter('/target'), 'permissions' => $qb->createNamedParameter(1)]);
$qb->execute();
$this->repair->run($this->outputMock);
//Verify
$qb = $this->connection->getQueryBuilder();
$qb->selectAlias($qb->createFunction('COUNT(*)'), 'count')->from('share');
$cursor = $qb->execute();
$data = $cursor->fetch();
$cursor->closeCursor();
$count = (int) $data['count'];
$this->assertEquals(1, $count);
$user1->delete();
$user2->delete();
}
示例5: setup
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');
}
}
示例6: addUser
/**
* @return OC_OCS_Result
*/
public function addUser()
{
$userId = isset($_POST['userid']) ? $_POST['userid'] : null;
$password = isset($_POST['password']) ? $_POST['password'] : null;
if ($this->userManager->userExists($userId)) {
\OCP\Util::writeLog('ocs_api', 'Failed addUser attempt: User already exists.', \OCP\Util::ERROR);
return new OC_OCS_Result(null, 102, 'User already exists');
} else {
try {
$this->userManager->createUser($userId, $password);
\OCP\Util::writeLog('ocs_api', 'Successful addUser call with userid: ' . $_POST['userid'], \OCP\Util::INFO);
return new OC_OCS_Result(null, 100);
} catch (\Exception $e) {
\OCP\Util::writeLog('ocs_api', 'Failed addUser attempt with exception: ' . $e->getMessage(), \OCP\Util::ERROR);
return new OC_OCS_Result(null, 101, 'Bad request');
}
}
}
示例7: setup
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();
}
示例8: addUser
/**
* @return OC_OCS_Result
*/
public function addUser()
{
$userId = isset($_POST['userid']) ? $_POST['userid'] : null;
$password = isset($_POST['password']) ? $_POST['password'] : null;
if ($this->userManager->userExists($userId)) {
$this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
return new OC_OCS_Result(null, 102, 'User already exists');
} else {
try {
$this->userManager->createUser($userId, $password);
$this->logger->info('Successful addUser call with userid: ' . $_POST['userid'], ['app' => 'ocs_api']);
return new OC_OCS_Result(null, 100);
} catch (\Exception $e) {
$this->logger->error('Failed addUser attempt with exception: ' . $e->getMessage(), ['app' => 'ocs_api']);
return new OC_OCS_Result(null, 101, 'Bad request');
}
}
}
示例9: addUser
/**
* @return OC_OCS_Result
*/
public function addUser()
{
$userId = isset($_POST['userid']) ? $_POST['userid'] : null;
$password = isset($_POST['password']) ? $_POST['password'] : null;
$groups = isset($_POST['groups']) ? $_POST['groups'] : null;
$user = $this->userSession->getUser();
$isAdmin = $this->groupManager->isAdmin($user->getUID());
$subAdminManager = $this->groupManager->getSubAdmin();
if (!$isAdmin && !$subAdminManager->isSubAdmin($user)) {
return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED);
}
if ($this->userManager->userExists($userId)) {
$this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
return new OC_OCS_Result(null, 102, 'User already exists');
}
if (is_array($groups)) {
foreach ($groups as $group) {
if (!$this->groupManager->groupExists($group)) {
return new OC_OCS_Result(null, 104, 'group ' . $group . ' does not exist');
}
if (!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) {
return new OC_OCS_Result(null, 105, 'insufficient privileges for group ' . $group);
}
}
} else {
if (!$isAdmin) {
return new OC_OCS_Result(null, 106, 'no group specified (required for subadmins)');
}
}
try {
$newUser = $this->userManager->createUser($userId, $password);
$this->logger->info('Successful addUser call with userid: ' . $userId, ['app' => 'ocs_api']);
if (is_array($groups)) {
foreach ($groups as $group) {
$this->groupManager->get($group)->addUser($newUser);
$this->logger->info('Added userid ' . $userId . ' to group ' . $group, ['app' => 'ocs_api']);
}
}
return new OC_OCS_Result(null, 100);
} catch (\Exception $e) {
$this->logger->error('Failed addUser attempt with exception: ' . $e->getMessage(), ['app' => 'ocs_api']);
return new OC_OCS_Result(null, 101, 'Bad request');
}
}
示例10: createUser
/**
* Creates a local user
*
* @param $userId
* @param $password
*/
private function createUser($userId, $password)
{
$user = $this->userManager->createUser($userId, $password);
$user->setDisplayName('Gallery Tester (' . $userId . ')');
}