Yaffs site version 1.1
[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 Symfony\Component\Console\Command\Command;
14 use Drupal\Console\Core\Command\Shared\CommandTrait;
15 use Drupal\Console\Utils\DrupalApi;
16 use Drupal\Console\Core\Style\DrupalStyle;
17
18 /**
19  * Class DebugCommand
20  *
21  * @package Drupal\Console\Command\User
22  */
23 class RoleCommand extends Command
24 {
25     use CommandTrait;
26
27     /**
28      * @var DrupalApi
29      */
30     protected $drupalApi;
31
32     /**
33      * RoleCommand constructor.
34      *
35      * @param DrupalApi $drupalApi
36      */
37     public function __construct(DrupalApi $drupalApi)
38     {
39         $this->drupalApi = $drupalApi;
40         parent::__construct();
41     }
42
43     /**
44      * {@inheritdoc}
45      */
46     protected function configure()
47     {
48         $this
49             ->setName('user:role')
50             ->setDescription($this->trans('commands.user.role.description'))
51             ->addArgument(
52                 'operation',
53                 InputOption::VALUE_REQUIRED,
54                 $this->trans('commands.user.role.operation')
55             )
56             ->addArgument(
57                 'user',
58                 InputOption::VALUE_REQUIRED,
59                 $this->trans('commands.user.role.user')
60             )
61             ->addArgument(
62                 'role',
63                 InputOption::VALUE_REQUIRED,
64                 $this->trans('commands.user.role.role')
65             );
66     }
67
68     /**
69      * {@inheritdoc}
70      */
71     protected function interact(InputInterface $input, OutputInterface $output)
72     {
73         $operation = $input->getArgument('operation');
74         $user = $input->getArgument('user');
75         $role = $input->getArgument('role');
76
77         if (!$operation || !$user || !$role) {
78             throw new \Exception(
79                 $this->trans('commands.user.role.messages.bad-arguments')
80             );
81         }
82     }
83
84     /**
85      * {@inheritdoc}
86      */
87     protected function execute(InputInterface $input, OutputInterface $output)
88     {
89         $io = new DrupalStyle($input, $output);
90         $operation = $input->getArgument('operation');
91         $user = $input->getArgument('user');
92         $role = $input->getArgument('role');
93
94         $systemRoles = $this->drupalApi->getRoles();
95
96         if (is_numeric($user)) {
97             $userObject = user_load($user);
98         } else {
99             $userObject = user_load_by_name($user);
100         }
101
102
103         if (!is_object($userObject)) {
104             if (!filter_var($user, FILTER_VALIDATE_EMAIL) === false) {
105                 $userObject = user_load_by_mail($user);
106             }
107         }
108
109         if (!is_object($userObject)) {
110             $io->error(sprintf($this->trans('commands.user.role.messages.no-user-found'), $user));
111             return 1;
112         }
113
114         if (!array_key_exists($role, $systemRoles)) {
115             $io->error(sprintf($this->trans('commands.user.role.messages.no-role-found'), $role));
116             return 1;
117         }
118
119         if ("add" == $operation) {
120             $userObject->addRole($role);
121             $userObject->save();
122             $io->success(
123                 sprintf(
124                     $this->trans('commands.user.role.messages.add-success'),
125                     $userObject->name->value . " (" . $userObject->mail->value . ") ",
126                     $role
127                 )
128             );
129         }
130
131         if ("remove" == $operation) {
132             $userObject->removeRole($role);
133             $userObject->save();
134
135             $io->success(
136                 sprintf(
137                     $this->trans('commands.user.role.messages.remove-success'),
138                     $userObject->name->value . " (" . $userObject->mail->value . ") ",
139                     $role
140                 )
141             );
142         }
143     }
144 }