2016-06-09 15:49:53 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-13 19:46:27 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2020-01-13 16:23:49 +03:00
|
|
|
* @author Kim Brose <kim.brose@rwth-aachen.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-06-09 15:49:53 +03:00
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-06-09 15:49:53 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Core\Command\User;
|
|
|
|
|
|
|
|
use OC\Core\Command\Base;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IDBConnection;
|
2017-02-08 15:52:55 +03:00
|
|
|
use OCP\IUser;
|
2016-06-09 15:49:53 +03:00
|
|
|
use OCP\IUserManager;
|
2019-11-22 22:52:10 +03:00
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
2016-06-09 15:49:53 +03:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
class Setting extends Base {
|
|
|
|
/** @var IUserManager */
|
|
|
|
protected $userManager;
|
|
|
|
|
|
|
|
/** @var IConfig */
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/** @var IDBConnection */
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IUserManager $userManager
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param IDBConnection $connection
|
|
|
|
*/
|
|
|
|
public function __construct(IUserManager $userManager, IConfig $config, IDBConnection $connection) {
|
|
|
|
parent::__construct();
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->connection = $connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function configure() {
|
|
|
|
parent::configure();
|
|
|
|
$this
|
|
|
|
->setName('user:setting')
|
2016-06-14 16:22:13 +03:00
|
|
|
->setDescription('Read and modify user settings')
|
2016-06-09 15:49:53 +03:00
|
|
|
->addArgument(
|
|
|
|
'uid',
|
|
|
|
InputArgument::REQUIRED,
|
|
|
|
'User ID used to login'
|
|
|
|
)
|
|
|
|
->addArgument(
|
|
|
|
'app',
|
|
|
|
InputArgument::OPTIONAL,
|
2016-06-14 16:22:13 +03:00
|
|
|
'Restrict the settings to a given app',
|
2016-06-09 15:49:53 +03:00
|
|
|
''
|
|
|
|
)
|
|
|
|
->addArgument(
|
|
|
|
'key',
|
|
|
|
InputArgument::OPTIONAL,
|
|
|
|
'Setting key to set, get or delete',
|
|
|
|
''
|
|
|
|
)
|
2016-06-09 16:07:06 +03:00
|
|
|
->addOption(
|
|
|
|
'ignore-missing-user',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'Use this option to ignore errors when the user does not exist'
|
|
|
|
)
|
|
|
|
|
|
|
|
// Get
|
2016-06-09 15:49:53 +03:00
|
|
|
->addOption(
|
|
|
|
'default-value',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_REQUIRED,
|
|
|
|
'(Only applicable on get) If no default value is set and the config does not exist, the command will exit with 1'
|
|
|
|
)
|
2016-06-09 16:07:06 +03:00
|
|
|
|
|
|
|
// Set
|
2016-07-14 16:09:47 +03:00
|
|
|
->addArgument(
|
2016-06-09 16:07:06 +03:00
|
|
|
'value',
|
2016-07-14 16:09:47 +03:00
|
|
|
InputArgument::OPTIONAL,
|
|
|
|
'The new value of the setting',
|
|
|
|
null
|
2016-06-09 16:07:06 +03:00
|
|
|
)
|
|
|
|
->addOption(
|
|
|
|
'update-only',
|
2016-06-09 15:49:53 +03:00
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
2016-06-09 16:07:06 +03:00
|
|
|
'Only updates the value, if it is not set before, it is not being added'
|
2016-06-09 15:49:53 +03:00
|
|
|
)
|
2016-06-09 16:14:15 +03:00
|
|
|
|
|
|
|
// Delete
|
|
|
|
->addOption(
|
|
|
|
'delete',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'Specify this option to delete the config'
|
|
|
|
)
|
|
|
|
->addOption(
|
|
|
|
'error-if-not-exists',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'Checks whether the setting exists before deleting it'
|
|
|
|
)
|
2016-06-09 15:49:53 +03:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function checkInput(InputInterface $input) {
|
|
|
|
$uid = $input->getArgument('uid');
|
|
|
|
if (!$input->getOption('ignore-missing-user') && !$this->userManager->userExists($uid)) {
|
2020-01-03 06:17:11 +03:00
|
|
|
throw new \InvalidArgumentException('The user "' . $uid . '" does not exist.');
|
2016-06-09 15:49:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($input->getArgument('key') === '' && $input->hasParameterOption('--default-value')) {
|
2016-06-09 16:14:15 +03:00
|
|
|
throw new \InvalidArgumentException('The "default-value" option can only be used when specifying a key.');
|
2016-06-09 16:07:06 +03:00
|
|
|
}
|
|
|
|
|
2016-07-14 16:09:47 +03:00
|
|
|
if ($input->getArgument('key') === '' && $input->getArgument('value') !== null) {
|
|
|
|
throw new \InvalidArgumentException('The value argument can only be used when specifying a key.');
|
2016-06-09 16:14:15 +03:00
|
|
|
}
|
2016-07-14 16:09:47 +03:00
|
|
|
if ($input->getArgument('value') !== null && $input->hasParameterOption('--default-value')) {
|
|
|
|
throw new \InvalidArgumentException('The value argument can not be used together with "default-value".');
|
2016-06-09 16:07:06 +03:00
|
|
|
}
|
2016-07-14 16:09:47 +03:00
|
|
|
if ($input->getOption('update-only') && $input->getArgument('value') === null) {
|
2016-06-09 16:07:06 +03:00
|
|
|
throw new \InvalidArgumentException('The "update-only" option can only be used together with "value".');
|
2016-06-09 15:49:53 +03:00
|
|
|
}
|
2016-06-09 16:14:15 +03:00
|
|
|
|
2016-06-13 17:04:38 +03:00
|
|
|
if ($input->getArgument('key') === '' && $input->getOption('delete')) {
|
2016-06-09 16:14:15 +03:00
|
|
|
throw new \InvalidArgumentException('The "delete" option can only be used when specifying a key.');
|
|
|
|
}
|
2016-06-13 17:04:38 +03:00
|
|
|
if ($input->getOption('delete') && $input->hasParameterOption('--default-value')) {
|
|
|
|
throw new \InvalidArgumentException('The "delete" option can not be used together with "default-value".');
|
2016-06-09 16:14:15 +03:00
|
|
|
}
|
2016-07-14 16:09:47 +03:00
|
|
|
if ($input->getOption('delete') && $input->getArgument('value') !== null) {
|
2016-06-13 17:04:38 +03:00
|
|
|
throw new \InvalidArgumentException('The "delete" option can not be used together with "value".');
|
2016-06-09 16:14:15 +03:00
|
|
|
}
|
2016-06-13 17:04:38 +03:00
|
|
|
if ($input->getOption('error-if-not-exists') && !$input->getOption('delete')) {
|
2016-06-09 16:14:15 +03:00
|
|
|
throw new \InvalidArgumentException('The "error-if-not-exists" option can only be used together with "delete".');
|
|
|
|
}
|
2016-06-09 15:49:53 +03:00
|
|
|
}
|
|
|
|
|
2020-06-26 15:54:51 +03:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int {
|
2016-06-09 15:49:53 +03:00
|
|
|
try {
|
|
|
|
$this->checkInput($input);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>');
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$uid = $input->getArgument('uid');
|
|
|
|
$app = $input->getArgument('app');
|
|
|
|
$key = $input->getArgument('key');
|
|
|
|
|
|
|
|
if ($key !== '') {
|
|
|
|
$value = $this->config->getUserValue($uid, $app, $key, null);
|
2016-07-14 16:09:47 +03:00
|
|
|
if ($input->getArgument('value') !== null) {
|
2016-06-09 16:07:06 +03:00
|
|
|
if ($input->hasParameterOption('--update-only') && $value === null) {
|
|
|
|
$output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>');
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-02-08 15:52:55 +03:00
|
|
|
if ($app === 'settings' && $key === 'email') {
|
|
|
|
$user = $this->userManager->get($uid);
|
|
|
|
if ($user instanceof IUser) {
|
|
|
|
$user->setEMailAddress($input->getArgument('value'));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 16:09:47 +03:00
|
|
|
$this->config->setUserValue($uid, $app, $key, $input->getArgument('value'));
|
2016-06-14 12:31:32 +03:00
|
|
|
return 0;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($input->hasParameterOption('--delete')) {
|
2016-06-09 16:14:15 +03:00
|
|
|
if ($input->hasParameterOption('--error-if-not-exists') && $value === null) {
|
|
|
|
$output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>');
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-02-08 15:52:55 +03:00
|
|
|
if ($app === 'settings' && $key === 'email') {
|
|
|
|
$user = $this->userManager->get($uid);
|
|
|
|
if ($user instanceof IUser) {
|
|
|
|
$user->setEMailAddress('');
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-09 16:14:15 +03:00
|
|
|
$this->config->deleteUserValue($uid, $app, $key);
|
2016-06-14 12:31:32 +03:00
|
|
|
return 0;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($value !== null) {
|
2016-06-09 15:49:53 +03:00
|
|
|
$output->writeln($value);
|
2016-06-14 12:31:32 +03:00
|
|
|
return 0;
|
2016-06-09 15:49:53 +03:00
|
|
|
} else {
|
|
|
|
if ($input->hasParameterOption('--default-value')) {
|
|
|
|
$output->writeln($input->getOption('default-value'));
|
2016-06-14 12:31:32 +03:00
|
|
|
return 0;
|
2016-06-09 15:49:53 +03:00
|
|
|
} else {
|
|
|
|
$output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>');
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-06-14 12:31:32 +03:00
|
|
|
$settings = $this->getUserSettings($uid, $app);
|
|
|
|
$this->writeArrayInOutputFormat($input, $output, $settings);
|
|
|
|
return 0;
|
2016-06-09 15:49:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getUserSettings($uid, $app) {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('*')
|
|
|
|
->from('preferences')
|
|
|
|
->where($query->expr()->eq('userid', $query->createNamedParameter($uid)));
|
|
|
|
|
|
|
|
if ($app !== '') {
|
|
|
|
$query->andWhere($query->expr()->eq('appid', $query->createNamedParameter($app)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $query->execute();
|
|
|
|
$settings = [];
|
|
|
|
while ($row = $result->fetch()) {
|
|
|
|
$settings[$row['appid']][$row['configkey']] = $row['configvalue'];
|
|
|
|
}
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
return $settings;
|
|
|
|
}
|
|
|
|
}
|