Check User Permissions for Logged-In User in Lightning Web Component

Advertisements

Last Updated on July 7, 2022 by Rakesh Gupta

Big Idea or Enduring Question:

  • How to check whether the current user has specific permission (RunReports) or not to customize a component’s behavior?

Objectives:

After reading this blog, you’ll be able to:

  • Check standard user permission for the current user
  • Check custom permission for the current user 
  • Customize the component’s behavior based on the current user’s permission
  • and much more

In the past written a few articles on Lightning Web Component. Why not check them out while you are at it?!

  1. Add Lightning Web Components in Mobile and Lightning Experience as Tabs
  2. Create a Form with a Progress Bar in Lightning Web Component

Business Use case

Keegan Watson is working as a Junior Developer at Gurukul on Cloud (GoC). Now she wants to learn how to check whether the current user has RunReports permission or not. 

Check Permissions

Permissions are the best way to control access and behavior in a Salesforce. When developing Lightning web components, you can customize a component’s behavior based on whether the current user has specific permission or not. 

To check whether a user has a permission, import Salesforce permissions from the @salesforce/userPermission and @salesforce/customPermission scoped modules and evaluate whether it’s true or undefined. Then if the user has the permission, the component can take a specific action.


//to check standard permission

import hasPermission from '@salesforce/userPermission/PermissionName';

//to check custom permission
import hasCustomPermission from '@salesforce/customPermission/Custom_Permission_Api_Name';

Orgs use namespaces as unique identifiers for their customization and packages. Custom permissions can include a namespace. If the custom permission was installed from a managed package, prepend the namespace followed by __ to the permission name.

Automation Champion Approach (I-do):

Now it’s time to create a lightning web component to programmatically check RunReports permission in lwc controller. 

userPermissionCheck.js-meta.xml

A lightning web component can be used to build custom pages for Lightning Experience and the Salesforce mobile app quickly with point-and-click tools. Make sure to add the right target for it.

This configuration file makes the component available for all Lightning page types but restricts support on desktops.


<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>55.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordPage</target>
        <target>lightning__AppPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>

userPermissionCheck.html

A getter is a function that computes a value for a property. If the user has the RunReports standard permission, then the  component will display User has Permission to Run Reports message. 


<!-- userPermissionCheck.html -->
<template>
    <lightning-card title="User Permisison Check Example">
    <template if:true={isRunReport}>
        <h2>User has Permission to Run Reports.</h2>
    </template>

    <template if:false={isRunReport}>
        <h2>User Does Not have Permission to Run Reports.</h2>
    </template>
</lightning-card>
</template>

userPermissionCheck.js

This sample checks whether the current user has theRunReports standard permission.


// userPermissionCheck.js
import { LightningElement } from 'lwc';
import hasRunReports from '@salesforce/userPermission/RunReports';

export default class PermissionCheck extends LightningElement {
    get isRunReport() {
        return hasRunReports;
    }
}

Proof of Concept

Formative Assessment:

I want to hear from you!

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.

Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!

Preferred Timing(required)

2 thoughts on “Check User Permissions for Logged-In User in Lightning Web Component

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Automation Champion

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version
%%footer%%