如何使用 Guard 保护具有保护条件的 FeatureModule

发布于 2025-01-10 05:19:43 字数 2912 浏览 0 评论 0原文

在 Angular-13 应用程序中,我具有以下角色:

角色:管理员、校长、教师和助理教师

用户只能拥有一个角色。

我有这些守卫:

export class AuthGuard implements CanActivate {
  constructor(private authService: AuthService, private toastr: ToastrService, private router: Router) { }
  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ):
    | Observable<boolean | UrlTree>
    | Promise<boolean | UrlTree>
    | boolean
    | UrlTree {
    if (!this.authService.isLoggedIn()) {
      this.toastr.info('Please Log In!');
      this.router.navigate(['/auth']);
      return false;
    }
    this.authService.isLoggedIn();
    return true;
  }
}

export class AdminGuard implements CanActivate {
  checkStatus!: boolean;
  constructor(private authService: AuthService, private toastr: ToastrService, private router: Router) { }

  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
    ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    return this.checkStatus = this.authService.isAdmin();
  }
}

export class PrincipalGuard implements CanActivate {
  checkStatus!: boolean;
  constructor(private authService: AuthService, private toastr: ToastrService, private router: Router) { }

  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
    ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
      return this.checkStatus = this.authService.isPrincipal();
  }
}

AuthService:

  public isAdmin(): boolean {
    if (localStorage.getItem("role") === "Admin") {
      return true
    } else {
      this.toastr.info('Forbidden!');
      this.router.navigate(['/auth']);
      return false;
    };
  }

  public isPrincipal(): boolean {
    if (localStorage.getItem("role") === "Principal") {
      return true
    } else {
      this.toastr.info('Forbidden!');
      this.router.navigate(['/auth']);
      return false;
    };
  }

我有几个功能模块,例如 AdminModule、TeacherModule,...

下面是我的 app-routing.module:

const routes: Routes = [
  {
    path: '',
    redirectTo: 'auth',
    pathMatch: 'full'
  },
  { path: 'server-error', component: ServerErrorComponent, data: {breadcrumb: 'Server Error'} },
  { path: 'not-found', component: NotFoundComponent, data: {breadcrumb: 'Not Found'} },
  {
    path: 'admin-dashboard',
    canActivate: [AuthGuard, AdminGuard],
  loadChildren: () => import('./feature/admin/admin.module').then(m => m.AdminModule)
},
  { path: '**', redirectTo: 'not-found', pathMatch: 'full' }
];

从上面的代码中,我想用 AuthGuard 和 来保护 AdminModule(管理仪表板) AdminGuard 或校长。这意味着,用户必须具有管理员或主体角色才能访问它。

这意味着 AuthGuard && (AdminGuard || 校长)

如何修改

canActivate:[AuthGuard、AdminGuard]

来实现这个?

谢谢

In an Angular-13 application I have these roles:

Roles: Admin, Principal, Teacher and AssistantTeacher

The users can have only one role.

I have these guards:

export class AuthGuard implements CanActivate {
  constructor(private authService: AuthService, private toastr: ToastrService, private router: Router) { }
  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ):
    | Observable<boolean | UrlTree>
    | Promise<boolean | UrlTree>
    | boolean
    | UrlTree {
    if (!this.authService.isLoggedIn()) {
      this.toastr.info('Please Log In!');
      this.router.navigate(['/auth']);
      return false;
    }
    this.authService.isLoggedIn();
    return true;
  }
}

export class AdminGuard implements CanActivate {
  checkStatus!: boolean;
  constructor(private authService: AuthService, private toastr: ToastrService, private router: Router) { }

  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
    ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    return this.checkStatus = this.authService.isAdmin();
  }
}

export class PrincipalGuard implements CanActivate {
  checkStatus!: boolean;
  constructor(private authService: AuthService, private toastr: ToastrService, private router: Router) { }

  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
    ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
      return this.checkStatus = this.authService.isPrincipal();
  }
}

AuthService:

  public isAdmin(): boolean {
    if (localStorage.getItem("role") === "Admin") {
      return true
    } else {
      this.toastr.info('Forbidden!');
      this.router.navigate(['/auth']);
      return false;
    };
  }

  public isPrincipal(): boolean {
    if (localStorage.getItem("role") === "Principal") {
      return true
    } else {
      this.toastr.info('Forbidden!');
      this.router.navigate(['/auth']);
      return false;
    };
  }

I have several Feature Modules, such as AdminModule, TeacherModule, ...

Below is my app-routing.module:

const routes: Routes = [
  {
    path: '',
    redirectTo: 'auth',
    pathMatch: 'full'
  },
  { path: 'server-error', component: ServerErrorComponent, data: {breadcrumb: 'Server Error'} },
  { path: 'not-found', component: NotFoundComponent, data: {breadcrumb: 'Not Found'} },
  {
    path: 'admin-dashboard',
    canActivate: [AuthGuard, AdminGuard],
  loadChildren: () => import('./feature/admin/admin.module').then(m => m.AdminModule)
},
  { path: '**', redirectTo: 'not-found', pathMatch: 'full' }
];

From the above code, I want to protect the AdminModule (admin-dashboard) with AuthGuard and AdminGuard or Principal. That means, the user must have the role of Admin or Principal in order to access it.

That means AuthGuard && (AdminGuard || Principal)

How do I modify

canActivate: [AuthGuard, AdminGuard]

to achieve this?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文