@@ -20,14 +20,17 @@ import { CoreText } from '@singletons/text';
2020import { CoreUrl , CoreUrlPartNames } from '@singletons/url' ;
2121import { CoreWS , CoreWSAjaxPreSets , CoreWSExternalWarning } from '@services/ws' ;
2222import { CorePath } from '@singletons/path' ;
23- import { CoreJsonPatch } from '@singletons/json-patch' ;
23+ import { CoreJsonPatch , JsonPatchOperation } from '@singletons/json-patch' ;
2424import { 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 */
2930export 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+ } ;
0 commit comments