2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2018-02-27 17:06:06 -05:00
|
|
|
import {Observable} from 'rxjs';
|
2016-07-26 17:39:02 -04:00
|
|
|
|
|
|
|
import {Route} from './config';
|
2016-06-02 18:29:15 -04:00
|
|
|
import {ActivatedRouteSnapshot, RouterStateSnapshot} from './router_state';
|
|
|
|
|
2016-07-26 17:39:02 -04:00
|
|
|
|
2016-06-02 18:29:15 -04:00
|
|
|
/**
|
2018-04-05 06:38:57 -04:00
|
|
|
* @description
|
2016-06-28 17:49:29 -04:00
|
|
|
*
|
2018-04-05 06:51:21 -04:00
|
|
|
* Interface that a class can implement to be a guard deciding if a route can be activated.
|
|
|
|
*
|
2016-06-28 17:49:29 -04:00
|
|
|
* ```
|
2016-08-30 18:57:24 -04:00
|
|
|
* class UserToken {}
|
|
|
|
* class Permissions {
|
|
|
|
* canActivate(user: UserToken, id: string): boolean {
|
|
|
|
* return true;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2016-07-13 10:27:32 -04:00
|
|
|
* @Injectable()
|
2016-06-28 17:49:29 -04:00
|
|
|
* class CanActivateTeam implements CanActivate {
|
|
|
|
* constructor(private permissions: Permissions, private currentUser: UserToken) {}
|
|
|
|
*
|
2016-08-19 18:48:09 -04:00
|
|
|
* canActivate(
|
|
|
|
* route: ActivatedRouteSnapshot,
|
|
|
|
* state: RouterStateSnapshot
|
|
|
|
* ): Observable<boolean>|Promise<boolean>|boolean {
|
|
|
|
* return this.permissions.canActivate(this.currentUser, route.params.id);
|
2016-06-28 17:49:29 -04:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2016-08-19 18:48:09 -04:00
|
|
|
* @NgModule({
|
|
|
|
* imports: [
|
|
|
|
* RouterModule.forRoot([
|
|
|
|
* {
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: TeamCmp,
|
|
|
|
* canActivate: [CanActivateTeam]
|
|
|
|
* }
|
|
|
|
* ])
|
|
|
|
* ],
|
2016-08-30 18:57:24 -04:00
|
|
|
* providers: [CanActivateTeam, UserToken, Permissions]
|
2016-08-19 18:48:09 -04:00
|
|
|
* })
|
2016-08-30 18:57:24 -04:00
|
|
|
* class AppModule {}
|
2016-06-28 17:49:29 -04:00
|
|
|
* ```
|
|
|
|
*
|
2017-02-16 13:26:53 -05:00
|
|
|
* You can alternatively provide a function with the `canActivate` signature:
|
2016-06-28 17:49:29 -04:00
|
|
|
*
|
|
|
|
* ```
|
2016-08-19 18:48:09 -04:00
|
|
|
* @NgModule({
|
|
|
|
* imports: [
|
|
|
|
* RouterModule.forRoot([
|
|
|
|
* {
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: TeamCmp,
|
|
|
|
* canActivate: ['canActivateTeam']
|
|
|
|
* }
|
|
|
|
* ])
|
|
|
|
* ],
|
|
|
|
* providers: [
|
|
|
|
* {
|
|
|
|
* provide: 'canActivateTeam',
|
|
|
|
* useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => true
|
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* })
|
2016-08-30 18:57:24 -04:00
|
|
|
* class AppModule {}
|
2016-06-28 17:49:29 -04:00
|
|
|
* ```
|
|
|
|
*
|
2018-04-05 17:31:44 -04:00
|
|
|
*
|
2016-06-02 18:29:15 -04:00
|
|
|
*/
|
|
|
|
export interface CanActivate {
|
2016-06-08 14:13:41 -04:00
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
|
2016-07-13 21:12:59 -04:00
|
|
|
Observable<boolean>|Promise<boolean>|boolean;
|
2016-07-13 18:15:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-05 06:38:57 -04:00
|
|
|
* @description
|
2016-07-13 18:15:20 -04:00
|
|
|
*
|
2018-04-05 06:51:21 -04:00
|
|
|
* Interface that a class can implement to be a guard deciding if a child route can be activated.
|
|
|
|
*
|
2016-07-13 18:15:20 -04:00
|
|
|
* ```
|
2016-08-30 18:57:24 -04:00
|
|
|
* class UserToken {}
|
|
|
|
* class Permissions {
|
|
|
|
* canActivate(user: UserToken, id: string): boolean {
|
|
|
|
* return true;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2016-07-13 18:15:20 -04:00
|
|
|
* @Injectable()
|
2016-10-11 18:47:57 -04:00
|
|
|
* class CanActivateTeam implements CanActivateChild {
|
2016-07-13 18:15:20 -04:00
|
|
|
* constructor(private permissions: Permissions, private currentUser: UserToken) {}
|
|
|
|
*
|
2016-08-19 18:48:09 -04:00
|
|
|
* canActivateChild(
|
|
|
|
* route: ActivatedRouteSnapshot,
|
|
|
|
* state: RouterStateSnapshot
|
|
|
|
* ): Observable<boolean>|Promise<boolean>|boolean {
|
2016-07-26 17:39:02 -04:00
|
|
|
* return this.permissions.canActivate(this.currentUser, route.params.id);
|
2016-07-13 18:15:20 -04:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2016-08-19 18:48:09 -04:00
|
|
|
* @NgModule({
|
|
|
|
* imports: [
|
|
|
|
* RouterModule.forRoot([
|
|
|
|
* {
|
|
|
|
* path: 'root',
|
|
|
|
* canActivateChild: [CanActivateTeam],
|
|
|
|
* children: [
|
|
|
|
* {
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: Team
|
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* }
|
|
|
|
* ])
|
|
|
|
* ],
|
2016-08-30 18:57:24 -04:00
|
|
|
* providers: [CanActivateTeam, UserToken, Permissions]
|
2016-08-19 18:48:09 -04:00
|
|
|
* })
|
2016-08-30 18:57:24 -04:00
|
|
|
* class AppModule {}
|
2016-07-13 18:15:20 -04:00
|
|
|
* ```
|
|
|
|
*
|
2017-02-16 13:26:53 -05:00
|
|
|
* You can alternatively provide a function with the `canActivateChild` signature:
|
2016-07-13 18:15:20 -04:00
|
|
|
*
|
|
|
|
* ```
|
2016-08-19 18:48:09 -04:00
|
|
|
* @NgModule({
|
|
|
|
* imports: [
|
|
|
|
* RouterModule.forRoot([
|
|
|
|
* {
|
|
|
|
* path: 'root',
|
|
|
|
* canActivateChild: ['canActivateTeam'],
|
|
|
|
* children: [
|
|
|
|
* {
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: Team
|
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* }
|
|
|
|
* ])
|
|
|
|
* ],
|
|
|
|
* providers: [
|
2016-07-13 18:15:20 -04:00
|
|
|
* {
|
2016-08-19 18:48:09 -04:00
|
|
|
* provide: 'canActivateTeam',
|
|
|
|
* useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => true
|
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* })
|
2016-08-30 18:57:24 -04:00
|
|
|
* class AppModule {}
|
2016-07-13 18:15:20 -04:00
|
|
|
* ```
|
|
|
|
*
|
2018-04-05 17:31:44 -04:00
|
|
|
*
|
2016-07-13 18:15:20 -04:00
|
|
|
*/
|
|
|
|
export interface CanActivateChild {
|
|
|
|
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot):
|
2016-07-13 21:12:59 -04:00
|
|
|
Observable<boolean>|Promise<boolean>|boolean;
|
2016-06-02 18:29:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-05 06:38:57 -04:00
|
|
|
* @description
|
2016-06-28 17:49:29 -04:00
|
|
|
*
|
2018-04-05 06:51:21 -04:00
|
|
|
* Interface that a class can implement to be a guard deciding if a route can be deactivated.
|
|
|
|
*
|
2016-06-28 17:49:29 -04:00
|
|
|
* ```
|
2016-08-30 18:57:24 -04:00
|
|
|
* class UserToken {}
|
|
|
|
* class Permissions {
|
|
|
|
* canDeactivate(user: UserToken, id: string): boolean {
|
|
|
|
* return true;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2016-07-13 10:27:32 -04:00
|
|
|
* @Injectable()
|
2016-08-19 18:48:09 -04:00
|
|
|
* class CanDeactivateTeam implements CanDeactivate<TeamComponent> {
|
2016-06-28 17:49:29 -04:00
|
|
|
* constructor(private permissions: Permissions, private currentUser: UserToken) {}
|
|
|
|
*
|
2016-08-19 18:48:09 -04:00
|
|
|
* canDeactivate(
|
|
|
|
* component: TeamComponent,
|
2016-12-27 17:08:06 -05:00
|
|
|
* currentRoute: ActivatedRouteSnapshot,
|
|
|
|
* currentState: RouterStateSnapshot,
|
|
|
|
* nextState: RouterStateSnapshot
|
2016-08-19 18:48:09 -04:00
|
|
|
* ): Observable<boolean>|Promise<boolean>|boolean {
|
2016-07-26 17:39:02 -04:00
|
|
|
* return this.permissions.canDeactivate(this.currentUser, route.params.id);
|
2016-06-28 17:49:29 -04:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2016-08-19 18:48:09 -04:00
|
|
|
* @NgModule({
|
|
|
|
* imports: [
|
|
|
|
* RouterModule.forRoot([
|
|
|
|
* {
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: TeamCmp,
|
|
|
|
* canDeactivate: [CanDeactivateTeam]
|
|
|
|
* }
|
|
|
|
* ])
|
|
|
|
* ],
|
2016-08-30 18:57:24 -04:00
|
|
|
* providers: [CanDeactivateTeam, UserToken, Permissions]
|
2016-08-19 18:48:09 -04:00
|
|
|
* })
|
2016-08-30 18:57:24 -04:00
|
|
|
* class AppModule {}
|
2016-06-28 17:49:29 -04:00
|
|
|
* ```
|
|
|
|
*
|
2017-02-16 13:26:53 -05:00
|
|
|
* You can alternatively provide a function with the `canDeactivate` signature:
|
2016-06-28 17:49:29 -04:00
|
|
|
*
|
|
|
|
* ```
|
2016-08-19 18:48:09 -04:00
|
|
|
* @NgModule({
|
|
|
|
* imports: [
|
|
|
|
* RouterModule.forRoot([
|
|
|
|
* {
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: TeamCmp,
|
2017-01-20 17:19:23 -05:00
|
|
|
* canDeactivate: ['canDeactivateTeam']
|
2016-08-19 18:48:09 -04:00
|
|
|
* }
|
|
|
|
* ])
|
|
|
|
* ],
|
|
|
|
* providers: [
|
|
|
|
* {
|
|
|
|
* provide: 'canDeactivateTeam',
|
2016-12-27 17:08:06 -05:00
|
|
|
* useValue: (component: TeamComponent, currentRoute: ActivatedRouteSnapshot, currentState:
|
|
|
|
* RouterStateSnapshot, nextState: RouterStateSnapshot) => true
|
2016-08-19 18:48:09 -04:00
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* })
|
2016-08-30 18:57:24 -04:00
|
|
|
* class AppModule {}
|
2016-06-28 17:49:29 -04:00
|
|
|
* ```
|
|
|
|
*
|
2018-04-05 17:31:44 -04:00
|
|
|
*
|
2016-06-02 18:29:15 -04:00
|
|
|
*/
|
|
|
|
export interface CanDeactivate<T> {
|
2016-12-27 17:08:06 -05:00
|
|
|
canDeactivate(
|
|
|
|
component: T, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot,
|
|
|
|
nextState?: RouterStateSnapshot): Observable<boolean>|Promise<boolean>|boolean;
|
2016-06-27 17:00:07 -04:00
|
|
|
}
|
|
|
|
|
2016-06-27 15:27:23 -04:00
|
|
|
/**
|
2018-04-05 06:38:57 -04:00
|
|
|
* @description
|
2016-06-28 17:49:29 -04:00
|
|
|
*
|
2018-04-05 06:51:21 -04:00
|
|
|
* Interface that class can implement to be a data provider.
|
|
|
|
*
|
2016-06-28 17:49:29 -04:00
|
|
|
* ```
|
2016-08-30 18:57:24 -04:00
|
|
|
* class Backend {
|
|
|
|
* fetchTeam(id: string) {
|
|
|
|
* return 'someTeam';
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2016-07-13 10:27:32 -04:00
|
|
|
* @Injectable()
|
2016-08-19 18:48:09 -04:00
|
|
|
* class TeamResolver implements Resolve<Team> {
|
2016-06-28 17:49:29 -04:00
|
|
|
* constructor(private backend: Backend) {}
|
|
|
|
*
|
2016-08-19 18:48:09 -04:00
|
|
|
* resolve(
|
|
|
|
* route: ActivatedRouteSnapshot,
|
|
|
|
* state: RouterStateSnapshot
|
|
|
|
* ): Observable<any>|Promise<any>|any {
|
|
|
|
* return this.backend.fetchTeam(route.params.id);
|
2016-06-28 17:49:29 -04:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2016-08-19 18:48:09 -04:00
|
|
|
* @NgModule({
|
|
|
|
* imports: [
|
|
|
|
* RouterModule.forRoot([
|
|
|
|
* {
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: TeamCmp,
|
|
|
|
* resolve: {
|
|
|
|
* team: TeamResolver
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ])
|
|
|
|
* ],
|
|
|
|
* providers: [TeamResolver]
|
|
|
|
* })
|
2016-08-30 18:57:24 -04:00
|
|
|
* class AppModule {}
|
2016-06-28 17:49:29 -04:00
|
|
|
* ```
|
|
|
|
*
|
2017-02-16 13:26:53 -05:00
|
|
|
* You can alternatively provide a function with the `resolve` signature:
|
2016-06-28 17:49:29 -04:00
|
|
|
*
|
2016-08-19 18:48:09 -04:00
|
|
|
* ```
|
|
|
|
* @NgModule({
|
|
|
|
* imports: [
|
|
|
|
* RouterModule.forRoot([
|
|
|
|
* {
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: TeamCmp,
|
|
|
|
* resolve: {
|
|
|
|
* team: 'teamResolver'
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ])
|
|
|
|
* ],
|
|
|
|
* providers: [
|
|
|
|
* {
|
|
|
|
* provide: 'teamResolver',
|
|
|
|
* useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => 'team'
|
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* })
|
2016-08-30 18:57:24 -04:00
|
|
|
* class AppModule {}
|
2016-08-19 18:48:09 -04:00
|
|
|
* ```
|
2018-04-05 17:31:44 -04:00
|
|
|
*
|
2016-06-27 15:27:23 -04:00
|
|
|
*/
|
2016-06-27 17:00:07 -04:00
|
|
|
export interface Resolve<T> {
|
2016-12-08 14:24:38 -05:00
|
|
|
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<T>|Promise<T>|T;
|
2016-06-27 15:27:23 -04:00
|
|
|
}
|
2016-07-26 17:39:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-04-05 06:38:57 -04:00
|
|
|
* @description
|
2016-07-26 17:39:02 -04:00
|
|
|
*
|
2018-04-05 06:51:21 -04:00
|
|
|
* Interface that a class can implement to be a guard deciding if a children can be loaded.
|
|
|
|
*
|
2016-07-26 17:39:02 -04:00
|
|
|
* ```
|
2016-08-30 18:57:24 -04:00
|
|
|
* class UserToken {}
|
|
|
|
* class Permissions {
|
|
|
|
* canLoadChildren(user: UserToken, id: string): boolean {
|
|
|
|
* return true;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2016-07-26 17:39:02 -04:00
|
|
|
* @Injectable()
|
2016-08-01 18:48:40 -04:00
|
|
|
* class CanLoadTeamSection implements CanLoad {
|
2016-07-26 17:39:02 -04:00
|
|
|
* constructor(private permissions: Permissions, private currentUser: UserToken) {}
|
|
|
|
*
|
2017-02-18 17:28:45 -05:00
|
|
|
* canLoad(route: Route): Observable<boolean>|Promise<boolean>|boolean {
|
2016-07-26 17:39:02 -04:00
|
|
|
* return this.permissions.canLoadChildren(this.currentUser, route);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2016-08-19 18:48:09 -04:00
|
|
|
* @NgModule({
|
|
|
|
* imports: [
|
|
|
|
* RouterModule.forRoot([
|
|
|
|
* {
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: TeamCmp,
|
|
|
|
* loadChildren: 'team.js',
|
|
|
|
* canLoad: [CanLoadTeamSection]
|
|
|
|
* }
|
|
|
|
* ])
|
|
|
|
* ],
|
2016-08-30 18:57:24 -04:00
|
|
|
* providers: [CanLoadTeamSection, UserToken, Permissions]
|
2016-08-19 18:48:09 -04:00
|
|
|
* })
|
2016-08-30 18:57:24 -04:00
|
|
|
* class AppModule {}
|
2016-07-26 17:39:02 -04:00
|
|
|
* ```
|
|
|
|
*
|
2017-02-16 13:26:53 -05:00
|
|
|
* You can alternatively provide a function with the `canLoad` signature:
|
2016-07-26 17:39:02 -04:00
|
|
|
*
|
|
|
|
* ```
|
2016-08-19 18:48:09 -04:00
|
|
|
* @NgModule({
|
|
|
|
* imports: [
|
|
|
|
* RouterModule.forRoot([
|
|
|
|
* {
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: TeamCmp,
|
|
|
|
* loadChildren: 'team.js',
|
|
|
|
* canLoad: ['canLoadTeamSection']
|
|
|
|
* }
|
|
|
|
* ])
|
|
|
|
* ],
|
|
|
|
* providers: [
|
|
|
|
* {
|
|
|
|
* provide: 'canLoadTeamSection',
|
|
|
|
* useValue: (route: Route) => true
|
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* })
|
2016-08-30 18:57:24 -04:00
|
|
|
* class AppModule {}
|
2016-07-26 17:39:02 -04:00
|
|
|
* ```
|
|
|
|
*
|
2018-04-05 17:31:44 -04:00
|
|
|
*
|
2016-07-26 17:39:02 -04:00
|
|
|
*/
|
2016-08-19 18:48:09 -04:00
|
|
|
export interface CanLoad { canLoad(route: Route): Observable<boolean>|Promise<boolean>|boolean; }
|