Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {Component, OnInit} from "@angular/core";
- import {Router} from "@angular/router";
- import {ModuloService} from "../../shared/layout/navigation/http-modulo-service";
- import {Modulo} from "../../+dto/maintenance/modulo";
- import {Observable} from "rxjs";
- import {StorageService} from "../../+common/storageLocalValues/storage.service";
- import {AuthenticationService} from "../+services/authentication.service";
- import {CurrentUser} from "../../+dto/currentUser";
- import {Response} from "@angular/http";
- import {AssignedRole} from "../../+dto/assignedRole";
- import {AutorizacionFilter} from "../../+dto/autorizacionFilter";
- var Rx = require('rxjs/Rx');
- declare var $: any;
- @Component({
- selector: 'app-login',
- templateUrl: './login.component.html'
- })
- export class LoginComponent implements OnInit {
- model: any = {};
- loading = false;
- error = '';
- modulos: Array<Modulo>;
- currentUser: CurrentUser;
- private rolAsignado: Array<AssignedRole> = [];
- errorMessage: string;
- authorizationFilter: AutorizacionFilter = new AutorizacionFilter();
- constructor(
- private router: Router,
- private storageService: StorageService,
- private authenticationService: AuthenticationService,
- private moduloService: ModuloService) {
- }
- ngOnInit() {
- this.resizeWindow();
- $(window).bind("load resize scroll", () => {
- this.resizeWindow();
- });
- $(document).ready(() => {
- this.resizeWindow();
- });
- }
- resizeWindow () {
- $('.content-login').css("height", $(window).height() + "px");
- }
- onForgotPassword(){
- this.router.navigate(['forgot/password']);
- }
- login() {
- this.loading = true;
- this.authorizationFilter.idUsuario = 23;
- this.authorizationFilter.codigoModulo = 'A001';
- this.authenticationService.login(this.model.username, this.model.password)
- .subscribe( data => {
- this.moduloService.getModulosPermitidos(this.model.username)
- .subscribe( dataModulos => {
- this.modulos = dataModulos;
- localStorage.setItem("modulos", JSON.stringify(this.modulos));
- //second service
- this.authenticationService.authenticateUser(this.model.username)
- .subscribe( dataAuthenticate => {
- this.currentUser = dataAuthenticate;
- localStorage.setItem("authenticatedUser", JSON.stringify(this.currentUser));
- //3er service
- this.storageService.retrieveComboLocalStorage(this.authorizationFilter).subscribe(
- data =>{
- this.storageService.localStorageValuesCommons(data);
- let source1 = Rx.Observable
- .of(
- this.navigateRolByDefault()
- )
- .toPromise()
- .catch(err => {
- this.handleError(err);
- });
- source1.then((value) => {console.log('navigateRolByDefault'+value)});
- },
- error => {
- this.error = 'Contacta al administrador';
- this.handleError(error);
- this.loading = false;
- })
- },
- error => {
- this.error = 'Usuario o contraseña es incorrecto.';
- this.handleError(error);
- this.loading = false;
- });
- },
- error => {
- this.error = 'Usuario no tiene módulos asignados.';
- this.handleError(error);
- this.loading = false;
- });
- },
- error => {
- this.error = error.json().message;
- this.handleError(error);
- this.loading = false;
- });
- }
- private navigateRolByDefault(){
- let rolByDefault;
- this.currentUser = JSON.parse(localStorage.getItem("authenticatedUser") || '{}');
- var index;
- for(index=0;index<this.currentUser.assignedRoles.length;index++) {
- var r=this.currentUser.assignedRoles[index]
- if(r.assigned && r.roleName=='RHANA' && r.roleDefault == true){
- rolByDefault = r.roleName;
- }
- else if(r.assigned && r.roleName=='EMPLE' && r.roleDefault == true){
- rolByDefault = r.roleName;
- }
- else if(r.assigned && r.roleName=='GEREN' && r.roleDefault == true){
- rolByDefault = r.roleName;
- }
- else if(r.assigned && r.roleName=='ADMIN' && r.roleDefault == true){
- rolByDefault = r.roleName;
- }
- else if(r.assigned && r.roleDefault == true){
- rolByDefault = r.roleName;
- }
- }
- if(rolByDefault == 'RHANA'){
- this.router.navigate(['/dashboard/principalAnalistaRRHH']);
- }
- else if(rolByDefault == 'EMPLE'){
- this.router.navigate(['/dashboard/principal']);
- }
- else if(rolByDefault == 'GEREN'){
- this.router.navigate(['/dashboard/principalJefe']);
- }
- else if(rolByDefault == 'ADMIN'){
- this.router.navigate(['/organizacion/busquedaEmpresa']);
- }
- else{
- this.router.navigate(['/miscellaneous/blank']);
- }
- }
- private handleError(error: Response) {
- return Observable.throw(error.json().error || 'Error del Servidor');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement