Achieve different sharing modes (with sharing and without sharing) for authenticated and unauthenticated users when calling an Apex method

 To achieve different sharing modes (with sharing and without sharing) for authenticated and unauthenticated users when calling an Apex method, you can leverage a combination of Apex classes and conditional logic to determine the user's authentication status.

Steps to Achieve This:

  1. Create Two Apex Classes:

    • One with with sharing
    • One with without sharing
  2. Use a Wrapper Class:

    • Create a wrapper class or a method that checks if the user is authenticated or not and calls the appropriate class.

Example Implementation:

Step 1: Create Two Apex Classes

Class with with sharing:

apex

public with sharing class SharedService { @AuraEnabled public static void performAction() { // Your logic here } }

Class with without sharing:

apex

public without sharing class UnsharedService { @AuraEnabled public static void performAction() { // Your logic here } }

Step 2: Wrapper Class to Determine User Type

Wrapper Class:

apex

public class ServiceWrapper { @AuraEnabled public static void performAction() { if (isAuthenticatedUser()) { SharedService.performAction(); } else { UnsharedService.performAction(); } } private static Boolean isAuthenticatedUser() { // Check the user authentication status // You can use UserInfo class to check if the user is authenticated return UserInfo.getUserType() != 'Guest'; } }

Explanation:

  1. Separate Classes:

    • SharedService and UnsharedService are the two Apex classes. One operates in the with sharing mode, and the other operates in the without sharing mode.
  2. Wrapper Class:

    • ServiceWrapper class contains the performAction method that checks if the current user is authenticated using UserInfo.getUserType().
    • If the user is authenticated (UserInfo.getUserType() != 'Guest'), it calls the method in the SharedService class.
    • If the user is unauthenticated, it calls the method in the UnsharedService class.
  3. User Authentication Check:

    • UserInfo.getUserType() returns the type of user. For guest users, it returns Guest.

Calling the Wrapper Class from LWC:

When you call the Apex method from your Lightning Web Component (LWC), you should call the method in the ServiceWrapper class:

javascript

import { LightningElement, track, wire } from 'lwc'; import performAction from '@salesforce/apex/ServiceWrapper.performAction'; export default class MyComponent extends LightningElement { handleAction() { performAction() .then(result => { // Handle success }) .catch(error => { // Handle error }); } }

By using this approach, you ensure that authenticated users are subjected to sharing rules, while unauthenticated users bypass them, achieving the desired behavior based on user authentication status.

No comments:

Post a Comment

Async/Await Concept in Javascript/LWC

  Concept of async and await in JavaScript async and await are used in asynchronous programming in JavaScript. They help us write clean...