Skip to content

fix missing global context checks #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/utils/auth/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getCacheItem, IRestOptions, setCacheItem } from "../../exports-index";
import { isNullOrEmptyString, isNullOrUndefined, isNumber } from "../../helpers/typecheckers";
import { SPFxAuthToken, SPFxAuthTokenType } from "../../types/auth";
import { GetJson, GetJsonSync } from "../rest";
import { GetRestBaseUrl } from "../sharepoint.rest/common";
import { GetRestBaseUrl, hasGlobalContext } from "../sharepoint.rest/common";

export function GetTokenAudiencePrefix(appId: string) {
return `api://${appId}`;
Expand Down Expand Up @@ -66,7 +66,7 @@ function _getGetSPFxClientAuthTokenParams(siteUrl: string, spfxTokenType: SPFxAu
}

function _parseAndCacheGetSPFxClientAuthTokenResult(result: SPFxAuthToken, spfxTokenType: SPFxAuthTokenType = SPFxAuthTokenType.Graph) {
if (!isNullOrUndefined(result) && !isNullOrEmptyString(result.access_token)) {
if (hasGlobalContext() && !isNullOrUndefined(result) && !isNullOrEmptyString(result.access_token)) {
let expiration = isNumber(result.expires_on) ?
new Date(result.expires_on * 1000) :
{
Expand All @@ -81,9 +81,11 @@ function _parseAndCacheGetSPFxClientAuthTokenResult(result: SPFxAuthToken, spfxT
}

function _getSPFxClientAuthTokenFromCache(spfxTokenType: SPFxAuthTokenType = SPFxAuthTokenType.Graph) {
let cachedToken = getCacheItem<string>(`access_token_${spfxTokenType}_${_spPageContextInfo.webId}`);
if (!isNullOrEmptyString(cachedToken)) {
return cachedToken;
if (hasGlobalContext()) {
let cachedToken = getCacheItem<string>(`access_token_${spfxTokenType}_${_spPageContextInfo.webId}`);
if (!isNullOrEmptyString(cachedToken)) {
return cachedToken;
}
}
return null;
}
Expand Down
11 changes: 7 additions & 4 deletions src/utils/sharepoint.rest/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import { DateOrNull } from "../../types/common.types";
import { toIsoDateFormat } from "../date";
import { LocaleKnownScript } from "../knownscript";
import { SPServerLocalTimeToUTCSync } from "./web";
import { hasGlobalContext } from "./common";

function _SPServerLocalTimeToUTC(dateValueStr: string) {
//yyyy-MM-ddTHH:mm or SPServerLocalTime (5/27/2021 11:34) to UTC
let utcTimeValue = SPServerLocalTimeToUTCSync(_spPageContextInfo.webServerRelativeUrl, dateValueStr);
if (hasGlobalContext()) {
//yyyy-MM-ddTHH:mm or SPServerLocalTime (5/27/2021 11:34) to UTC
let utcTimeValue = SPServerLocalTimeToUTCSync(_spPageContextInfo.webServerRelativeUrl, dateValueStr);

if (!isNullOrEmptyString(utcTimeValue)) {
return new Date(utcTimeValue);
if (!isNullOrEmptyString(utcTimeValue)) {
return new Date(utcTimeValue);
}
}
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/sharepoint.rest/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { IRestItem } from "../../types/sharepoint.utils.types";
import { LocaleKnownScript } from "../../utils/knownscript";
import { ConsoleLogger } from "../consolelogger";
import { GetJson, GetJsonSync } from "../rest";
import { GetFieldNameFromRawValues, GetSiteUrl, __getSPRestErrorData, getFieldNameForUpdate } from "./common";
import { GetFieldNameFromRawValues, GetSiteUrl, __getSPRestErrorData, getFieldNameForUpdate, hasGlobalContext } from "./common";
import { GetList, GetListFields, GetListFieldsAsHash, GetListRestUrl } from "./list";
import { GetUser, GetUserSync } from "./user";

Expand Down Expand Up @@ -423,7 +423,7 @@ export function GetSPFieldValueAsText(value: any, field: IFieldInfoEX): string[]
rawValues.forEach(raw => {
if (isNullOrEmptyString(raw)) {/** skip */ }
else if (isNumber(raw))
if (isUser) {
if (isUser && hasGlobalContext()) {
//todo - try not sync...
try {
let userInfo = GetUserSync(_spPageContextInfo.siteServerRelativeUrl, raw);
Expand Down
9 changes: 6 additions & 3 deletions src/utils/sharepoint.rest/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ export function GetTenantId() {

/** Get tenant id lower case no {} */
export function GetPortalUrl() {
return _spPageContextInfo.portalUrl;
if (hasGlobalContext()) {
return _spPageContextInfo.portalUrl;
}
return null;
}

/** Get site id lower case no {} */
Expand Down Expand Up @@ -478,7 +481,7 @@ export async function GetAllSubWebs(siteUrl: string, options?: { allSiteCollecti

if (queryFailed) {
// Igor: Issue #7702
if (_spPageContextInfo && _spPageContextInfo.siteServerRelativeUrl.toLowerCase() !== siteUrl.toLowerCase()) {
if (hasGlobalContext() && _spPageContextInfo && _spPageContextInfo.siteServerRelativeUrl.toLowerCase() !== siteUrl.toLowerCase()) {
//siteUrl = _spPageContextInfo.siteServerRelativeUrl;
//currentSite = await GetWebInfo(siteUrl);
//Kevin: Issue 1028
Expand Down Expand Up @@ -937,7 +940,7 @@ export async function SPServerLocalTime(siteUrl: string) {

export function GetContextWebInformationSync(siteUrl: string): IContextWebInformation {
var siteId: string = null;
if (_spPageContextInfo && _spPageContextInfo.isAppWeb) {
if (hasGlobalContext() && _spPageContextInfo && _spPageContextInfo.isAppWeb) {
//inside an app web you can't get the contextinfo for any other site
siteUrl = _spPageContextInfo.webServerRelativeUrl;
siteId = _spPageContextInfo.siteId;
Expand Down