Per my understanding of With Sharing, Without Sharing and non-sharing-specified Classes in Apex, I would like to jot down the following notes and leave certain blanks for not sure answers; I wanted to know if I were correct/not and clear/not with the following notes.
1) With Sharing keyword implies in another words, "with Security Settings enforced". This again, pertains to only respecting OWDs and Sharing Rules [ What about Object Level Security/OLS and Field Level Security/FLS? ]
2) If a class is declared as "with sharing" then, the sharing settings apply to all code contained in the class, including initialization code, constructors and methods. However, Inner Classes DO NOT inherit sharing settings from the container class.
3) If a class is not declared either with sharing or without sharing, then by default, such a class is executed in system mode, i.e. without sharing mode; and current sharing rules remain in effect- which means that if any other class that has sharing enforced, calls such a non-specified-sharing class, then the called or non-specified-sharing class is executed in "with sharing" mode.
4) If class B extends class A, then all code in class B inherits the same sharing settings that class A has. However, class B's inner classes do not inherit class B's sharing settings.
5) If a with sharing class calls a without sharing class, then the called method(s) will be executed in the mode of the class in which they were defined, in this case, the called method(s) will execute in without sharing mode.
I have written a few example case scenarios, as below, that would summarize the understanding of the above concepts, I would like to learn if I am correct, also willing to know expert advice / suggestions w.r.t each of the below scenarios.
Say I have 3 classes:
- public with sharing class A {}
- public without sharing class B{}
- public class C{} // Class C is a non-specified-sharing class.
Now, let's consider the following case scenarios:
class B extends A // class B's code now executes in class A's mode, i.e. in "with sharing" mode.
class B calls class A // called code will now be executed in the mode of the class in which it was defined, in this case, in class A's mode, i.e. in "with sharing" mode.
class C calls class A // called method's code in class A will execute in class A's mode, i.e. in "with sharing" mode.
class C extends class A // code in class C now executes in the parent class A's mode, i.e. in "with sharing" mode.
class A calls C // code in class C is executed in class C's mode, i.e. in "without sharing" mode although it's sharing settings are unspecified in the class' declaration.
class A extends C // class A's code now executes in the parent class C's mode, i.e. in "without sharing" mode.
I'm not quite sure of the case 6. Besides, I also wanted to know the use cases for which the with sharing and without sharing rules are applied and used. How are the sharing settings applicable in case of Visualforce Controller Classes and/or Controller Extensions.