summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/services/change-password-guard.service.ts
blob: d97160f922a27cd215ab5bf2eb63f8705d79a1d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { Injectable } from '@angular/core';
import {
  ActivatedRouteSnapshot,
  CanActivate,
  CanActivateChild,
  Router,
  RouterStateSnapshot
} from '@angular/router';

import { AuthStorageService } from './auth-storage.service';

/**
 * This service guard checks if a user must be redirected to a special
 * page at '/login-change-password' to set a new password.
 */
@Injectable({
  providedIn: 'root'
})
export class ChangePasswordGuardService implements CanActivate, CanActivateChild {
  constructor(private router: Router, private authStorageService: AuthStorageService) {}

  canActivate(_route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    // Redirect to '/login-change-password' when the following constraints
    // are fulfilled:
    // - The user must be logged in.
    // - SSO must be disabled.
    // - The flag 'User must change password at next logon' must be set.
    if (
      this.authStorageService.isLoggedIn() &&
      !this.authStorageService.isSSO() &&
      this.authStorageService.getPwdUpdateRequired()
    ) {
      this.router.navigate(['/login-change-password'], { queryParams: { returnUrl: state.url } });
      return false;
    }
    return true;
  }

  canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    return this.canActivate(childRoute, state);
  }
}