diff options
author | Lukas Reschke <lukas@owncloud.com> | 2016-04-06 10:40:55 +0200 |
---|---|---|
committer | Lukas Reschke <lukas@owncloud.com> | 2016-04-06 11:00:52 +0200 |
commit | a4b19a5b1e4079752e33d6eb75c72a47ce048bde (patch) | |
tree | db63cde4a4c0c69fd7c284331ba84367a93279f6 /core/Command/User | |
parent | 046506dd146f823499098d0d2b0042072e436469 (diff) | |
download | nextcloud-server-a4b19a5b1e4079752e33d6eb75c72a47ce048bde.tar.gz nextcloud-server-a4b19a5b1e4079752e33d6eb75c72a47ce048bde.zip |
Rename files to be PSR-4 compliant
Diffstat (limited to 'core/Command/User')
-rw-r--r-- | core/Command/User/Add.php | 154 | ||||
-rw-r--r-- | core/Command/User/Delete.php | 70 | ||||
-rw-r--r-- | core/Command/User/LastSeen.php | 74 | ||||
-rw-r--r-- | core/Command/User/Report.php | 86 | ||||
-rw-r--r-- | core/Command/User/ResetPassword.php | 121 |
5 files changed, 505 insertions, 0 deletions
diff --git a/core/Command/User/Add.php b/core/Command/User/Add.php new file mode 100644 index 00000000000..6c7e3a47231 --- /dev/null +++ b/core/Command/User/Add.php @@ -0,0 +1,154 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Laurens Post <lkpost@scept.re> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Core\Command\User; + +use OC\Files\Filesystem; +use OCP\IGroupManager; +use OCP\IUser; +use OCP\IUserManager; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Question\Question; + +class Add extends Command { + /** @var \OCP\IUserManager */ + protected $userManager; + + /** @var \OCP\IGroupManager */ + protected $groupManager; + + /** + * @param IUserManager $userManager + * @param IGroupManager $groupManager + */ + public function __construct(IUserManager $userManager, IGroupManager $groupManager) { + parent::__construct(); + $this->userManager = $userManager; + $this->groupManager = $groupManager; + } + + protected function configure() { + $this + ->setName('user:add') + ->setDescription('adds a user') + ->addArgument( + 'uid', + InputArgument::REQUIRED, + 'User ID used to login (must only contain a-z, A-Z, 0-9, -, _ and @)' + ) + ->addOption( + 'password-from-env', + null, + InputOption::VALUE_NONE, + 'read password from environment variable OC_PASS' + ) + ->addOption( + 'display-name', + null, + InputOption::VALUE_OPTIONAL, + 'User name used in the web UI (can contain any characters)' + ) + ->addOption( + 'group', + 'g', + InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, + 'groups the user should be added to (The group will be created if it does not exist)' + ); + } + + 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() . '"'); + } + } +} diff --git a/core/Command/User/Delete.php b/core/Command/User/Delete.php new file mode 100644 index 00000000000..b9a0a0e3950 --- /dev/null +++ b/core/Command/User/Delete.php @@ -0,0 +1,70 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Jens-Christian Fischer <jens-christian.fischer@switch.ch> + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Core\Command\User; + +use OCP\IUserManager; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Input\InputArgument; + +class Delete extends Command { + /** @var IUserManager */ + protected $userManager; + + /** + * @param IUserManager $userManager + */ + public function __construct(IUserManager $userManager) { + $this->userManager = $userManager; + parent::__construct(); + } + + protected function configure() { + $this + ->setName('user:delete') + ->setDescription('deletes the specified user') + ->addArgument( + 'uid', + InputArgument::REQUIRED, + 'the username' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $user = $this->userManager->get($input->getArgument('uid')); + if (is_null($user)) { + $output->writeln('<error>User does not exist</error>'); + return; + } + + if ($user->delete()) { + $output->writeln('<info>The specified user was deleted</info>'); + return; + } + + $output->writeln('<error>The specified user could not be deleted. Please check the logs.</error>'); + } +} diff --git a/core/Command/User/LastSeen.php b/core/Command/User/LastSeen.php new file mode 100644 index 00000000000..6bb45a87875 --- /dev/null +++ b/core/Command/User/LastSeen.php @@ -0,0 +1,74 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Pierre Ozoux <pierre@ozoux.net> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Core\Command\User; + +use OCP\IUserManager; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Input\InputArgument; + +class LastSeen extends Command { + /** @var IUserManager */ + protected $userManager; + + /** + * @param IUserManager $userManager + */ + public function __construct(IUserManager $userManager) { + $this->userManager = $userManager; + parent::__construct(); + } + + protected function configure() { + $this + ->setName('user:lastseen') + ->setDescription('shows when the user was logged in last time') + ->addArgument( + 'uid', + InputArgument::REQUIRED, + 'the username' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $user = $this->userManager->get($input->getArgument('uid')); + if(is_null($user)) { + $output->writeln('<error>User does not exist</error>'); + return; + } + + $lastLogin = $user->getLastLogin(); + if($lastLogin === 0) { + $output->writeln('User ' . $user->getUID() . + ' has never logged in, yet.'); + } else { + $date = new \DateTime(); + $date->setTimestamp($lastLogin); + $output->writeln($user->getUID() . + '`s last login: ' . $date->format('d.m.Y H:i')); + } + } +} diff --git a/core/Command/User/Report.php b/core/Command/User/Report.php new file mode 100644 index 00000000000..df9f7e41620 --- /dev/null +++ b/core/Command/User/Report.php @@ -0,0 +1,86 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Core\Command\User; + +use OCP\IUserManager; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class Report extends Command { + /** @var IUserManager */ + protected $userManager; + + /** + * @param IUserManager $userManager + */ + public function __construct(IUserManager $userManager) { + $this->userManager = $userManager; + parent::__construct(); + } + + protected function configure() { + $this + ->setName('user:report') + ->setDescription('shows how many users have access'); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + /** @var \Symfony\Component\Console\Helper\TableHelper $table */ + $table = $this->getHelperSet()->get('table'); + $table->setHeaders(array('User Report', '')); + $userCountArray = $this->countUsers(); + if(!empty($userCountArray)) { + $total = 0; + $rows = array(); + foreach($userCountArray as $classname => $users) { + $total += $users; + $rows[] = array($classname, $users); + } + + $rows[] = array(' '); + $rows[] = array('total users', $total); + } else { + $rows[] = array('No backend enabled that supports user counting', ''); + } + + $userDirectoryCount = $this->countUserDirectories(); + $rows[] = array(' '); + $rows[] = array('user directories', $userDirectoryCount); + + $table->setRows($rows); + $table->render($output); + } + + private function countUsers() { + return $this->userManager->countUsers(); + } + + private function countUserDirectories() { + $dataview = new \OC\Files\View('/'); + $userDirectories = $dataview->getDirectoryContent('/', 'httpd/unix-directory'); + return count($userDirectories); + } +} diff --git a/core/Command/User/ResetPassword.php b/core/Command/User/ResetPassword.php new file mode 100644 index 00000000000..f3f2d5b0630 --- /dev/null +++ b/core/Command/User/ResetPassword.php @@ -0,0 +1,121 @@ +<?php +/** + * @author Andreas Fischer <bantu@owncloud.com> + * @author Christopher Schäpers <kondou@ts.unde.re> + * @author Clark Tomlinson <fallen013@gmail.com> + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Laurens Post <lkpost@scept.re> + * @author Morris Jobke <hey@morrisjobke.de> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Core\Command\User; + +use OCP\IUserManager; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class ResetPassword extends Command { + + /** @var IUserManager */ + protected $userManager; + + public function __construct(IUserManager $userManager) { + $this->userManager = $userManager; + parent::__construct(); + } + + protected function configure() { + $this + ->setName('user:resetpassword') + ->setDescription('Resets the password of the named user') + ->addArgument( + 'user', + InputArgument::REQUIRED, + 'Username to reset password' + ) + ->addOption( + 'password-from-env', + null, + InputOption::VALUE_NONE, + 'read password from environment variable OC_PASS' + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $username = $input->getArgument('user'); + + /** @var $user \OCP\IUser */ + $user = $this->userManager->get($username); + if (is_null($user)) { + $output->writeln('<error>User does not exist</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'); + + if (\OCP\App::isEnabled('encryption')) { + $output->writeln( + '<error>Warning: Resetting the password when using encryption will result in data loss!</error>' + ); + if (!$dialog->askConfirmation($output, '<question>Do you want to continue?</question>', true)) { + return 1; + } + } + + $password = $dialog->askHiddenResponse( + $output, + '<question>Enter a new password: </question>', + false + ); + $confirm = $dialog->askHiddenResponse( + $output, + '<question>Confirm the new 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 new password!</error>"); + return 1; + } + + $success = $user->setPassword($password); + if ($success) { + $output->writeln("<info>Successfully reset password for " . $username . "</info>"); + } else { + $output->writeln("<error>Error while resetting password!</error>"); + return 1; + } + } +} |