Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. <?php
  2. namespace ModuleNameSpaceYourModuleNameSetup;
  3.  
  4. use MagentoFrameworkSetupInstallDataInterface;
  5. use MagentoFrameworkSetupModuleContextInterface;
  6. use MagentoFrameworkSetupModuleDataSetupInterface;
  7.  
  8. /* For get RoleType and UserType for create Role */;
  9. use MagentoAuthorizationModelAclRoleGroup as RoleGroup;
  10. use MagentoAuthorizationModelUserContextInterface;
  11. /** * @codeCoverageIgnore */
  12.  
  13. class InstallData implements InstallDataInterface {
  14.  
  15. /** * RoleFactory * * @var roleFactory */
  16. private $roleFactory;
  17.  
  18. /** * RulesFactory * * @var rulesFactory */
  19. private $rulesFactory;
  20.  
  21. /** * Init * * @param MagentoAuthorizationModelRoleFactory $roleFactory * @param MagentoAuthorizationModelRulesFactory $rulesFactory */
  22.  
  23. public function __construct(
  24. MagentoAuthorizationModelRoleFactory $roleFactory, /* Instance of Role*/
  25. MagentoAuthorizationModelRulesFactory $rulesFactory /* Instance of Rule */
  26. /*this define that which resource permitted to wich role */ ) {
  27. $this->roleFactory = $roleFactory;
  28. $this->rulesFactory = $rulesFactory;
  29. }
  30.  
  31. /** * {@inheritdoc} * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */
  32.  
  33. public function install(
  34. ModuleDataSetupInterface $setup,
  35. ModuleContextInterface $context) {
  36. /** * Create Warehouse role */
  37. $role=$this->roleFactory->create();
  38. $role->setName('YourRoleName') //Set Role Name Which you want to create
  39.  
  40. ->setPid(0) //set parent role id of your role
  41. ->setRoleType(RoleGroup::ROLE_TYPE)
  42. ->setUserType(UserContextInterface::USER_TYPE_ADMIN);
  43.  
  44. $role->save();
  45. /* Now we set that which resources we allow to this role */ $resource=[
  46. 'Magento_Backend::admin',
  47. 'Magento_Sales::sales'
  48. ];
  49.  
  50. /* Array of resource ids which we want to allow this role*/
  51. $this->rulesFactory->create()->setRoleId($role->getId())->setResources($resource)->saveRel();
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement