Skip to content

Commit

Permalink
- Fixed #1769. Updated the token logic for {Site} and {User} tokens. …
Browse files Browse the repository at this point in the history
…If the token property is not found in the current context (client side resolution) then the value is let untouched for server side processing as a query variable + restricted manual character escape to curly braces only. (#1784)
  • Loading branch information
FranckyC committed Feb 1, 2022
1 parent 894e73a commit 0d55fe4
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions search-parts/src/services/tokenService/TokenService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ export class TokenService implements ITokenService {
// The 'OR/AND' operator should be called after all tokens are processed (works with comma delimited values potentially coming from resolved)
inputString = this.replaceAndOrOperator(inputString);

// Replace manually escaped characters
inputString = inputString.replace(/\\(.)/gi, '$1');
// Replace manually escaped curly braces
inputString = inputString.replace(/\\({|})/gi, '$1');
}

return inputString;
Expand Down Expand Up @@ -349,38 +349,39 @@ export class TokenService implements ITokenService {
while (matches !== null) {

let userProperty = matches[1].toLowerCase();
let propertyValue = null;

// {User.Audiences} is resolved server side by SharePoint
if (!/^(audiences)$/gi.test(userProperty)) {
// Check if other user profile properties have to be retrieved
if (!/^(name|email)$/gi.test(userProperty)) {

let propertyValue = null;

// Check if other user profile properties have to be retrieved
if (!/^(name|email)$/gi.test(userProperty)) {

// Check if the user profile api was already called
if (!this.userProperties) {
this.userProperties = await this.getUserProfileProperties();
}
// Check if the user profile api was already called
if (!this.userProperties) {
this.userProperties = await this.getUserProfileProperties();
}

propertyValue = this.userProperties[userProperty] ? this.userProperties[userProperty] : '';
// Need to enclose with quotes because of dash separated values (ex: "sps-interests")
propertyValue = ObjectHelper.byPath(this.userProperties, `"${userProperty}"`);

} else {
} else {

switch (userProperty) {
switch (userProperty) {

case "email":
propertyValue = this.pageContext.user.email;
break;
case "email":
propertyValue = this.pageContext.user.email;
break;

case "name":
propertyValue = this.pageContext.user.displayName;
break;
default:
propertyValue = this.pageContext.user.displayName;
break;
}
case "name":
propertyValue = this.pageContext.user.displayName;
break;
default:
propertyValue = this.pageContext.user.displayName;
break;
}
}

// If value not found in the fetched properties, let the value untouched to be resolved server side by a query variable
// Ex: {User.Audiences} or {User.PreferredDisplayLanguage}
if (propertyValue !== undefined) {

const tokenExprToReplace = new RegExp(matches[0], 'gi');

Expand Down Expand Up @@ -500,7 +501,15 @@ export class TokenService implements ITokenService {

while (matches !== null) {
const siteProp = matches[1];
inputString = inputString.replace(new RegExp(matches[0], "gi"), this.pageContext.site ? ObjectHelper.byPath(this.pageContext.site, siteProp) : '');

// Ensure the property is in the page context first.
// If not, let the value untouched as it could be a query variable instead processed server side (ex: {Site.URL}
const sitePropertyValue = ObjectHelper.byPath(this.pageContext.site, siteProp);

if (sitePropertyValue) {
inputString = inputString.replace(new RegExp(matches[0], "gi"), this.pageContext.site ? sitePropertyValue : '');
}

matches = siteRegExp.exec(inputString);
}
}
Expand Down

0 comments on commit 0d55fe4

Please sign in to comment.