어느 Salesforce Developer의 개발 성장기

Apex 키워드: with sharing, without sharing, and inherited sharing 의 차이에 대해 알아보자 본문

Salesforce/Learning concept

Apex 키워드: with sharing, without sharing, and inherited sharing 의 차이에 대해 알아보자

Developer_Foryou 2020. 3. 9. 20:07

with sharing

  • 사용자에 대한 공유 규칙이 클래스에 고려되도록 지정할 수 있다.
  • Apex Code가 System Context에서 실행되므로 클래스에 대해 이 키워드를 명시적으로 설정해야한다.
    • System Context에서 Apex Code는 개체에 대한 사용 권한, 필드 수준 보안, 공유 규칙이 현재 사용자에게 적용되지 않은 모든 개체 및 피드에 대한 권한을 가진다.
    • with sharing 키워드를 활용하여 사용자의 공유 규칙에 따라 코드가 실행되지 않도록 한다.
  • 예외:
    • Developer Console에서 Execute Anonymous Windows에서 Apex Class 실행
public with sharing class sharingClass {
    
    // Code here
    
}

without sharing

  • 사용자에 대한 공유 규칙이 적용되지 않도록 설정.
  • without sharing 키워드를 사용하여 사용자의 공유 규칙을 해제하여 실행할 수 있음.
public without sharing class noSharing {
    
    // Code here
    
}

 

Implementation Details About with sharing and without sharing Keywords

  • 메소드가 정의된 클래스의 공유 설정이 적용되며 메소드가 호출되는 클래스에는 적용되지 않는다.
    • 예를 들어, with sharing class에 의해 호출된 without sharing class의 메서드의 경우, 이 메서드는 공유 규칙을 적용하여 실행된다.
  • 클래스가 with sharing 또는 without sharing이 선언되지 않으면 현재 공유 규칙이 계속 적용된다. 따라서 클래스는 다른 클래스에서 공유 규칙을 가져 오는 경우를 제외하고 공유 규칙을 적용하지 않는다.
    • 예를 들어 class가 with sharing class로 선언된 다른 클래스에 의해 호출되면 with sharing class로 설정된다.
  • Inner class와 Outer class는 모두 with sharing class로 선언 될 수 있다.
    • 공유 설정은 초기화 코드, 생성자 및 메서드를 포함하여 클래스에 포함 된 모든 코드에 적용된다.
  • Inner class는 container class에서 공유설정을 상속하지 않는다.
  • 클래스는 한 클래스가 다른 클래스를 상속(extends)하거나 인터페이스(implements)를 구현할 때, 공유 설정을 상위 클래스에서 상속받는다.

 

Inherited Sharing

sharing 선언이 없는 Apex는 기본적으로 안전하지 않다. runtime에서 with sharing 또는 without sharing 모드로 실행할 수 있는 Apex class를 디자인 하는 것은 고급 기술이다. 이러한 기술은 실수로 특정 공유 선언이 누락 된 기술과 구별하기가 어려울 수 있다. Inherited sharing 선언은 모호성을 피하면서 의도를 명확하게 만든다.
Inherited sharing을 사용하면 AppExchange Security Review를 통과하고 권한이 부여된 Apex Code가 예상치 못한 방식이나 안전하지 않은 방식으로 사용되지 않도록 할 수 있다.

  • An Aura component controller
  • A Visualforce controller
  • An Apex REST service
  • Any other entry point to an Apex transaction
public inherited sharing class InheritedSharingClass{
    public List<Contact> getAllTheSecrets(){
        return [SELECT Name FROM Contact];
    }
}
<apex:page controller="InheritedSharingClass">
    <apex:repeat value="{!allTheSecrets}" var="record">
        {!record.Name}
    </apex:repeat>
</apex:page>

 

Comments