UNPKG

los-auth

Version:

Libreria de autenticacion para las aplicaciones internas de la empresa LOS

1 lines 5.46 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("@angular/common/http"),require("angular2-jwt"),require("rxjs/Rx"),require("@angular/router")):"function"==typeof define&&define.amd?define(["exports","@angular/core","@angular/common/http","angular2-jwt","rxjs/Rx","@angular/router"],t):t(e.ngLosAuth={},e.ng.core,e.http,e.angular2Jwt,e.Rx,e.router)}(this,function(e,t,o,r,n,s){"use strict";var a=function(){function e(e){this.http=e,this.jwtHelper=new r.JwtHelper,this.authStorage="auth",this.roomStorage="room"}return e.prototype.login=function(e){var t=this,r=new o.HttpHeaders({"Content-Type":"application/x-www-form-urlencoded"}),n="grant_type="+e.grant_type+"&username="+e.username+"&password="+e.password+"&client_id="+e.client_id;return this.http.post(e.url,n,{headers:r}).map(function(e){return sessionStorage.setItem(t.authStorage,JSON.stringify(e)),e})},e.prototype.logout=function(){var e=this;return new n.Observable(function(t){sessionStorage.removeItem(e.authStorage),t.next(!0),t.complete()})},e.prototype.isLogged=function(){var e=this;return new n.Observable(function(t){null!==sessionStorage.getItem(e.authStorage)?t.next(!0):t.next(!1),t.complete()})},e.prototype.isLoggedDirect=function(){return null!==sessionStorage.getItem(this.authStorage)},e.prototype.getUserActive=function(){var e=this;return new n.Observable(function(t){null!==sessionStorage.getItem(e.authStorage)?t.next(e.jwtHelper.decodeToken(JSON.parse(sessionStorage.getItem(e.authStorage)).access_token)):t.next(null),t.complete()})},e.prototype.getToken=function(){return null!==sessionStorage.getItem(this.authStorage)?JSON.parse(sessionStorage.getItem(this.authStorage)).access_token:""},e.prototype.getUserRoles=function(){var e=this;return new n.Observable(function(t){null!==sessionStorage.getItem(e.authStorage)?t.next(e.jwtHelper.decodeToken(JSON.parse(sessionStorage.getItem(e.authStorage)).access_token).role):t.next([]),t.complete()})},e.prototype.hasPermission=function(e){var t=this;return new n.Observable(function(o){t.getUserRoles().subscribe(function(t){var r=!1;e.forEach(function(e){t.indexOf(e.toString())>-1&&(r=!0)}),o.next(r),o.complete()})})},e.prototype.getUserApplications=function(){var e=this;return new n.Observable(function(t){null!==sessionStorage.getItem(e.authStorage)?t.next(JSON.parse(e.jwtHelper.decodeToken(JSON.parse(sessionStorage.getItem(e.authStorage)).access_token).user_applications)):t.next([]),t.complete()})},e.prototype.changePassword=function(e){return this.http.put(e.ApiURL,e)},e.prototype.switchSkinTheme=function(e){return this.http.put("",null)},e.prototype.getUserSkin=function(){var e=this;return new n.Observable(function(t){e.getUserActive().subscribe(function(e){e?t.next(e.skin):t.next(""),t.complete()})})},e.prototype.setSkinLocal=function(){return new n.Observable},e.prototype.validateUserSkin=function(){return new n.Observable},e.prototype.getUserToken=function(){var e=this;return new n.Observable(function(t){null!==sessionStorage.getItem(e.authStorage)?t.next(e.jwtHelper.decodeToken(JSON.parse(sessionStorage.getItem(e.authStorage)).access_token)):t.next(""),t.complete()})},e.prototype.getUserRooms=function(){var e=this;return new n.Observable(function(t){if(null!==sessionStorage.getItem(e.authStorage)){var o=[];e.jwtHelper.decodeToken(JSON.parse(sessionStorage.getItem(e.authStorage)).access_token).salas.forEach(function(e){o.push(JSON.parse(e))}),t.next(o)}else t.next([]);t.complete()})},e.prototype.getUserRoomDefault=function(){var e=this;return new n.Observable(function(t){null!==sessionStorage.getItem(e.authStorage)?t.next(e.jwtHelper.decodeToken(JSON.parse(sessionStorage.getItem(e.authStorage)).access_token).sala_default_id):t.next(""),t.complete()})},e.prototype.setRoomActive=function(e){var t=this;return new n.Observable(function(o){sessionStorage.setItem(t.roomStorage,e),o.next(!0),o.complete()})},e.prototype.getRoomActive=function(){var e=this;return new n.Observable(function(t){t.next(sessionStorage.getItem(e.roomStorage)),t.complete()})},e.decorators=[{type:t.Injectable}],e.ctorParameters=function(){return[{type:o.HttpClient}]},e}(),u=function(){function e(e,t){this.authService=e,this.route=t}return e.prototype.canActivate=function(e,t){var o=this;return this.authService.isLogged().subscribe(function(t){if(!t)return!1;o.authService.hasPermission(e.losAuthRoles).subscribe(function(e){return!!e})}),!0},e.decorators=[{type:t.Injectable}],e.ctorParameters=function(){return[{type:a},{type:s.Router}]},e}(),i=function(){function e(e){this.injector=e}return e.prototype.intercept=function(e,t){var o=this.injector.get(a);return e=e.clone({setHeaders:{Authorization:"Bearer "+o.getToken()}}),t.handle(e)},e.decorators=[{type:t.Injectable}],e.ctorParameters=function(){return[{type:t.Injector}]},e}(),c=function(){function e(){}return e.forRoot=function(){return{ngModule:e,providers:[a,u,{provide:o.HTTP_INTERCEPTORS,useClass:i,multi:!0}]}},e.decorators=[{type:t.NgModule,args:[{imports:[o.HttpClientModule],exports:[o.HttpClientModule]}]}],e.ctorParameters=function(){return[]},e}(),p=function(){function e(){}return e.Routes=function(e){var t=[];return e.forEach(function(e){var o=[];e.losAuthActivate&&o.push(u),e.canActivate=o,t.push(e)}),s.RouterModule.forRoot(t,{useHash:!0})},e}();e.LosAuthModule=c,e.LosAuthService=a,e.LosAuthGuardService=u,e.LosAuthRouting=p,Object.defineProperty(e,"__esModule",{value:!0})});