Hello friends, today we will discuss Check User Permissions for Current User in LWC. User-specific actions, visibility, or data access often depend on their permissions. Checking these permissions dynamically enhances security and user experience. Now we can directly check these permissions in LWC, no need to call the Apex server call.
Also, check this: Show Toast Message in Lightning Flows Salesforce
Key Highlights :
- Security: Ensure users only access what they’re authorized to, safeguarding sensitive data and actions.
- User-Centric: Provide a tailored experience, showing or hiding functionality based on user roles and permissions.
- Efficiency: Streamline processes by dynamically enabling or disabling features as needed.
- We can check custom permission and standard permission for the current user.
- Can Modify the component’s behavior based on the current user’s permission
- import hasPermission from ‘@salesforce/userPermission/PermissionName’;
Code :
Using the @salesforce/userPermission and @salesforce/customPermission scoped modules, you can check whether a user has permissions. A specific action can be taken by the component if the user has permission.
//check standard permission import hasPermission from '@salesforce/userPermission/PermissionName'; //to check custom permission import hasCustomPermission from '@salesforce/customPermission/Custom_Permission_Api_Name';
checkPermission.Html:
<template> <setup-panel-group> <setup-button disabled={isSetupEnabled} onclick={openSetup}></setup-button> </setup-panel-group> <template lwc:if={isReportVisible}> <c-expense-report></c-expense-report> </template> </template>
checkPermission.JS:
import { LightningElement } from 'lwc'; import hasViewSetup from '@salesforce/userPermission/ViewSetup'; import hasViewReport from "@salesforce/customPermission/ViewReport"; export default class App extends LightingElement { get isSetupEnabled() { return hasViewSetup; } get isReportVisible() { return hasViewReport; } openSetup(e) { } }
checkPermission.js-meta.xml:
<?xml version="1.0"?> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>57.0</apiVersion> <isExposed>true</isExposed> <targets> <target>lightning__HomePage</target> </targets> </LightningComponentBundle>