MOBILE-3947 core: Stop using deprecated guard interfaces

main
Dani Palou 2023-11-29 11:43:31 +01:00
parent 2d141bc104
commit 30cac7d056
22 changed files with 119 additions and 196 deletions

View File

@ -12,39 +12,23 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, UrlTree } from '@angular/router';
import { ActivatedRouteSnapshot, CanActivateFn } from '@angular/router';
import { Router } from '@singletons';
import { AddonMessagesMainMenuHandlerService } from '../services/handlers/mainmenu';
import { AddonMessages } from '../services/messages';
/**
* Guard to redirect to the right page based on the current Moodle site version.
*
* @returns Route.
*/
@Injectable({ providedIn: 'root' })
export class AddonMessagesIndexGuard implements CanActivate {
export const messagesIndexGuard: CanActivateFn = async (route: ActivatedRouteSnapshot) => {
const enabled = AddonMessages.isGroupMessagingEnabled();
const path = `/main/${AddonMessagesMainMenuHandlerService.PAGE_NAME}/` + ( enabled ? 'group-conversations' : 'index');
/**
* @inheritdoc
*/
canActivate(route: ActivatedRouteSnapshot): UrlTree {
return this.guard(route);
}
const newRoute = Router.parseUrl(path);
/**
* Check if there is a pending redirect and trigger it.
*
* @returns The redirection route.
*/
private guard(route: ActivatedRouteSnapshot): UrlTree {
const enabled = AddonMessages.isGroupMessagingEnabled();
const path = `/main/${AddonMessagesMainMenuHandlerService.PAGE_NAME}/` + ( enabled ? 'group-conversations' : 'index');
newRoute.queryParams = route.queryParams;
const newRoute = Router.parseUrl(path);
newRoute.queryParams = route.queryParams;
return newRoute;
}
}
return newRoute;
};

View File

@ -28,7 +28,7 @@ import { CoreMainMenuComponentsModule } from '@features/mainmenu/components/comp
import { buildTabMainRoutes } from '@features/mainmenu/mainmenu-tab-routing.module';
import { CoreSearchComponentsModule } from '@features/search/components/components.module';
import { CoreScreen } from '@services/screen';
import { AddonMessagesIndexGuard } from './guards';
import { messagesIndexGuard } from './guards';
/**
* Build module routes.
@ -120,7 +120,7 @@ function buildRoutes(injector: Injector): Routes {
loadChildren: () => import('./messages-settings-lazy.module').then(m => m.AddonMessagesSettingsLazyModule),
},
...buildTabMainRoutes(injector, {
canActivate: [AddonMessagesIndexGuard],
canActivate: [messagesIndexGuard],
}),
];
}

View File

@ -16,7 +16,7 @@ import { conditionalRoutes } from '@/app/app-routing.module';
import { CoreSharedModule } from '@/core/shared.module';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { CanLeaveGuard } from '@guards/can-leave';
import { canLeaveGuard } from '@guards/can-leave';
import { CoreScreen } from '@services/screen';
import { AddonModAssignComponentsModule } from './components/components.module';
import { AddonModAssignEditPage } from './pages/edit/edit';
@ -32,7 +32,7 @@ const commonRoutes: Routes = [
{
path: ':courseId/:cmId/edit',
component: AddonModAssignEditPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
];
@ -45,7 +45,7 @@ const mobileRoutes: Routes = [
{
path: ':courseId/:cmId/submission/:submitId',
component: AddonModAssignSubmissionReviewPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
];
@ -58,7 +58,7 @@ const tabletRoutes: Routes = [
{
path: ':submitId',
component: AddonModAssignSubmissionReviewPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
],
},

View File

@ -22,7 +22,7 @@ import { AddonModChatSessionMessagesPage } from './pages/session-messages/sessio
import { CoreScreen } from '@services/screen';
import { conditionalRoutes } from '@/app/app-routing.module';
import { AddonModChatSessionsPage } from './pages/sessions/sessions';
import { CanLeaveGuard } from '@guards/can-leave';
import { canLeaveGuard } from '@guards/can-leave';
const commonRoutes: Routes = [
{
@ -32,7 +32,7 @@ const commonRoutes: Routes = [
{
path: ':courseId/:cmId/chat',
component: AddonModChatChatPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
];

View File

@ -18,7 +18,7 @@ import { RouterModule, Routes } from '@angular/router';
import { CoreCommentsComponentsModule } from '@features/comments/components/components.module';
import { CoreCompileHtmlComponentModule } from '@features/compile/components/compile-html/compile-html.module';
import { CoreRatingComponentsModule } from '@features/rating/components/components.module';
import { CanLeaveGuard } from '@guards/can-leave';
import { canLeaveGuard } from '@guards/can-leave';
import { AddonModDataComponentsCompileModule } from './components/components-compile.module';
import { AddonModDataComponentsModule } from './components/components.module';
import { AddonModDataEditPage } from './pages/edit/edit';
@ -33,12 +33,12 @@ const routes: Routes = [
{
path: ':courseId/:cmId/edit',
component: AddonModDataEditPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
{
path: ':courseId/:cmId/edit/:entryId',
component: AddonModDataEditPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
{
path: ':courseId/:cmId/:entryId',

View File

@ -22,7 +22,7 @@ import { conditionalRoutes } from '@/app/app-routing.module';
import { CoreScreen } from '@services/screen';
import { AddonModFeedbackAttemptPage } from '@addons/mod/feedback/pages/attempt/attempt';
import { AddonModFeedbackFormPage } from '@addons/mod/feedback/pages/form/form';
import { CanLeaveGuard } from '@guards/can-leave';
import { canLeaveGuard } from '@guards/can-leave';
import { AddonModFeedbackNonRespondentsPage } from '@addons/mod/feedback/pages/nonrespondents/nonrespondents';
const commonRoutes: Routes = [
@ -33,7 +33,7 @@ const commonRoutes: Routes = [
{
path: ':courseId/:cmId/form',
component: AddonModFeedbackFormPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
{
path: ':courseId/:cmId/nonrespondents',

View File

@ -16,14 +16,14 @@ import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AddonModForumComponentsModule } from '@addons/mod/forum/components/components.module';
import { CanLeaveGuard } from '@guards/can-leave';
import { canLeaveGuard } from '@guards/can-leave';
import { CoreSharedModule } from '@/core/shared.module';
import { AddonModForumDiscussionPage } from '@addons/mod/forum/pages/discussion/discussion';
const routes: Routes = [{
path: '',
component: AddonModForumDiscussionPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
}];
@NgModule({

View File

@ -16,7 +16,7 @@ import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AddonModForumComponentsModule } from '@addons/mod/forum/components/components.module';
import { CanLeaveGuard } from '@guards/can-leave';
import { canLeaveGuard } from '@guards/can-leave';
import { CoreEditorComponentsModule } from '@features/editor/components/components.module';
import { CoreSharedModule } from '@/core/shared.module';
import { AddonModForumNewDiscussionPage } from '@addons/mod/forum/pages/new-discussion/new-discussion';
@ -24,7 +24,7 @@ import { AddonModForumNewDiscussionPage } from '@addons/mod/forum/pages/new-disc
const routes: Routes = [{
path: '',
component: AddonModForumNewDiscussionPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
}];
@NgModule({

View File

@ -16,13 +16,13 @@ import { NgModule } from '@angular/core';
import { CoreSharedModule } from '@/core/shared.module';
import { CoreEditorComponentsModule } from '@features/editor/components/components.module';
import { RouterModule, Routes } from '@angular/router';
import { CanLeaveGuard } from '@guards/can-leave';
import { canLeaveGuard } from '@guards/can-leave';
import { AddonModGlossaryEditPage } from '@addons/mod/glossary/pages/edit/edit';
const routes: Routes = [{
path: '',
component: AddonModGlossaryEditPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
}];
@NgModule({

View File

@ -16,7 +16,7 @@ import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { CoreSharedModule } from '@/core/shared.module';
import { CanLeaveGuard } from '@guards/can-leave';
import { canLeaveGuard } from '@guards/can-leave';
import { AddonModH5PActivityComponentsModule } from './components/components.module';
import { AddonModH5PActivityIndexPage } from './pages/index/index';
import { AddonModH5PActivityUserAttemptsPage } from '@addons/mod/h5pactivity/pages/user-attempts/user-attempts';
@ -27,7 +27,7 @@ const routes: Routes = [
{
path: ':courseId/:cmId',
component: AddonModH5PActivityIndexPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
{
path: ':courseId/:cmId/userattempts/:userId',

View File

@ -21,7 +21,7 @@ import { AddonModLessonComponentsModule } from './components/components.module';
import { AddonModLessonIndexPage } from './pages/index';
import { CoreEditorComponentsModule } from '@features/editor/components/components.module';
import { AddonModLessonPlayerPage } from '@addons/mod/lesson/pages/player/player';
import { CanLeaveGuard } from '@guards/can-leave';
import { canLeaveGuard } from '@guards/can-leave';
import { AddonModLessonUserRetakePage } from '@addons/mod/lesson/pages/user-retake/user-retake';
const routes: Routes = [
@ -32,7 +32,7 @@ const routes: Routes = [
{
path: ':courseId/:cmId/player',
component: AddonModLessonPlayerPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
{
path: ':courseId/:cmId/user-retake/:userId',

View File

@ -22,7 +22,7 @@ import { AddonModQuizIndexPage } from './pages/index';
import { AddonModQuizAttemptPage } from '@addons/mod/quiz/pages/attempt/attempt';
import { CoreQuestionComponentsModule } from '@features/question/components/components.module';
import { AddonModQuizPlayerPage } from '@addons/mod/quiz/pages/player/player';
import { CanLeaveGuard } from '@guards/can-leave';
import { canLeaveGuard } from '@guards/can-leave';
import { AddonModQuizReviewPage } from '@addons/mod/quiz/pages/review/review';
const routes: Routes = [
@ -33,7 +33,7 @@ const routes: Routes = [
{
path: ':courseId/:cmId/player',
component: AddonModQuizPlayerPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
{
path: ':courseId/:cmId/attempt/:attemptId',

View File

@ -18,7 +18,7 @@ import { CoreSharedModule } from '@/core/shared.module';
import { AddonModWikiComponentsModule } from './components/components.module';
import { AddonModWikiIndexPage } from './pages/index/index';
import { CoreEditorComponentsModule } from '@features/editor/components/components.module';
import { CanLeaveGuard } from '@guards/can-leave';
import { canLeaveGuard } from '@guards/can-leave';
import { AddonModWikiEditPage } from './pages/edit/edit';
const routes: Routes = [
@ -33,7 +33,7 @@ const routes: Routes = [
{
path: ':courseId/:cmId/edit',
component: AddonModWikiEditPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
];

View File

@ -14,7 +14,7 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { CanLeaveGuard } from '@guards/can-leave';
import { canLeaveGuard } from '@guards/can-leave';
import { CoreSharedModule } from '@/core/shared.module';
import { AddonModWorkshopIndexPage } from './pages/index/index';
@ -32,17 +32,17 @@ const routes: Routes = [
{
path: ':courseId/:cmId/:submissionId',
component: AddonModWorkshopSubmissionPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
{
path: ':courseId/:cmId/:submissionId/edit',
component: AddonModWorkshopEditSubmissionPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
{
path: ':courseId/:cmId/:submissionId/:assessmentId',
component: AddonModWorkshopAssessmentPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
];

View File

@ -12,50 +12,28 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Injectable } from '@angular/core';
import { CanActivate, CanLoad, UrlTree } from '@angular/router';
import { CanActivateFn } from '@angular/router';
import { CoreSites } from '@services/sites';
import { CoreUtils } from '@services/utils/utils';
import { Router } from '@singletons';
import { CoreLoginHelper } from '../services/login-helper';
@Injectable({ providedIn: 'root' })
export class CoreLoginHasSitesGuard implements CanActivate, CanLoad {
/**
* Guard to check if the user has any sites stored.
*
* @returns True if user has sites, redirect route otherwise.
*/
export const hasSitesGuard: CanActivateFn = async () => {
const sites = await CoreUtils.ignoreErrors(CoreSites.getSites(), []);
/**
* @inheritdoc
*/
canActivate(): Promise<true | UrlTree> {
return this.guard();
if (sites.length > 0) {
return true;
}
/**
* @inheritdoc
*/
canLoad(): Promise<true | UrlTree> {
return this.guard();
}
const [path, params] = await CoreLoginHelper.getAddSiteRouteInfo();
const route = Router.parseUrl(path);
/**
* Check if the user has any sites stored.
*
* @returns Promise resolved with true if it's not redirected or the redirection route.
*/
private async guard(): Promise<true | UrlTree> {
const sites = await CoreUtils.ignoreErrors(CoreSites.getSites(), []);
route.queryParams = params;
if (sites.length > 0) {
return true;
}
const [path, params] = await CoreLoginHelper.getAddSiteRouteInfo();
const route = Router.parseUrl(path);
route.queryParams = params;
return route;
}
}
return route;
};

View File

@ -16,7 +16,7 @@ import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { CoreSharedModule } from '@/core/shared.module';
import { CoreLoginHasSitesGuard } from './guards/has-sites';
import { hasSitesGuard } from './guards/has-sites';
import { CoreLoginComponentsModule } from './components/components.module';
import { CoreLoginHelper } from './services/login-helper';
import { CoreLoginForgottenPasswordPage } from '@features/login/pages/forgotten-password/forgotten-password';
@ -44,8 +44,7 @@ const routes: Routes = [
{
path: 'sites',
component: CoreLoginSitesPage,
canLoad: [CoreLoginHasSitesGuard],
canActivate: [CoreLoginHasSitesGuard],
canActivate: [hasSitesGuard],
},
{
path: 'forgottenpassword',

View File

@ -17,7 +17,7 @@ import { Routes } from '@angular/router';
import { AppRoutingModule } from '@/app/app-routing.module';
import { CoreLoginHelper, CoreLoginHelperProvider } from './services/login-helper';
import { CoreRedirectGuard } from '@guards/redirect';
import { redirectGuard } from '@guards/redirect';
import { CoreLoginCronHandler } from './services/handlers/cron';
import { CoreCronDelegate } from '@services/cron';
import { CoreEvents } from '@singletons/events';
@ -30,8 +30,7 @@ const appRoutes: Routes = [
{
path: 'login',
loadChildren: () => import('./login-lazy.module').then(m => m.CoreLoginLazyModule),
canActivate: [CoreRedirectGuard],
canLoad: [CoreRedirectGuard],
canActivate: [redirectGuard],
},
];

View File

@ -12,61 +12,41 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Injectable } from '@angular/core';
import { CanLoad, CanActivate, UrlTree } from '@angular/router';
import { CanActivateFn } from '@angular/router';
import { CoreLoginHelper } from '@features/login/services/login-helper';
import { CoreApp } from '@services/app';
import { CoreSites } from '@services/sites';
import { Router } from '@singletons';
@Injectable({ providedIn: 'root' })
export class CoreMainMenuAuthGuard implements CanLoad, CanActivate {
/**
* @inheritdoc
*/
canActivate(): Promise<true | UrlTree> {
return this.guard();
/**
* Guard to check if the user is authenticated.
*
* @returns True if user has sites, redirect route otherwise.
*/
export const authGuard: CanActivateFn = async () => {
if (!CoreSites.isLoggedIn()) {
return Router.parseUrl('/login');
}
/**
* @inheritdoc
*/
canLoad(): Promise<true | UrlTree> {
return this.guard();
}
if (CoreLoginHelper.isSiteLoggedOut()) {
// Send the user to reconnect page.
const newRoute = Router.parseUrl('/login/reconnect');
const siteId = CoreSites.getCurrentSiteId();
/**
* Check if the current user should be redirected to the authentication page.
*
* @returns Promise resolved with true if it's not redirected or the redirection route.
*/
private async guard(): Promise<true | UrlTree> {
if (!CoreSites.isLoggedIn()) {
return Router.parseUrl('/login');
// Pass redirect data (if any and belongs to same site).
let redirect = CoreApp.consumeMemoryRedirect();
if (redirect?.siteId !== siteId) {
redirect = null;
}
if (CoreLoginHelper.isSiteLoggedOut()) {
// Send the user to reconnect page.
const newRoute = Router.parseUrl('/login/reconnect');
const siteId = CoreSites.getCurrentSiteId();
newRoute.queryParams = {
siteId,
...redirect,
};
// Pass redirect data (if any and belongs to same site).
let redirect = CoreApp.consumeMemoryRedirect();
if (redirect?.siteId !== siteId) {
redirect = null;
}
newRoute.queryParams = {
siteId,
...redirect,
};
return newRoute;
}
return true;
return newRoute;
}
}
return true;
};

View File

@ -14,7 +14,7 @@
import { APP_INITIALIZER, NgModule } from '@angular/core';
import { Routes } from '@angular/router';
import { CoreMainMenuAuthGuard } from '@features/mainmenu/guards/auth';
import { authGuard } from '@features/mainmenu/guards/auth';
import { AppRoutingModule } from '@/app/app-routing.module';
@ -38,8 +38,7 @@ const appRoutes: Routes = [
{
path: 'main',
loadChildren: () => import('./mainmenu-lazy.module').then(m => m.CoreMainMenuLazyModule),
canActivate: [CoreMainMenuAuthGuard],
canLoad: [CoreMainMenuAuthGuard],
canActivate: [authGuard],
},
{
path: 'reload',

View File

@ -23,7 +23,7 @@ import { CoreSitePluginsComponentsModule } from './components/components.module'
import { CoreSitePluginsHelper } from './services/siteplugins-helper';
import { CoreSharedModule } from '@/core/shared.module';
import { CoreSitePluginsPluginPage } from '@features/siteplugins/pages/plugin/plugin';
import { CanLeaveGuard } from '@guards/can-leave';
import { canLeaveGuard } from '@guards/can-leave';
import { CoreSitePluginsCourseOptionPage } from '@features/siteplugins/pages/course-option/course-option';
import { CoreSitePluginsModuleIndexPage } from '@features/siteplugins/pages/module-index/module-index';
@ -31,7 +31,7 @@ const routes: Routes = [
{
path: 'siteplugins/content/:component/:method/:hash',
component: CoreSitePluginsPluginPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
];
@ -39,7 +39,7 @@ const homeRoutes: Routes = [
{
path: 'siteplugins/homecontent/:component/:method',
component: CoreSitePluginsPluginPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
];
@ -47,7 +47,7 @@ const courseIndexRoutes: Routes = [
{
path: 'siteplugins/:handlerUniqueName',
component: CoreSitePluginsCourseOptionPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
];
@ -55,7 +55,7 @@ const moduleRoutes: Routes = [
{
path: 'siteplugins/module/:courseId/:cmId',
component: CoreSitePluginsModuleIndexPage,
canDeactivate: [CanLeaveGuard],
canDeactivate: [canLeaveGuard],
},
];

View File

@ -12,26 +12,30 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Injectable } from '@angular/core';
import { CanDeactivate } from '@angular/router';
import { CanDeactivateFn } from '@angular/router';
import { CoreUtils } from '@services/utils/utils';
@Injectable({ providedIn: 'root' })
export class CanLeaveGuard implements CanDeactivate<unknown> {
/**
* Check if a component implements the canLeave interface.
*
* @param component Component instance to check.
* @returns Whether it implements CanLeave interface.
*/
const isCanLeave = (component: unknown | null): component is CanLeave =>
component !== null && 'canLeave' in <CanLeave> component;
async canDeactivate(component: unknown | null): Promise<boolean> {
if (!this.isCanLeave(component)) {
return true;
}
return CoreUtils.ignoreErrors(component.canLeave(), false);
/**
* Guard to check if the user can leave a page.
*
* @returns True if user has sites, redirect route otherwise.
*/
export const canLeaveGuard: CanDeactivateFn<unknown> = async (component: unknown) => {
if (!isCanLeave(component)) {
return true;
}
isCanLeave(component: unknown | null): component is CanLeave {
return component !== null && 'canLeave' in <CanLeave> component;
}
}
return CoreUtils.ignoreErrors(component.canLeave(), false);
};
export interface CanLeave {
/**

View File

@ -12,38 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Injectable } from '@angular/core';
import { CanActivate, CanLoad, UrlTree } from '@angular/router';
import { CanActivateFn } from '@angular/router';
import { CoreApp } from '@services/app';
import { CoreRedirectPayload } from '@services/navigator';
import { CoreSites } from '@services/sites';
import { Router } from '@singletons';
import { CoreConstants } from '../constants';
@Injectable({ providedIn: 'root' })
export class CoreRedirectGuard implements CanLoad, CanActivate {
/**
* @inheritdoc
*/
canLoad(): Promise<true | UrlTree> {
return this.guard();
}
/**
* @inheritdoc
*/
canActivate(): Promise<true | UrlTree> {
return this.guard();
}
/**
* Check if there is a pending redirect and trigger it.
*
* @returns Promise resolved with true if it's not redirected or the redirection route.
*/
private async guard(): Promise<true | UrlTree> {
const redirect = CoreApp.consumeMemoryRedirect();
/**
* Guard to check if there is a pending redirect and trigger it.
*
* @returns True if there's no redirect, redirection route otherwise.
*/
export const redirectGuard: CanActivateFn = async () => {
const redirect = CoreApp.consumeMemoryRedirect();
if (!redirect) {
return true;
}
@ -81,6 +63,4 @@ export class CoreRedirectGuard implements CanLoad, CanActivate {
route.queryParams = redirect.redirectOptions?.params || {};
return route;
}
}
};