Skip to content

Commit 15fd1b8

Browse files
committed
MOBILE-4919 site: Allow apply WS override only for certain user
1 parent 3b30586 commit 15fd1b8

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed

src/core/classes/sites/authenticated-site.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ import { Observable, ObservableInput, ObservedValueOf, OperatorFunction, Subject
3737
import { finalize, map, mergeMap } from 'rxjs/operators';
3838
import { CoreSiteError } from '@classes/errors/siteerror';
3939
import { CoreUserAuthenticatedSupportConfig } from '@features/user/classes/support/authenticated-support-config';
40-
import { CoreSiteInfo, CoreSiteInfoResponse, CoreSitePublicConfigResponse, CoreUnauthenticatedSite } from './unauthenticated-site';
40+
import {
41+
CoreSiteInfo,
42+
CoreSiteInfoResponse,
43+
CoreSitePublicConfigResponse,
44+
CoreUnauthenticatedSite,
45+
CoreWSOverride,
46+
} from './unauthenticated-site';
4147
import { Md5 } from 'ts-md5';
4248
import { CoreSiteWSCacheRecord } from '@services/database/sites';
4349
import { CoreErrorLogs } from '@singletons/error-logs';
@@ -100,7 +106,7 @@ export class CoreAuthenticatedSite extends CoreUnauthenticatedSite {
100106
privateToken?: string;
101107
infos?: CoreSiteInfo;
102108

103-
protected logger: CoreLogger;
109+
protected logger = CoreLogger.getInstance('CoreAuthenticatedSite');
104110
protected cleanUnicode = false;
105111
protected offlineDisabled = false;
106112
private memoryCache: Record<string, CoreSiteWSCacheRecord> = {};
@@ -124,7 +130,6 @@ export class CoreAuthenticatedSite extends CoreUnauthenticatedSite {
124130
) {
125131
super(siteUrl, otherData.publicConfig);
126132

127-
this.logger = CoreLogger.getInstance('CoreAuthenticaedSite');
128133
this.token = token;
129134
this.privateToken = otherData.privateToken;
130135
}
@@ -1617,6 +1622,23 @@ export class CoreAuthenticatedSite extends CoreUnauthenticatedSite {
16171622
CoreEvents.trigger(eventName, data);
16181623
}
16191624

1625+
/**
1626+
* @inheritdoc
1627+
*/
1628+
protected shouldApplyWSOverride(patch: CoreWSOverride): boolean {
1629+
if (!Number(patch.userid)) {
1630+
return true;
1631+
}
1632+
1633+
if (!this.infos?.userid) {
1634+
// Strange case, when doing WS calls the site should always have the userid already.
1635+
// Apply the patch to match the behaviour of unauthenticated site.
1636+
return true;
1637+
}
1638+
1639+
return Number(patch.userid) === this.infos.userid;
1640+
}
1641+
16201642
}
16211643

16221644
/**

src/core/classes/sites/unauthenticated-site.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ import { CoreText } from '@singletons/text';
2020
import { CoreUrl, CoreUrlPartNames } from '@singletons/url';
2121
import { CoreWS, CoreWSAjaxPreSets, CoreWSExternalWarning } from '@services/ws';
2222
import { CorePath } from '@singletons/path';
23-
import { CoreJsonPatch } from '@singletons/json-patch';
23+
import { CoreJsonPatch, JsonPatchOperation } from '@singletons/json-patch';
2424
import { CoreUtils } from '@singletons/utils';
25+
import { CoreLogger } from '@singletons/logger';
2526

2627
/**
2728
* Class that represents a Moodle site where the user still hasn't authenticated.
2829
*/
2930
export class CoreUnauthenticatedSite {
3031

32+
protected logger = CoreLogger.getInstance('CoreUnauthenticatedSite');
33+
3134
siteUrl: string;
3235

3336
protected publicConfig?: CoreSitePublicConfigResponse;
@@ -499,7 +502,34 @@ export class CoreUnauthenticatedSite {
499502
return data;
500503
}
501504

502-
return CoreJsonPatch.applyPatches(data, CoreConstants.CONFIG.wsOverrides[method]);
505+
CoreConstants.CONFIG.wsOverrides[method].forEach((patch) => {
506+
if (!this.shouldApplyWSOverride(patch)) {
507+
this.logger.warn('Patch ignored, conditions not fulfilled:', patch);
508+
509+
return;
510+
}
511+
512+
try {
513+
CoreJsonPatch.applyPatch(data, patch);
514+
} catch (error) {
515+
this.logger.error('Error applying WS override:', error, patch);
516+
}
517+
});
518+
519+
return data;
520+
}
521+
522+
/**
523+
* Whether a patch should be applied as a WS override.
524+
*
525+
* @param patch Patch to check.
526+
* @returns Whether it should be applied.
527+
*/
528+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
529+
protected shouldApplyWSOverride(patch: CoreWSOverride): boolean {
530+
// Always apply patches for unauthenticated sites since we don't have user info.
531+
// If the pacth for an AJAX WebService contains an userid is probably by mistake.
532+
return true;
503533
}
504534

505535
}
@@ -653,3 +683,10 @@ export enum TypeOfLogin {
653683
BROWSER = 2, // SSO in browser window is required.
654684
EMBEDDED = 3, // SSO in embedded browser is required.
655685
}
686+
687+
/**
688+
* WebService override patch.
689+
*/
690+
export type CoreWSOverride = JsonPatchOperation & {
691+
userid?: number; // To apply the patch only if the current user matches this userid.
692+
};

src/types/config.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { OpenFileAction } from '@singletons/opener';
1919
import { CoreLoginSiteFinderSettings, CoreLoginSiteSelectorListMethod } from '@features/login/services/login-helper';
2020
import { CoreDatabaseConfiguration } from '@classes/database/database-table';
2121
import { ToastDuration } from '@services/overlays/toasts';
22-
import { JsonPatchOperation } from '@singletons/json-patch';
22+
import { CoreWSOverride } from '@classes/sites/unauthenticated-site';
2323

2424
/* eslint-disable @typescript-eslint/naming-convention */
2525

@@ -81,5 +81,5 @@ export interface EnvironmentConfig {
8181
clearIABSessionWhenAutoLogin?: 'android' | 'ios' | 'all'; // Clear the session every time a new IAB is opened with auto-login.
8282
disabledFeatures?: string; // Disabled features for the whole app, using the same format as tool_mobile_disabledfeatures.
8383
collapsibleItemsExpanded: boolean; // Expand or collapse the collapsible items by default.
84-
wsOverrides: Record<string, JsonPatchOperation[]>; // Overrides to apply to WS calls.
84+
wsOverrides: Record<string, CoreWSOverride[]>; // Overrides to apply to WS calls.
8585
}

0 commit comments

Comments
 (0)