Skip to content

Commit 4281729

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

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

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

Lines changed: 27 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,25 @@ export class CoreAuthenticatedSite extends CoreUnauthenticatedSite {
16171622
CoreEvents.trigger(eventName, data);
16181623
}
16191624

1625+
/**
1626+
* @inheritdoc
1627+
*/
1628+
protected shouldApplyWSOverride(method: string, data: unknown, patch: CoreWSOverride): boolean {
1629+
if (!Number(patch.userid)) {
1630+
return true;
1631+
}
1632+
1633+
const info = this.infos ?? (method === 'core_webservice_get_site_info' ? (data as CoreSiteInfoResponse) : undefined);
1634+
1635+
if (!info?.userid) {
1636+
// Strange case, when doing WS calls the site should always have the userid already.
1637+
// Apply the patch to match the behaviour of unauthenticated site.
1638+
return true;
1639+
}
1640+
1641+
return Number(patch.userid) === info.userid;
1642+
}
1643+
16201644
}
16211645

16221646
/**

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

Lines changed: 41 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,36 @@ 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(method, data, patch)) {
507+
this.logger.warn('Patch ignored, conditions not fulfilled:', method, 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 method WS method name.
526+
* @param data Data returned by the WS.
527+
* @param patch Patch to check.
528+
* @returns Whether it should be applied.
529+
*/
530+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
531+
protected shouldApplyWSOverride(method: string, data: unknown, patch: CoreWSOverride): boolean {
532+
// Always apply patches for unauthenticated sites since we don't have user info.
533+
// If the pacth for an AJAX WebService contains an userid is probably by mistake.
534+
return true;
503535
}
504536

505537
}
@@ -653,3 +685,10 @@ export enum TypeOfLogin {
653685
BROWSER = 2, // SSO in browser window is required.
654686
EMBEDDED = 3, // SSO in embedded browser is required.
655687
}
688+
689+
/**
690+
* WebService override patch.
691+
*/
692+
export type CoreWSOverride = JsonPatchOperation & {
693+
userid?: number; // To apply the patch only if the current user matches this userid.
694+
};

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)