From 7cf3133c4b3abce92c49b7c0c02ba6fc059b81a6 Mon Sep 17 00:00:00 2001 From: Harold Juarez Date: Thu, 16 Nov 2017 11:15:38 -0600 Subject: [PATCH] [role:delete] New command --- config/services/role.yml | 5 + src/Command/Role/DeleteCommand.php | 182 +++++++++++++++++++++++++++++ src/Command/Role/NewCommand.php | 8 ++ src/Utils/Create/RoleData.php | 3 - 4 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 src/Command/Role/DeleteCommand.php diff --git a/config/services/role.yml b/config/services/role.yml index 4359612d5..b842ed1d4 100644 --- a/config/services/role.yml +++ b/config/services/role.yml @@ -2,5 +2,10 @@ services: console.role_new: class: Drupal\Console\Command\Role\NewCommand arguments: ['@database', '@entity_type.manager', '@date.formatter', '@console.drupal_api', '@console.validator', '@console.string_converter'] + tags: + - { name: drupal.command } + console.role_delete: + class: Drupal\Console\Command\Role\DeleteCommand + arguments: ['@database', '@entity_type.manager', '@date.formatter', '@console.drupal_api'] tags: - { name: drupal.command } \ No newline at end of file diff --git a/src/Command/Role/DeleteCommand.php b/src/Command/Role/DeleteCommand.php new file mode 100644 index 000000000..cf6648c2d --- /dev/null +++ b/src/Command/Role/DeleteCommand.php @@ -0,0 +1,182 @@ +database = $database; + $this->entityTypeManager = $entityTypeManager; + $this->dateFormatter = $dateFormatter; + $this->drupalApi = $drupalApi; + parent::__construct(); + } + + /** + * {@inheritdoc} + */ + protected function configure() + { + $this + ->setName('role:delete') + ->setDescription($this->trans('commands.role.delete.description')) + ->setHelp($this->trans('commands.role.delete.help')) + ->addArgument( + 'roles', + InputArgument::IS_ARRAY, + $this->trans('commands.role.delete.argument.roles') + )->setAliases(['rd']); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $io = new DrupalStyle($input, $output); + + $roles = $input->getArgument('roles'); + + $role = $this->deleteRole( + $roles + ); + + $tableHeader = [ + $this->trans('commands.role.delete.messages.role-id'), + $this->trans('commands.role.delete.messages.role-name'), + ]; + + if ($role['success']) { + $io->success( + sprintf( + $this->trans('commands.role.delete.messages.role-created') + ) + ); + + $io->table($tableHeader, $role['success']); + + return 0; + } + + if ($role['error']) { + $io->error($role['error']['error']); + + return 1; + } + } + + /** + * {@inheritdoc} + */ + protected function interact(InputInterface $input, OutputInterface $output) + { + $io = new DrupalStyle($input, $output); + + $rolename = $input->getArgument('roles'); + if (!$rolename) { + $roles_collection = []; + $roles = array_keys($this->drupalApi->getRoles()); + $io->writeln($this->trans('commands.common.questions.roles.message')); + while (true) { + $role = $io->choiceNoList( + $this->trans('commands.common.questions.roles.name'), + $roles, + null, + true + ); + $role = trim($role); + if (empty($role)) { + break; + } + + array_push($roles_collection, $role); + $role_key = array_search($role, $roles, true); + if ($role_key >= 0) { + unset($roles[$role_key]); + } + } + + $input->setArgument('roles', $roles_collection); + } + } + + /** + * Remove and returns an array of deleted roles + * + * @param $roles + * + * @return $array + */ + private function deleteRole($roles) + { + $result = []; + try { + foreach ($roles as $value) { + $role = $this->entityTypeManager->getStorage('user_role')->load($value); + $this->entityTypeManager->getStorage('user_role')->delete([$role]); + + $result['success'][] = [ + 'role-id' => $value, + 'role-name' => $value + ]; + } + } catch (\Exception $e) { + $result['error'] = [ + 'error' => 'Error: ' . get_class($e) . ', code: ' . $e->getCode() . ', message: ' . $e->getMessage() + ]; + } + + return $result; + } +} diff --git a/src/Command/Role/NewCommand.php b/src/Command/Role/NewCommand.php index b805dba1a..4e05adf35 100644 --- a/src/Command/Role/NewCommand.php +++ b/src/Command/Role/NewCommand.php @@ -174,6 +174,14 @@ function ($machine_name) { } } + /** + * Create and returns an array of new role + * + * @param $rolename + * @param $machine_name + * + * @return $array + */ private function createRole($rolename, $machine_name) { $role = Role::create( diff --git a/src/Utils/Create/RoleData.php b/src/Utils/Create/RoleData.php index d07feddad..e50675187 100644 --- a/src/Utils/Create/RoleData.php +++ b/src/Utils/Create/RoleData.php @@ -40,10 +40,7 @@ public function __construct( /** * Create and returns an array of new Roles. * - * @param $roles * @param $limit - * @param $password - * @param $timeRange * * @return array */