Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {Component, OnInit} from "@angular/core";
  2. import {Router} from "@angular/router";
  3. import {ModuloService} from "../../shared/layout/navigation/http-modulo-service";
  4. import {Modulo} from "../../+dto/maintenance/modulo";
  5. import {Observable} from "rxjs";
  6. import {StorageService} from "../../+common/storageLocalValues/storage.service";
  7. import {AuthenticationService} from "../+services/authentication.service";
  8. import {CurrentUser} from "../../+dto/currentUser";
  9. import {Response} from "@angular/http";
  10. import {AssignedRole} from "../../+dto/assignedRole";
  11. import {AutorizacionFilter} from "../../+dto/autorizacionFilter";
  12.  
  13. var Rx = require('rxjs/Rx');
  14.  
  15. declare var $: any;
  16.  
  17. @Component({
  18.     selector: 'app-login',
  19.     templateUrl: './login.component.html'
  20. })
  21. export class LoginComponent implements OnInit {
  22.     model: any = {};
  23.     loading = false;
  24.     error = '';
  25.     modulos: Array<Modulo>;
  26.     currentUser: CurrentUser;
  27.     private rolAsignado: Array<AssignedRole> = [];
  28.     errorMessage: string;
  29.  
  30.     authorizationFilter: AutorizacionFilter = new AutorizacionFilter();
  31.  
  32.     constructor(
  33.         private router: Router,
  34.         private storageService: StorageService,
  35.         private authenticationService: AuthenticationService,
  36.         private moduloService: ModuloService) {
  37.     }
  38.  
  39.     ngOnInit() {
  40.         this.resizeWindow();
  41.  
  42.         $(window).bind("load resize scroll", () => {
  43.             this.resizeWindow();
  44.         });
  45.  
  46.         $(document).ready(() => {
  47.             this.resizeWindow();
  48.         });
  49.     }
  50.  
  51.     resizeWindow () {
  52.         $('.content-login').css("height", $(window).height() + "px");
  53.     }
  54.  
  55.     onForgotPassword(){
  56.         this.router.navigate(['forgot/password']);
  57.     }
  58.  
  59.     login() {
  60.         this.loading = true;
  61.         this.authorizationFilter.idUsuario = 23;
  62.         this.authorizationFilter.codigoModulo = 'A001';
  63.  
  64.         this.authenticationService.login(this.model.username, this.model.password)
  65.             .subscribe( data => {
  66.  
  67.                     this.moduloService.getModulosPermitidos(this.model.username)
  68.                         .subscribe( dataModulos => {
  69.                                 this.modulos = dataModulos;
  70.                                 localStorage.setItem("modulos", JSON.stringify(this.modulos));
  71.                                 //second service
  72.                                 this.authenticationService.authenticateUser(this.model.username)
  73.                                     .subscribe( dataAuthenticate => {
  74.                                             this.currentUser = dataAuthenticate;
  75.                                             localStorage.setItem("authenticatedUser", JSON.stringify(this.currentUser));
  76.                                             //3er service
  77.                                             this.storageService.retrieveComboLocalStorage(this.authorizationFilter).subscribe(
  78.                                                 data =>{
  79.                                                     this.storageService.localStorageValuesCommons(data);
  80.  
  81.                                                     let source1 = Rx.Observable
  82.                                                         .of(
  83.                                                             this.navigateRolByDefault()
  84.                                                         )
  85.                                                         .toPromise()
  86.                                                         .catch(err => {
  87.                                                             this.handleError(err);
  88.                                                         });
  89.                                                     source1.then((value) => {console.log('navigateRolByDefault'+value)});
  90.  
  91.                                                 },
  92.                                                 error => {
  93.                                                     this.error = 'Contacta al administrador';
  94.                                                     this.handleError(error);
  95.                                                     this.loading = false;
  96.                                                 })
  97.  
  98.                                         },
  99.                                         error => {
  100.                                             this.error = 'Usuario o contraseña es incorrecto.';
  101.                                             this.handleError(error);
  102.                                             this.loading = false;
  103.                                     });
  104.                             },
  105.                             error => {
  106.                                 this.error = 'Usuario no tiene m&oacute;dulos asignados.';
  107.                                 this.handleError(error);
  108.                                 this.loading = false;
  109.                             });
  110.                 },
  111.                 error => {
  112.                     this.error = error.json().message;
  113.                     this.handleError(error);
  114.                     this.loading = false;
  115.             });    
  116.        
  117.  
  118.     }
  119.     private navigateRolByDefault(){
  120.         let rolByDefault;
  121.  
  122.         this.currentUser  = JSON.parse(localStorage.getItem("authenticatedUser") || '{}');
  123.         var index;
  124.         for(index=0;index<this.currentUser.assignedRoles.length;index++) {
  125.             var r=this.currentUser.assignedRoles[index]
  126.             if(r.assigned && r.roleName=='RHANA' && r.roleDefault == true){
  127.                 rolByDefault = r.roleName;
  128.             }
  129.             else if(r.assigned && r.roleName=='EMPLE' && r.roleDefault == true){
  130.                 rolByDefault = r.roleName;
  131.             }
  132.             else if(r.assigned && r.roleName=='GEREN' && r.roleDefault == true){
  133.                 rolByDefault = r.roleName;
  134.             }
  135.             else if(r.assigned && r.roleName=='ADMIN' && r.roleDefault == true){
  136.                 rolByDefault = r.roleName;
  137.             }
  138.             else if(r.assigned && r.roleDefault == true){
  139.                 rolByDefault = r.roleName;
  140.             }
  141.         }
  142.         if(rolByDefault == 'RHANA'){
  143.             this.router.navigate(['/dashboard/principalAnalistaRRHH']);
  144.         }
  145.         else if(rolByDefault == 'EMPLE'){
  146.             this.router.navigate(['/dashboard/principal']);
  147.         }
  148.         else if(rolByDefault == 'GEREN'){
  149.             this.router.navigate(['/dashboard/principalJefe']);
  150.         }
  151.         else if(rolByDefault == 'ADMIN'){
  152.             this.router.navigate(['/organizacion/busquedaEmpresa']);
  153.         }
  154.         else{
  155.             this.router.navigate(['/miscellaneous/blank']);
  156.         }
  157.  
  158.     }
  159.  
  160.     private handleError(error: Response) {
  161.         return Observable.throw(error.json().error || 'Error del Servidor');
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement