How to get Role Permissions by Role Id in Magento 2?

How to get Role Permissions by Role Id in Magento 2?

In this blog, I’ll walk you through a simple method for retrieving role permissions in Magento 2 based on role id using the Magento\Company\Model\Authorization\PermissionProvider class.

Managing user roles and permissions is crucial for controlling access to different features in any eCommerce platform. Whether you are running a B2B store or managing multiple departments within your organization, knowing how to retrieve role permissions is a key part of your administrative toolkit.

Why Role Permissions Matter in Magento 2

Role-based permissions allow you to control what each user can and cannot do in your Magento store. For instance, you may have different roles like Administrator, Customer Support, Sales Manager, or custom roles for B2B customers. Managing these permissions ensures that each role has the appropriate level of access, enhancing both security and operational efficiency.

Magento 2 provides several built-in tools for handling user permissions, and one of the key components used in B2B setups is the Magento\Company\Model\Authorization\PermissionProvider class from the Magento_Company module.

Let’s check a quick example below.

Example

The method getRolePermissionsByRoleId(6) fetches permissions for the role with the ID 6. This role ID is linked to a specific user role in your Magento 2 store, and the method returns all the permissions assigned to that role.

Output

The output will be something like this,

Following this guide, you can easily implement the code and start controlling what each role can access in your Magento 2 store.

Happy B2B 🙂

You may also like,

Allow Additional Permissions in Company User Roles by default in Magento 2 B2B

how to get shared catalog data by ID in Magento 2?

Get Company using Customer ID programmatically Magento 2 B2B

Get Company Admin using company ID programmatically Magento 2 B2B

Leave a Reply

Your email address will not be published. Required fields are marked *