File

src/app/modules/core/guard/auth-gard.service.ts

Description

Service for Route Guards to restrict the access of route based on roles and permission of logged in user.

Index

Properties
Methods

Constructor

constructor(router: Router, permissionService: PermissionService, resourceService: ResourceService, config: ConfigService, toasterService: ToasterService, userService: UserService)

constructor

Parameters :
Name Type Optional Description
router Router No
permissionService PermissionService No

Reference of permission service to check permission

resourceService ResourceService No

Reference of resourceService service

config ConfigService No
toasterService ToasterService No
userService UserService No

Methods

canActivate
canActivate(activatedRouteSnapshot: ActivatedRouteSnapshot, routerStateSnapshot: RouterStateSnapshot)

method CanActivate for guard .

Parameters :
Name Type Optional
activatedRouteSnapshot ActivatedRouteSnapshot No
routerStateSnapshot RouterStateSnapshot No
Returns : Observable<boolean>
canLoad
canLoad()

method CanLoad for guard.

Returns : boolean
getPermission
getPermission(roles)
Parameters :
Name Optional
roles No
Returns : any
navigateToHome
navigateToHome(observer)
Parameters :
Name Optional
observer No
Returns : void
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanLoad } from '@angular/router';
import { PermissionService } from './../services';
import { ConfigService, ResourceService, ToasterService } from '@sunbird/shared';
import { Observable } from 'rxjs';
import { UserService } from './../services/user/user.service';
import * as _ from 'lodash-es';

/**
 * Service for Route Guards to restrict the access of route
 * based on roles and permission of logged in user.
*/
@Injectable()
export class AuthGuard implements CanActivate, CanLoad {
    /**
      * reference of permissionService service.
    */
    public permissionService: PermissionService;
    /**
     * reference of resourceService service.
    */
    public resourceService: ResourceService;
    /**
 * reference of resourceService service.
*/
    public config: ConfigService;

    /**
    * constructor
    * @param {permissionService} permissionService Reference of permission service to check permission
    * @param {resourceService} resourceService Reference of resourceService service
    * @param {Router} route  Reference of Router
    */
    constructor(private router: Router, permissionService: PermissionService, resourceService: ResourceService,
        config: ConfigService, private toasterService: ToasterService, private userService: UserService) {
        this.permissionService = permissionService;
        this.resourceService = resourceService;
        this.config = config;
    }
    /**
    * method CanLoad for guard.
    */
    canLoad(): boolean {
        if (this.userService.loggedIn) {
            return true;
        } else {
            return false;
        }
    }
    /**
    * method CanActivate for guard .
    */
    canActivate(activatedRouteSnapshot: ActivatedRouteSnapshot, routerStateSnapshot: RouterStateSnapshot): Observable<boolean> {
        return this.getPermission(activatedRouteSnapshot.data.roles);
    }
    // canActivateChild(activatedRouteSnapshot: ActivatedRouteSnapshot, routerStateSnapshot: RouterStateSnapshot): Observable<boolean> {
    //     return this.getPermission(activatedRouteSnapshot.url[0].path);
    // }

    getPermission(roles) {
        return Observable.create(observer => {
            if (roles === 'rootOrgAdmin') {
                this.userService.userData$.subscribe(data => {
                    if (_.get(this.userService.userProfile, 'rootOrgAdmin')) {
                        observer.next(true);
                    } else {
                        this.navigateToHome(observer);
                    }
                });
            } else {
                this.permissionService.permissionAvailable$.subscribe(
                    permissionAvailable => {
                        if (permissionAvailable && permissionAvailable === 'success') {
                            if (roles && this.config.rolesConfig.ROLES[roles]) {
                                if (this.permissionService.checkRolesPermissions(this.config.rolesConfig.ROLES[roles])) {
                                    observer.next(true);
                                } else {
                                    this.navigateToHome(observer);
                                }
                            } else {
                                this.navigateToHome(observer);
                            }
                        } else if (permissionAvailable && permissionAvailable === 'error') {
                            this.navigateToHome(observer);
                        }
                    }
                );
            }
        });
    }

    navigateToHome(observer) {
        this.toasterService.warning(this.resourceService.messages.imsg.m0035);
        this.router.navigate(['home']);
        observer.next(false);
        observer.complete();
    }

}

results matching ""

    No results matching ""