Skip to content

Commit 32645c1

Browse files
authored
user mode and API Update (#124)
* user mode and API Update Signed-off-by: Piotr PG Gajek <[email protected]> * documentation update Signed-off-by: Piotr PG Gajek <[email protected]> --------- Signed-off-by: Piotr PG Gajek <[email protected]>
1 parent 6094715 commit 32645c1

File tree

9 files changed

+71
-8
lines changed

9 files changed

+71
-8
lines changed

force-app/main/default/classes/SOQL.cls

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public virtual inherited sharing class SOQL implements Queryable {
132132
Queryable forUpdate();
133133
Queryable allRows();
134134
// FIELD-LEVEL SECURITY
135+
Queryable userMode();
135136
Queryable systemMode();
136137
Queryable stripInaccessible();
137138
Queryable stripInaccessible(AccessType accessType);
@@ -661,6 +662,11 @@ public virtual inherited sharing class SOQL implements Queryable {
661662
return this;
662663
}
663664

665+
public SOQL userMode() {
666+
executor.userMode();
667+
return this;
668+
}
669+
664670
public SOQL systemMode() {
665671
executor.systemMode();
666672
return this;
@@ -1808,8 +1814,8 @@ public virtual inherited sharing class SOQL implements Queryable {
18081814
}
18091815

18101816
private inherited sharing class Executor {
1811-
private AccessLevel accessMode = AccessLevel.USER_MODE; // The object permissions, field-level security, sharing rules are enforced.
1812-
private DatabaseQuery sharingExecutor = new InheritedSharing();
1817+
private DatabaseQuery sharingExecutor;
1818+
private AccessLevel accessMode;
18131819
private AccessType accessType;
18141820
private String mockId;
18151821
private String ofObject;
@@ -1818,6 +1824,13 @@ public virtual inherited sharing class SOQL implements Queryable {
18181824
public Executor(String ofObject, QueryBuilder builder) {
18191825
this.ofObject = ofObject;
18201826
this.builder = builder;
1827+
1828+
userMode();
1829+
inheritedSharing();
1830+
}
1831+
1832+
public void inheritedSharing() {
1833+
sharingExecutor = new InheritedSharing();
18211834
}
18221835

18231836
public void withSharing() {
@@ -1832,6 +1845,10 @@ public virtual inherited sharing class SOQL implements Queryable {
18321845
accessType = type;
18331846
}
18341847

1848+
public void userMode() { // The object permissions, field-level security, sharing rules are enforced.
1849+
accessMode = AccessLevel.USER_MODE;
1850+
}
1851+
18351852
public void systemMode() { // The object permissions, field-level permissions are ignored, sharing rules are controlled by the sharingMode.
18361853
accessMode = AccessLevel.SYSTEM_MODE;
18371854
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3-
<apiVersion>60.0</apiVersion>
3+
<apiVersion>61.0</apiVersion>
44
<status>Active</status>
55
</ApexClass>

force-app/main/default/classes/SOQL_Test.cls

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,29 @@ private class SOQL_Test {
22232223
}
22242224
}
22252225

2226+
@IsTest
2227+
static void userMode() {
2228+
// Setup
2229+
insert new Task(Subject = 'Test', Type = 'Other');
2230+
2231+
System.runAs(minimumAccessUser()) {
2232+
// Test
2233+
Exception queryException = null;
2234+
2235+
try {
2236+
Task task = (Task) SOQL.of(Task.SObjectType)
2237+
.with(Task.Type)
2238+
.userMode()
2239+
.toObject();
2240+
} catch(Exception e) {
2241+
queryException = e;
2242+
}
2243+
2244+
// Verify
2245+
Assert.isTrue(queryException.getMessage().contains('No such column \'Type\' on entity \'Task\'.'));
2246+
}
2247+
}
2248+
22262249
@IsTest
22272250
static void stripInaccessibleToObject() {
22282251
// Setup
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3-
<apiVersion>60.0</apiVersion>
3+
<apiVersion>61.0</apiVersion>
44
<status>Active</status>
55
</ApexClass>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3-
<apiVersion>60.0</apiVersion>
3+
<apiVersion>61.0</apiVersion>
44
<status>Active</status>
55
</ApexClass>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3-
<apiVersion>60.0</apiVersion>
3+
<apiVersion>61.0</apiVersion>
44
<status>Active</status>
55
</ApexClass>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3-
<apiVersion>60.0</apiVersion>
3+
<apiVersion>61.0</apiVersion>
44
<status>Active</status>
55
</ApexClass>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3-
<apiVersion>60.0</apiVersion>
3+
<apiVersion>61.0</apiVersion>
44
<status>Active</status>
55
</ApexClass>

website/docs/api/soql.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ The following are methods for `SOQL`.
130130

131131
[**FIELD-LEVEL SECURITY**](#field-level-security)
132132

133+
- [`userMode()`](#userMode)
133134
- [`systemMode()`](#systemmode)
134135
- [`stripInaccessible()`](#stripinaccessible)
135136
- [`stripInaccessible(AccessType accessType)`](#stripinaccessible)
@@ -1682,6 +1683,28 @@ By default AccessLevel is set as `USER_MODE`.
16821683

16831684
More details you can find in [here](../advanced-usage/fls.md)
16841685

1686+
### userMode
1687+
1688+
By default, all queries are executed `WITH USER_MODE`. However, developers can override this. For more details, check [Field-Level Security](../advanced-usage/fls.md) and [Sharing Rules](../advanced-usage/sharing.md).
1689+
1690+
The `userMode` method can be useful to override the `systemMode()` provided by the selector.
1691+
1692+
> Execution mode in which the object permissions, field-level security, and sharing rules of the current user are enforced.
1693+
1694+
**Signature**
1695+
1696+
```apex
1697+
Queryable userMode()
1698+
```
1699+
1700+
**Example**
1701+
1702+
```apex
1703+
SOQL.of(Account.SObjectType)
1704+
.userMode()
1705+
.toList();
1706+
```
1707+
16851708
### systemMode
16861709

16871710
> Execution mode in which the the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords.

0 commit comments

Comments
 (0)