|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (C) 2020 Jan Böhmer |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as published |
|
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace App\Command; |
|
20
|
|
|
|
|
21
|
|
|
use App\Entity\User; |
|
22
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
23
|
|
|
use Symfony\Component\Console\Command\Command; |
|
24
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
25
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
26
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
27
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
28
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
29
|
|
|
|
|
30
|
|
|
class UserPromoteCommand extends Command |
|
31
|
|
|
{ |
|
32
|
|
|
protected static $defaultName = 'app:user-promote'; |
|
33
|
|
|
|
|
34
|
|
|
protected $entityManager; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct(EntityManagerInterface $entityManager) |
|
37
|
|
|
{ |
|
38
|
|
|
parent::__construct(self::$defaultName); |
|
39
|
|
|
$this->entityManager = $entityManager; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function configure() |
|
43
|
|
|
{ |
|
44
|
|
|
$this |
|
45
|
|
|
->setDescription('Add a role to the given user, to give him actions to certain operations.') |
|
46
|
|
|
->addArgument('username', InputArgument::REQUIRED, 'The user you want to promote') |
|
47
|
|
|
->addOption('role', null, InputOption::VALUE_OPTIONAL, 'The role this user should be given (e.g. ROLE_EDIT_USER)') |
|
48
|
|
|
; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
52
|
|
|
{ |
|
53
|
|
|
$io = new SymfonyStyle($input, $output); |
|
54
|
|
|
$username = $input->getArgument('username'); |
|
55
|
|
|
|
|
56
|
|
|
$repo = $this->entityManager->getRepository(User::class); |
|
57
|
|
|
$user = $repo->findOneBy([ |
|
58
|
|
|
'username' => $username, |
|
59
|
|
|
]); |
|
60
|
|
|
|
|
61
|
|
|
if ($user) { |
|
62
|
|
|
$io->note(sprintf('You are about to change the following user: %s', $user->getUsername())); |
|
63
|
|
|
} else { |
|
64
|
|
|
$io->error('User not found!'); |
|
65
|
|
|
|
|
66
|
|
|
return self::FAILURE; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$new_role = $input->getOption('role'); |
|
70
|
|
|
if (null === $new_role) { |
|
71
|
|
|
while (empty($new_role)) { |
|
72
|
|
|
$new_role = $io->ask('Input the ROLE that should be added (e.g. ROLE_EDIT_USER)'); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$new_role = strtoupper($new_role); |
|
77
|
|
|
|
|
78
|
|
|
$user->addRole($new_role); |
|
79
|
|
|
|
|
80
|
|
|
//Save changes to DB |
|
81
|
|
|
$this->entityManager->flush(); |
|
82
|
|
|
|
|
83
|
|
|
$io->success('User was promoted successfully.'); |
|
84
|
|
|
|
|
85
|
|
|
return 0; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|