File

src/app/modules/observation/guards/ml/ml.guard.ts

Index

Properties
Methods

Constructor

constructor(userService: UserService, resourceService: ResourceService, toasterService: ToasterService, router: Router)
Parameters :
Name Type Optional
userService UserService No
resourceService ResourceService No
toasterService ToasterService No
router Router No

Methods

canActivate
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot)
Parameters :
Name Type Optional
next ActivatedRouteSnapshot No
state RouterStateSnapshot No
Returns : Observable | Promise | boolean | UrlTree
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { UserService } from '@sunbird/core';
import { IUserData, ToasterService, ResourceService } from '@sunbird/shared';
import { take } from 'rxjs/operators';
import * as _ from 'lodash-es';

@Injectable({
  providedIn: 'root'
})
export class MlGuard implements CanActivate {
  constructor(public userService: UserService, public resourceService: ResourceService,
    public toasterService: ToasterService, public router: Router) { }
  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    return this.userService.userData$.pipe(take(1)).toPromise().then((profileData: IUserData) => {
      const subRoles = [];
      if (profileData
        && profileData.userProfile
        && profileData.userProfile['profileUserTypes']) {
        for (const subRole of profileData.userProfile['profileUserTypes']) {
          subRoles.push(subRole.type);
        }
      }
      if (subRoles.includes('administrator') || subRoles.includes('leader') || subRoles.includes('teacher')) {
        return true;
      } else {
        this.toasterService.error(_.get(this.resourceService, 'messages.stmsg.m0145'));
        const queryParam = {
          showEditUserDetailsPopup: true
        };
        this.router.navigate(['profile'], { queryParams: queryParam });
        return false;
      }
    }).catch(error => {
      return false;
    });
  }
}

results matching ""

    No results matching ""