mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 09:35:49 +02:00
Added a console command to set the password of a user.
This commit is contained in:
parent
10f39b7f45
commit
f31336bb87
2 changed files with 118 additions and 0 deletions
95
src/Command/SetPasswordCommand.php
Normal file
95
src/Command/SetPasswordCommand.php
Normal file
|
@ -0,0 +1,95 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Command;
|
||||||
|
|
||||||
|
use App\Entity\User;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
|
||||||
|
|
||||||
|
class SetPasswordCommand extends Command
|
||||||
|
{
|
||||||
|
protected static $defaultName = 'app:set-password';
|
||||||
|
|
||||||
|
protected $entityManager;
|
||||||
|
protected $encoder;
|
||||||
|
|
||||||
|
public function __construct(EntityManagerInterface $entityManager, UserPasswordEncoderInterface $passwordEncoder)
|
||||||
|
{
|
||||||
|
$this->entityManager = $entityManager;
|
||||||
|
$this->encoder = $passwordEncoder;
|
||||||
|
|
||||||
|
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected function configure()
|
||||||
|
{
|
||||||
|
$this
|
||||||
|
->setDescription('Sets the password of a user')
|
||||||
|
->setHelp('This password allows you to set the password of a user, without knowing the old password.')
|
||||||
|
->addArgument('user', InputArgument::REQUIRED, 'The name of the user')
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
|
{
|
||||||
|
$io = new SymfonyStyle($input, $output);
|
||||||
|
$user_name = $input->getArgument('user');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var User $user
|
||||||
|
*/
|
||||||
|
$users = $this->entityManager->getRepository(User::class)->findBy(['name' => $user_name]);
|
||||||
|
$user = $users[0];
|
||||||
|
|
||||||
|
|
||||||
|
if($user == null)
|
||||||
|
{
|
||||||
|
$io->error(sprintf('No user with the given username %s found in the database!', $user_name));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$io->note('User found!');
|
||||||
|
|
||||||
|
$proceed = $io->confirm(
|
||||||
|
sprintf('You are going to change the password of %s with ID %d. Proceed?',
|
||||||
|
$user->getFullName(true), $user->getID()));
|
||||||
|
|
||||||
|
if(!$proceed)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$success = false;
|
||||||
|
$new_password = "";
|
||||||
|
|
||||||
|
while(!$success) {
|
||||||
|
$pw1 = $io->askHidden("Please enter new password:");
|
||||||
|
$pw2 = $io->askHidden('Please confirm:');
|
||||||
|
if($pw1 !== $pw2) {
|
||||||
|
$io->error('The entered password did not match! Please try again.');
|
||||||
|
} else {
|
||||||
|
//Exit loop
|
||||||
|
$success = true;
|
||||||
|
$new_password = $pw1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Encode password
|
||||||
|
$hash = $this->encoder->encodePassword($user, $new_password);
|
||||||
|
$user->setPassword($hash);
|
||||||
|
|
||||||
|
//And save it to databae
|
||||||
|
$this->entityManager->persist($user);
|
||||||
|
$this->entityManager->flush();
|
||||||
|
|
||||||
|
$io->success('Password was set successful! You can now log in using the new password.');
|
||||||
|
}
|
||||||
|
}
|
|
@ -74,6 +74,12 @@ class User extends NamedDBElement implements UserInterface, HasPermissionsInterf
|
||||||
*/
|
*/
|
||||||
protected $password;
|
protected $password;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool True if the user needs to change password after log in
|
||||||
|
* @ORM\Column(type="boolean")
|
||||||
|
*/
|
||||||
|
protected $need_pw_change;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string|null The first name of the User
|
* @var string|null The first name of the User
|
||||||
* @ORM\Column(type="string", length=255, nullable=true)
|
* @ORM\Column(type="string", length=255, nullable=true)
|
||||||
|
@ -228,6 +234,23 @@ class User extends NamedDBElement implements UserInterface, HasPermissionsInterf
|
||||||
* Getters
|
* Getters
|
||||||
************************************************/
|
************************************************/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the full name in the format FIRSTNAME LASTNAME [(USERNAME)].
|
||||||
|
* Example: Max Muster (m.muster)
|
||||||
|
* @param bool $including_username Include the username in the full name.
|
||||||
|
* @return string A string with the full name of this user.
|
||||||
|
*/
|
||||||
|
public function getFullName(bool $including_username = false)
|
||||||
|
{
|
||||||
|
$str = $this->getFirstName() . ' ' . $this->getLastName();
|
||||||
|
if ($including_username) {
|
||||||
|
$str .= ' (' . $this->getName() . ')';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $str;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function setName(string $new_name) : NamedDBElement
|
public function setName(string $new_name) : NamedDBElement
|
||||||
{
|
{
|
||||||
// Anonymous user is not allowed to change its username
|
// Anonymous user is not allowed to change its username
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue