Move "NewPassword" to a common "user" console command

This commit is contained in:
nupplaPhil 2020-02-20 23:08:19 +01:00
parent 19b89d1fd1
commit 4d436c10df
No known key found for this signature in database
GPG Key ID: D8365C3D36B77D90
2 changed files with 31 additions and 11 deletions

View File

@ -24,7 +24,7 @@ namespace Friendica\Console;
use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Database\Database;
use Friendica\Model\User;
use Friendica\Model\User as UserModel;
use RuntimeException;
/**
@ -32,7 +32,7 @@ use RuntimeException;
*
* With this tool, you can set a new password for a user
*/
class NewPassword extends \Asika\SimpleConsole\Console
class User extends \Asika\SimpleConsole\Console
{
protected $helpOptions = ['h', 'help', '?'];
@ -52,12 +52,12 @@ class NewPassword extends \Asika\SimpleConsole\Console
protected function getHelp()
{
$help = <<<HELP
console newpassword - Creates a new password for a given user
console user - Modify user settings per console commands.
Usage
bin/console newpassword <nickname> [<password>] [-h|--help|-?] [-v]
bin/console user <nickname> password [<password>] [-h|--help|-?] [-v]
Description
Creates a new password for a user without using the "forgot password" functionality.
Modify user settings per console commands.
Options
-h|--help|-? Show help information
@ -88,8 +88,8 @@ HELP;
return 0;
}
if (count($this->args) > 2) {
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
if (count($this->args) < 2) {
throw new \Asika\SimpleConsole\CommandArgsException('Not enough arguments.');
}
if ($this->appMode->isInstall()) {
@ -103,14 +103,34 @@ HELP;
throw new RuntimeException($this->l10n->t('User not found'));
}
$password = $this->getArgument(1);
$command = $this->getArgument(1);
switch ($command) {
case 'password':
return $this->setPassword($user);
default:
throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
}
}
/**
* Sets a new password
*
* @param array $user The user
*
* @return int Return code of this command
*/
private function setPassword(array $user)
{
$password = $this->getArgument(2);
if (is_null($password)) {
$this->out($this->l10n->t('Enter new password: '), false);
$password = \Seld\CliPrompt\CliPrompt::hiddenPrompt(true);
}
try {
$result = User::updatePassword($user['uid'], $password);
$result = UserModel::updatePassword($user['uid'], $password);
if (!$this->dba->isResult($result)) {
throw new \Exception($this->l10n->t('Password update failed. Please try again.'));

View File

@ -57,7 +57,7 @@ Commands:
autoinstall Starts automatic installation of friendica based on values from htconfig.php
lock Edit site locks
maintenance Set maintenance mode for this node
newpassword Set a new password for a given user
user User management
php2po Generate a messages.po file from a strings.php file
po2php Generate a strings.php file from a messages.po file
typo Checks for parse errors in Friendica files
@ -85,7 +85,7 @@ HELP;
'autoinstall' => Friendica\Console\AutomaticInstallation::class,
'lock' => Friendica\Console\Lock::class,
'maintenance' => Friendica\Console\Maintenance::class,
'newpassword' => Friendica\Console\NewPassword::class,
'user' => Friendica\Console\User::class,
'php2po' => Friendica\Console\PhpToPo::class,
'po2php' => Friendica\Console\PoToPhp::class,
'typo' => Friendica\Console\Typo::class,