1

I want to create a new admin role with some rights.

So I use the following code :

    $role = $this->roleFactory->create();
    $role->setName('MyNewRole')
        ->setPid(0)
        ->setRoleType(RoleGroup::ROLE_TYPE)
        ->setUserType(UserContextInterface::USER_TYPE_ADMIN);
    $role->save();

    $resource = [
        'Magento_Sales::sales'
    ];
    $this->rulesFactory->create()
        ->setRoleId($role->getId())
        ->setResources($resource)
        ->saveRel();

This code works. However, I would like all the subcategories of Sales, not only "Magento_Sales::sales".

For that, I know I can put the listing of all subcategories (for example : Magento_Sales::create Magento_Sales::invoice Magento_Sales::reorder ...)

But it's very long: there are 47

Does someone know how to do? Something like "Magento_Sales::*" or a recursive method maybe?

4
  • See how Magento reads acl.xml to built the tree in admin. From there you can deduce the resources you need. Jan 21, 2020 at 12:15
  • A know the resource I need (there are 47). Is there a way to make some recursive code instead of list all 47 resources ?
    – Kozame
    Jan 21, 2020 at 12:28
  • What is the reason to create it using code? Posible with admin panel. Jan 21, 2020 at 13:16
  • I have a lot of roles to create and I want to create them by Setup to have the code on all my environments.
    – Kozame
    Jan 21, 2020 at 13:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.