2019-03-20 12:27:11 +01:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-22 18:14:36 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-08-04 21:49:16 +02:00
|
|
|
namespace App\Command\User;
|
2019-03-20 12:27:11 +01:00
|
|
|
|
2019-08-12 18:04:53 +02:00
|
|
|
use App\Entity\UserSystem\User;
|
2020-04-03 18:27:47 +02:00
|
|
|
use App\Events\SecurityEvent;
|
|
|
|
use App\Events\SecurityEvents;
|
2019-03-20 12:27:11 +01:00
|
|
|
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\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
2020-04-03 18:27:47 +02:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2021-10-02 20:41:14 +02:00
|
|
|
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
2019-03-20 12:27:11 +01:00
|
|
|
|
|
|
|
class SetPasswordCommand extends Command
|
|
|
|
{
|
2022-11-14 23:58:38 +01:00
|
|
|
protected static $defaultName = 'partdb:users:set-password|app:set-password|users:set-password|partdb:user:set-password';
|
2019-03-20 12:27:11 +01:00
|
|
|
|
2022-09-18 22:59:31 +02:00
|
|
|
protected EntityManagerInterface $entityManager;
|
|
|
|
protected UserPasswordHasherInterface $encoder;
|
|
|
|
protected EventDispatcherInterface $eventDispatcher;
|
2019-03-20 12:27:11 +01:00
|
|
|
|
2021-10-02 20:41:14 +02:00
|
|
|
public function __construct(EntityManagerInterface $entityManager, UserPasswordHasherInterface $passwordEncoder, EventDispatcherInterface $eventDispatcher)
|
2019-03-20 12:27:11 +01:00
|
|
|
{
|
|
|
|
$this->entityManager = $entityManager;
|
|
|
|
$this->encoder = $passwordEncoder;
|
2020-04-03 18:27:47 +02:00
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
2019-03-20 12:27:11 +01:00
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:46:58 +01:00
|
|
|
protected function configure(): void
|
2019-03-20 12:27:11 +01:00
|
|
|
{
|
|
|
|
$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')
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:00:03 +01:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
2019-03-20 12:27:11 +01:00
|
|
|
{
|
|
|
|
$io = new SymfonyStyle($input, $output);
|
|
|
|
$user_name = $input->getArgument('user');
|
|
|
|
|
2020-02-01 17:00:03 +01:00
|
|
|
/** @var User[] $users */
|
2019-03-20 12:27:11 +01:00
|
|
|
$users = $this->entityManager->getRepository(User::class)->findBy(['name' => $user_name]);
|
|
|
|
|
2020-02-01 19:42:28 +01:00
|
|
|
if (empty($users)) {
|
2019-03-20 12:27:11 +01:00
|
|
|
$io->error(sprintf('No user with the given username %s found in the database!', $user_name));
|
2019-03-20 23:16:07 +01:00
|
|
|
|
2020-02-01 17:00:03 +01:00
|
|
|
return 1;
|
2019-03-20 12:27:11 +01:00
|
|
|
}
|
|
|
|
|
2020-02-01 19:42:28 +01:00
|
|
|
$user = $users[0];
|
|
|
|
|
2019-03-20 12:27:11 +01:00
|
|
|
$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()));
|
|
|
|
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!$proceed) {
|
2020-02-01 17:00:03 +01:00
|
|
|
return 1;
|
2019-03-20 12:27:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$success = false;
|
2019-03-20 23:16:07 +01:00
|
|
|
$new_password = '';
|
2019-03-20 12:27:11 +01:00
|
|
|
|
2020-08-21 21:36:22 +02:00
|
|
|
while (!$success) {
|
2019-03-20 23:16:07 +01:00
|
|
|
$pw1 = $io->askHidden('Please enter new password:');
|
2019-03-20 12:27:11 +01:00
|
|
|
$pw2 = $io->askHidden('Please confirm:');
|
2019-03-20 23:16:07 +01:00
|
|
|
if ($pw1 !== $pw2) {
|
2019-03-20 12:27:11 +01:00
|
|
|
$io->error('The entered password did not match! Please try again.');
|
|
|
|
} else {
|
|
|
|
//Exit loop
|
|
|
|
$success = true;
|
|
|
|
$new_password = $pw1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Encode password
|
2021-10-02 20:41:14 +02:00
|
|
|
$hash = $this->encoder->hashPassword($user, $new_password);
|
2019-03-20 12:27:11 +01:00
|
|
|
$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.');
|
2020-03-15 13:56:31 +01:00
|
|
|
|
2020-04-03 18:27:47 +02:00
|
|
|
$security_event = new SecurityEvent($user);
|
|
|
|
$this->eventDispatcher->dispatch($security_event, SecurityEvents::PASSWORD_CHANGED);
|
|
|
|
|
2020-02-01 17:00:03 +01:00
|
|
|
return 0;
|
2019-03-20 12:27:11 +01:00
|
|
|
}
|
|
|
|
}
|