Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / User / RoleCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\User\RoleCommand.
6  */
7
8 namespace Drupal\Console\Command\User;
9
10 use Symfony\Component\Console\Input\InputOption;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Core\Command\Command;
14 use Drupal\Console\Utils\DrupalApi;
15
16 /**
17  * Class DebugCommand
18  *
19  * @package Drupal\Console\Command\User
20  */
21 class RoleCommand extends Command
22 {
23     /**
24      * @var DrupalApi
25      */
26     protected $drupalApi;
27
28     /**
29      * RoleCommand constructor.
30      *
31      * @param DrupalApi $drupalApi
32      */
33     public function __construct(DrupalApi $drupalApi)
34     {
35         $this->drupalApi = $drupalApi;
36         parent::__construct();
37     }
38
39     /**
40      * {@inheritdoc}
41      */
42     protected function configure()
43     {
44         $this
45             ->setName('user:role')
46             ->setDescription($this->trans('commands.user.role.description'))
47             ->addArgument(
48                 'operation',
49                 InputOption::VALUE_REQUIRED,
50                 $this->trans('commands.user.role.arguments.operation')
51             )
52             ->addArgument(
53                 'user',
54                 InputOption::VALUE_REQUIRED,
55                 $this->trans('commands.user.role.arguments.user')
56             )
57             ->addArgument(
58                 'role',
59                 InputOption::VALUE_REQUIRED,
60                 $this->trans('commands.user.role.arguments.roles')
61             )->setAliases(['ur']);
62     }
63
64     /**
65      * {@inheritdoc}
66      */
67     protected function execute(InputInterface $input, OutputInterface $output)
68     {
69         $operation = $input->getArgument('operation');
70         $user = $input->getArgument('user');
71         $role = $input->getArgument('role');
72
73         if (!$operation || !$user || !$role) {
74             throw new \Exception(
75                 $this->trans('commands.user.role.messages.bad-arguments')
76             );
77         }
78
79         $systemRoles = $this->drupalApi->getRoles();
80
81         if (is_numeric($user)) {
82             $userObject = user_load($user);
83         } else {
84             $userObject = user_load_by_name($user);
85         }
86
87
88         if (!is_object($userObject)) {
89             if (!filter_var($user, FILTER_VALIDATE_EMAIL) === false) {
90                 $userObject = user_load_by_mail($user);
91             }
92         }
93
94         if (!is_object($userObject)) {
95             $this->getIo()->error(sprintf($this->trans('commands.user.role.messages.no-user-found'), $user));
96             return 1;
97         }
98
99         if (!array_key_exists($role, $systemRoles)) {
100             $this->getIo()->error(sprintf($this->trans('commands.user.role.messages.no-role-found'), $role));
101             return 1;
102         }
103
104         if ("add" == $operation) {
105             $userObject->addRole($role);
106             $userObject->save();
107             $this->getIo()->success(
108                 sprintf(
109                     $this->trans('commands.user.role.messages.add-success'),
110                     $userObject->name->value . " (" . $userObject->mail->value . ") ",
111                     $role
112                 )
113             );
114         }
115
116         if ("remove" == $operation) {
117             $userObject->removeRole($role);
118             $userObject->save();
119
120             $this->getIo()->success(
121                 sprintf(
122                     $this->trans('commands.user.role.messages.remove-success'),
123                     $userObject->name->value . " (" . $userObject->mail->value . ") ",
124                     $role
125                 )
126             );
127         }
128     }
129 }