diff --git a/Console/Command/NewUserRole.php b/Console/Command/NewUserRole.php deleted file mode 100644 index 6f6b7d8..0000000 --- a/Console/Command/NewUserRole.php +++ /dev/null @@ -1,125 +0,0 @@ -roleFactory = $roleFactory; - $this->rulesFactory = $rulesFactory; - parent::__construct(); - } - - /** - * @inheritDoc - */ - protected function configure() - { - $this->setName('salecto:user:role'); - $this->setDescription('This is my first console command.'); - $this->addOption( - self::NAME, - null, - InputOption::VALUE_REQUIRED, - 'NAME' - ); - parent::configure(); - } - - /** - * Execute the command - * - * @param InputInterface $input - * @param OutputInterface $output - * - * @return null|int - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - $salectoRole = $input->getOption(self::NAME); - $roles = $this->roleFactory->create(); - $role = $roles->getCollection(); - $role->addFieldToFilter('role_name',['eq' => $salectoRole]); - $role->addFieldToSelect('role_name'); - - if (empty($salectoRole)) { - $output->writeln(' --User Role Name Require. i.e. --role="value"'); - } elseif (empty($role->getFirstItem()->getRoleName())) { - - try{ - $role = $this->roleFactory->create(); - $role->setName($salectoRole) - ->setPid(0) - ->setRoleType(RoleGroup::ROLE_TYPE) - ->setUserType(UserContextInterface::USER_TYPE_ADMIN); - $role->save(); - $resource=[ - 'Magento_Cms::config_cms', - 'Magento_Backend::content_elements', - 'Magento_Cms::page', - 'Magento_Cms::save', - 'Magento_Cms::save_design', - 'Magento_Backend::stores', - 'Magento_Backend::stores_settings', - 'Magento_Config::config', - 'Magento_TwoFactorAuth::config', - 'Magento_Backend::system', - 'Magento_User::acl', - 'Magento_TwoFactorAuth::tfa' - ]; - $return = $this->rulesFactory->create() - ->setRoleId($role->getId()) - ->setResources($resource) - ->saveRel(); - }catch (\Exception $e){ - $output->writeln('Can not save new user role - `' . $e . '`'); - } - $output->writeln(' New User Role Added `'.$salectoRole.'`'); - } else { - $output->writeln(' --Role name already exists `'.$salectoRole.'`'); - } - } -} diff --git a/Console/Command/SalectoUserRole.php b/Console/Command/SalectoUserRole.php new file mode 100644 index 0000000..e6692db --- /dev/null +++ b/Console/Command/SalectoUserRole.php @@ -0,0 +1,175 @@ +roleFactory = $roleFactory; + $this->rulesFactory = $rulesFactory; + $this->aclResourceProvider = $aclResourceProvider; + parent::__construct(); + } + + /** + * @inheritDoc + */ + protected function configure () + { + $this->setName('reform:role:resource'); + $this->setDescription('Command for reform user role resources.'); + $this->addArgument( + self::ROLE_ID, + InputArgument::REQUIRED, + 'Expects role id' + ); + $this->addArgument( + self::RESOURCE_IDS, + InputArgument::IS_ARRAY | InputArgument::REQUIRED, + 'Expects resporces id(s)' + ); + $this->addOption( + self::DECLINE, + null, + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, + 'unsets the resource id and children from requested resources' + ); + parent::configure(); + } + + /** + * Execute the command + * + * @param InputInterface $input + * @param OutputInterface $output + * + * @return Exception|int + */ + protected function execute (InputInterface $input, OutputInterface $output) + { + $obsereRole = $input->getArgument('role'); + + $configResource = $this->aclResourceProvider->getAclResources(); + + $resourceIds = []; + foreach ($input->getArgument('resource') as $obsereResource ) { + $resourceFamily = $this->findResourceId($configResource, 'id', $obsereResource); + $familyIds = $this->getResourceIds($resourceFamily, 'id'); + $resourceIds = array_merge($resourceIds, $familyIds); + } + + $removeIds = $input->getOption(self::DECLINE); + + foreach ($removeIds as $removeId ) { + $resourceFamily = $this->findResourceId($resourceFamily, 'id', $removeId); + $familyIds = $this->getResourceIds($resourceFamily, 'id'); + $removeIds = array_merge($removeIds, $familyIds); + } + + $loadRole = $this->roleFactory->create()->load($obsereRole); + + if ($loadRole->getId()) { + try { + $updateRole = $this->rulesFactory->create() + ->setRoleId($loadRole->getId()) + ->setResources(array_values(array_diff($resourceIds, $removeIds))) + ->saveRel(); + + if ($updateRole->getRoleId()) { + $output->writeln('User role `'.$updateRole->getRoleId().'` => `'.$loadRole->getRoleName().'` been updated.'); + } + } catch (\Exception $e) { + $output->writeln('Can not save new user role - `' . $e . '`'); + } + } else { + $output->writeln('No user role found with id `'.$obsereRole.'`.'); + } + } + + /** + * It will fetch the array family of provided parent resource id `$configResource` + * + * @param Resources array $resourceData + * @param resource id $key + * @param parent resource id to find. $value + * + * @return null|array + */ + public function findResourceId($resourceData, $key, $value) + { + $results = array(); + if (is_array($resourceData)) { + if (isset($resourceData[$key]) && $resourceData[$key] == $value) { + $results[] = $resourceData; + } + + foreach ($resourceData as $subData) { + $results = array_merge($results, $this->findResourceId($subData, $key, $value)); + } + } + return $results; + } + + /** + * Collects the key `id` from array output of findResourceId() + * + * @param Resources array $resourceData + * @param resource ids to collect $key + * + * @return null|array + */ + public function getResourceIds($array, $key) { + $results = array(); + if (is_array($array)) { + if (isset($array[$key])) { + $results[] = $array[$key]; + } + foreach ($array as $subarray) { + $results = array_merge($results, $this->getResourceIds($subarray, $key)); + } + } + return $results; + } +} diff --git a/Console/Command/SetUserRole.php b/Console/Command/SetUserRole.php index 6301eca..ef00058 100644 --- a/Console/Command/SetUserRole.php +++ b/Console/Command/SetUserRole.php @@ -1,9 +1,9 @@ setName('salecto:set:admin'); + $this->setName('salecto:set:role'); $this->setDescription('It will set user as administrator.'); $this->addOption( self::USERID, @@ -92,8 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $userId = $input->getOption(self::USERID); $roleId = $input->getOption(self::ROLEID); - if($userId && $roleId){ - + if ($userId && $roleId) { $model = $this->_userFactory->create()->load($userId); $roleName = $this->checkRole($roleId); if ($userId && $model->isObjectNew()) { @@ -106,14 +105,21 @@ protected function execute(InputInterface $input, OutputInterface $output) $model->save(); $output->writeln('User with id `'.$userId.'`set as `'.$roleName.'`'); } - }else{ + } else { $output->writeln('please set both userid and roleid, i.e. --userid=integer --roleid=integer'); } } + /** + * Execute the command + * + * @param role id $roleId + * + * @return null|0|string + */ private function checkRole($roleId){ $role = $this->_roleFactory->create()->load($roleId); - if($role->getId()){ + if ($role->getId()) { return $role->getRoleName(); } else { return 0; diff --git a/README.md b/README.md index 55565b6..148d61e 100644 --- a/README.md +++ b/README.md @@ -4,16 +4,18 @@ Custom User Role and assining to user with command line. ## Composer install -- `composer config repositories.reponame vcs https://github.com/vac19/CustomUserRole` -- `composer require salecto1/magento2-custom-userRole` +- `composer config repositories.reponame vcs https://github.com/vac19/userrole` +- `composer require salecto1/custom-userRole` ## Composer uninstall -- `composer remove salecto1/magento2-custom-userRole` +- `composer remove salecto1/custom-userRole` ## Preview will be added -![command](/readme-images/command.png "command") +![admin-role-resources](/readme-images/resource_selected_admin.png "admin after effect") +![command-set-resource](/readme-images/set_resources.png "set resources") +![command-set-role-to-user](/readme-images/set_role_to_user.png.png "set role to user") ## Settings @@ -29,14 +31,14 @@ Custom User Role and assining to user with command line. - vashishtha chauhan / Salecto ### Install module -1. Run `composer require salecto1/magento2-custom-userRole` +1. Run `composer require salecto1/custom-userRole` 2. Run `php bin/magento setup:upgrade` 3. Run `php bin/magento setup:di:compile` 4. Run `php bin/magento s:s:d da_DK en_US` 5. Run `php bin/magento c:c` ### Uninstall module -1. Run `composer remove salecto1/magento2-custom-userRole` +1. Run `composer remove salecto1/custom-userRole` 2. Run `php bin/magento setup:di:compile` 3. Run `php bin/magento s:s:d da_DK en_US` 4. Run `php bin/magento c:c` diff --git a/etc/di.xml b/etc/di.xml index 90ec779..3323870 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -2,7 +2,7 @@ @@ -10,8 +10,8 @@ - Salecto\NewUserRole\Console\Command\NewUserRole Salecto\NewUserRole\Console\Command\SetUserRole + Salecto\NewUserRole\Console\Command\SalectoUserRole diff --git a/readme-images/resource_selected_admin.png b/readme-images/resource_selected_admin.png new file mode 100644 index 0000000..6e90b42 Binary files /dev/null and b/readme-images/resource_selected_admin.png differ diff --git a/readme-images/set_resources.png b/readme-images/set_resources.png new file mode 100644 index 0000000..b3dc210 Binary files /dev/null and b/readme-images/set_resources.png differ diff --git a/readme-images/command.png b/readme-images/set_role_to_user.png similarity index 100% rename from readme-images/command.png rename to readme-images/set_role_to_user.png