Skip to content

Commit bc7d159

Browse files
authored
Add UserSettings global variable resolution. (#207)
1 parent 6448f13 commit bc7d159

14 files changed

+166
-3
lines changed

docs/src/app/docs/referencing-org-data/page.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Object result = expression.Evaluator.run('$Resource.MyStaticResourceName');
4747
You can reference information about the current user through the `$User` global variable.
4848

4949
Available references are:
50+
5051
* `DefaultCurrency`
5152
* `FirstName`
5253
* `Language`
@@ -59,3 +60,26 @@ Available references are:
5960
```apex
6061
Object result = expression.Evaluator.run('$User.FirstName');
6162
```
63+
64+
## User Settings
65+
66+
You can reference user settings using the `$UserSettings` global variable.
67+
68+
This global variable wraps all properties of the `ConnectApi.UserSettings` class,
69+
so please reference
70+
the [documentation for that class](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_connectapi_output_usersettings.htm)
71+
for available properties.
72+
73+
```apex
74+
Object result = expression.Evaluator.run('$UserSettings.hasChatter');
75+
```
76+
77+
### Time Zone
78+
79+
The `timeZone` property of the `$UserSettings` global variable returns a map containing
80+
`name` and `gmtOffset` properties.
81+
82+
```apex
83+
Object result = expression.Evaluator.run('$UserSettings.timeZone.name');
84+
Object gmtOffset = expression.Evaluator.run('$UserSettings.timeZone.gmtOffset');
85+
```

expression-src/main/src/interpreter/Environment.cls

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public with sharing class Environment {
2222
GLOBAL_VARIABLES.put('$resource', new StaticResourceVariableResolver());
2323
GLOBAL_VARIABLES.put('$action', new ActionVariableResolver());
2424
GLOBAL_VARIABLES.put('$user', new UserVariableResolver());
25+
GLOBAL_VARIABLES.put('$usersettings', new UserSettingsVariableResolver());
2526

2627
// Placeholder value for whatever the called Action returned. Not used by language,
2728
// but can be used by the caller to get the return value of the Action.
@@ -46,11 +47,11 @@ public with sharing class Environment {
4647
// First try and find it locally
4748
Object possibleFunction = getVariables().get(lowercasedName);
4849
if (possibleFunction != null && possibleFunction instanceof Expr.FunctionDeclaration) {
49-
return (Expr.FunctionDeclaration) possibleFunction;
50+
return (Expr.FunctionDeclaration)possibleFunction;
5051
}
5152

5253
// Otherwise, look in the global variables
53-
return (Expr.FunctionDeclaration) GLOBAL_VARIABLES.get(functionName.toLowerCase());
54+
return (Expr.FunctionDeclaration)GLOBAL_VARIABLES.get(functionName.toLowerCase());
5455
}
5556

5657
/**
@@ -167,7 +168,7 @@ public with sharing class Environment {
167168
}
168169

169170
public Boolean equals(Object obj) {
170-
Environment other = (Environment) obj;
171+
Environment other = (Environment)obj;
171172
return parentEnvironment == other.parentEnvironment && context == other.context && variables.equals(other.variables);
172173
}
173174

expression-src/main/src/interpreter/CustomLabelVariableResolver.cls renamed to expression-src/main/src/interpreter/variable-resolvers/CustomLabelVariableResolver.cls

File renamed without changes.

expression-src/main/src/interpreter/CustomLabelVariableResolver.cls-meta.xml renamed to expression-src/main/src/interpreter/variable-resolvers/CustomLabelVariableResolver.cls-meta.xml

File renamed without changes.

expression-src/main/src/interpreter/CustomMetadataVariableResolver.cls renamed to expression-src/main/src/interpreter/variable-resolvers/CustomMetadataVariableResolver.cls

File renamed without changes.

expression-src/main/src/interpreter/CustomMetadataVariableResolver.cls-meta.xml renamed to expression-src/main/src/interpreter/variable-resolvers/CustomMetadataVariableResolver.cls-meta.xml

File renamed without changes.

expression-src/main/src/interpreter/IGlobalVariableResolver.cls renamed to expression-src/main/src/interpreter/variable-resolvers/IGlobalVariableResolver.cls

File renamed without changes.

expression-src/main/src/interpreter/IGlobalVariableResolver.cls-meta.xml renamed to expression-src/main/src/interpreter/variable-resolvers/IGlobalVariableResolver.cls-meta.xml

File renamed without changes.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
public with sharing class UserSettingsVariableResolver implements IGlobalVariableResolver {
2+
public Object get(String referenceName, List<Object> args) {
3+
switch on referenceName.toLowerCase() {
4+
when 'approvalposts' {
5+
return getUserSettings().approvalPosts;
6+
}
7+
when 'canAccesspersonalstreams' {
8+
return getUserSettings().canAccessPersonalStreams;
9+
}
10+
when 'canfollow' {
11+
return getUserSettings().canFollow;
12+
}
13+
when 'canmodifyallData' {
14+
return getUserSettings().canModifyAllData;
15+
}
16+
when 'canowngroups' {
17+
return getUserSettings().canOwnGroups;
18+
}
19+
when 'canviewalldata' {
20+
return getUserSettings().canViewAllData;
21+
}
22+
when 'canviewallgroups' {
23+
return getUserSettings().canViewAllGroups;
24+
}
25+
when 'canviewallusers' {
26+
return getUserSettings().canViewAllUsers;
27+
}
28+
when 'canviewcommunityswitcher' {
29+
return getUserSettings().canViewCommunitySwitcher;
30+
}
31+
when 'canviewfulluserprofile' {
32+
return getUserSettings().canViewFullUserProfile;
33+
}
34+
when 'canviewpublicfiles' {
35+
return getUserSettings().canViewPublicFiles;
36+
}
37+
when 'currencysymbol' {
38+
return getUserSettings().currencySymbol;
39+
}
40+
when 'externaluser' {
41+
return getUserSettings().externalUser;
42+
}
43+
when 'filesynclimit' {
44+
return getUserSettings().fileSyncLimit;
45+
}
46+
when 'filesyncstoragelimit' {
47+
return getUserSettings().fileSyncStorageLimit;
48+
}
49+
when 'foldersynclimit' {
50+
return getUserSettings().folderSyncLimit;
51+
}
52+
when 'hasaccesstointernalorg' {
53+
return getUserSettings().hasAccessToInternalOrg;
54+
}
55+
when 'haschatter' {
56+
return getUserSettings().hasChatter;
57+
}
58+
when 'hasfilesync' {
59+
return getUserSettings().hasFileSync;
60+
}
61+
when 'hasfieldservicelocationtracking' {
62+
return getUserSettings().hasFieldServiceLocationTracking;
63+
}
64+
when 'hasfieldservicemobileaccess' {
65+
return getUserSettings().hasFieldServiceMobileAccess;
66+
}
67+
when 'hasfilesyncmanagedclientautoupdate' {
68+
return getUserSettings().hasFileSyncManagedClientAutoUpdate;
69+
}
70+
when 'hasrestdataapiaccess' {
71+
return getUserSettings().hasRestDataApiAccess;
72+
}
73+
when 'timezone' {
74+
ConnectApi.TimeZone timeZone = getUserSettings().timeZone;
75+
return new Map<String, Object> {
76+
'name' => timeZone.name,
77+
'gmtOffset' => timeZone.gmtOffset
78+
};
79+
}
80+
when 'userdefaultcurrencyisocode' {
81+
return getUserSettings().userDefaultCurrencyIsoCode;
82+
}
83+
when 'userid' {
84+
return getUserSettings().userId;
85+
}
86+
when 'userlocale' {
87+
return getUserSettings().userLocale;
88+
}
89+
when else {
90+
throw new UserSettingsVariableResolverException('Unknown reference name: ' + referenceName);
91+
}
92+
}
93+
}
94+
95+
private static ConnectApi.UserSettings getUserSettings() {
96+
return ConnectApi.Organization.getSettings().userSettings;
97+
}
98+
99+
public class UserSettingsVariableResolverException extends Exception {}
100+
}

expression-src/main/src/interpreter/UserVariableResolver.cls-meta.xml renamed to expression-src/main/src/interpreter/variable-resolvers/UserSettingsVariableResolver.cls-meta.xml

File renamed without changes.

0 commit comments

Comments
 (0)