los-auth
Version:
Libreria de autenticacion para las aplicaciones internas de la empresa LOS
328 lines (319 loc) • 12.7 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common/http'), require('angular2-jwt'), require('rxjs/Rx'), require('@angular/router')) :
typeof define === 'function' && define.amd ? define(['exports', '@angular/core', '@angular/common/http', 'angular2-jwt', 'rxjs/Rx', '@angular/router'], factory) :
(factory((global.ngLosAuth = {}),global.ng.core,global.http,global.angular2Jwt,global.Rx,global.router));
}(this, (function (exports,core,http,angular2Jwt,Rx,router) { 'use strict';
var LosAuthService = /** @class */ (function () {
function LosAuthService(http$$1) {
this.http = http$$1;
this.jwtHelper = new angular2Jwt.JwtHelper();
this.authStorage = 'auth';
this.roomStorage = 'room';
}
LosAuthService.prototype.login = function (data) {
var _this = this;
var headers = new http.HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' });
var body = "grant_type=" + data.grant_type + "&username=" + data.username + "&password=" + data.password + "&client_id=" + data.client_id;
return this.http.post(data.url, body, { headers: headers }).map(function (response) {
sessionStorage.setItem(_this.authStorage, JSON.stringify(response));
return response;
});
};
LosAuthService.prototype.logout = function () {
var _this = this;
return new Rx.Observable(function (observable) {
sessionStorage.removeItem(_this.authStorage);
observable.next(true);
observable.complete();
});
};
LosAuthService.prototype.isLogged = function () {
var _this = this;
return new Rx.Observable(function (observable) {
if (sessionStorage.getItem(_this.authStorage) !== null)
observable.next(true);
else
observable.next(false);
observable.complete();
});
};
LosAuthService.prototype.isLoggedDirect = function () {
if (sessionStorage.getItem(this.authStorage) !== null)
return true;
else
return false;
};
LosAuthService.prototype.getUserActive = function () {
var _this = this;
return new Rx.Observable(function (observable) {
if (sessionStorage.getItem(_this.authStorage) !== null) {
observable.next(_this.jwtHelper.decodeToken(JSON.parse(sessionStorage.getItem(_this.authStorage)).access_token));
}
else
observable.next(null);
observable.complete();
});
};
LosAuthService.prototype.getToken = function () {
if (sessionStorage.getItem(this.authStorage) !== null)
return JSON.parse(sessionStorage.getItem(this.authStorage)).access_token;
else
return '';
};
LosAuthService.prototype.getUserRoles = function () {
var _this = this;
return new Rx.Observable(function (observable) {
if (sessionStorage.getItem(_this.authStorage) !== null) {
observable.next(_this.jwtHelper.decodeToken(JSON.parse(sessionStorage.getItem(_this.authStorage)).access_token).role);
}
else
observable.next([]);
observable.complete();
});
};
LosAuthService.prototype.hasPermission = function (roles) {
var _this = this;
return new Rx.Observable(function (observable) {
_this.getUserRoles().subscribe(function (userRoles) {
var hasPermission = false;
roles.forEach(function (role) {
if (userRoles.indexOf(role.toString()) > -1) {
hasPermission = true;
}
});
observable.next(hasPermission);
observable.complete();
});
});
};
LosAuthService.prototype.getUserApplications = function () {
var _this = this;
return new Rx.Observable(function (observable) {
if (sessionStorage.getItem(_this.authStorage) !== null) {
observable.next(JSON.parse(_this.jwtHelper.decodeToken(JSON.parse(sessionStorage.getItem(_this.authStorage)).access_token).user_applications));
}
else
observable.next([]);
observable.complete();
});
};
LosAuthService.prototype.changePassword = function (data) {
return this.http.put(data.ApiURL, data);
};
LosAuthService.prototype.switchSkinTheme = function (skinTheme) {
return this.http.put("", null);
};
LosAuthService.prototype.getUserSkin = function () {
var _this = this;
return new Rx.Observable(function (observable) {
_this.getUserActive().subscribe(function (user) {
if (user) {
observable.next(user.skin);
}
else {
observable.next('');
}
observable.complete();
});
});
};
LosAuthService.prototype.setSkinLocal = function () {
return new Rx.Observable();
};
LosAuthService.prototype.validateUserSkin = function () {
return new Rx.Observable();
};
// private validateSkinUser(ActiveSkin: Skin): Skin {
// let auth = this.storageService.getSessionObject('auth');
// let user = this.jwtHelper.decodeToken(auth.access_token);
// if (user.id != ActiveSkin.UserID || user.skin != ActiveSkin.Skin) {
// return this.loadSkinLocal();
// }
// return ActiveSkin;
// }
// private validateSkinUser(ActiveSkin: Skin): Skin {
// let auth = this.storageService.getSessionObject('auth');
// let user = this.jwtHelper.decodeToken(auth.access_token);
// if (user.id != ActiveSkin.UserID || user.skin != ActiveSkin.Skin) {
// return this.loadSkinLocal();
// }
// return ActiveSkin;
// }
LosAuthService.prototype.getUserToken =
// private validateSkinUser(ActiveSkin: Skin): Skin {
// let auth = this.storageService.getSessionObject('auth');
// let user = this.jwtHelper.decodeToken(auth.access_token);
// if (user.id != ActiveSkin.UserID || user.skin != ActiveSkin.Skin) {
// return this.loadSkinLocal();
// }
// return ActiveSkin;
// }
function () {
var _this = this;
return new Rx.Observable(function (observable) {
if (sessionStorage.getItem(_this.authStorage) !== null) {
observable.next(_this.jwtHelper.decodeToken(JSON.parse(sessionStorage.getItem(_this.authStorage)).access_token));
}
else
observable.next('');
observable.complete();
});
};
LosAuthService.prototype.getUserRooms = function () {
var _this = this;
return new Rx.Observable(function (observable) {
if (sessionStorage.getItem(_this.authStorage) !== null) {
var userRooms = _this.jwtHelper.decodeToken(JSON.parse(sessionStorage.getItem(_this.authStorage)).access_token).salas;
var rooms_1 = [];
userRooms.forEach(function (item) {
rooms_1.push(JSON.parse(item));
});
observable.next(rooms_1);
}
else
observable.next([]);
observable.complete();
});
};
LosAuthService.prototype.getUserRoomDefault = function () {
var _this = this;
return new Rx.Observable(function (observable) {
if (sessionStorage.getItem(_this.authStorage) !== null) {
observable.next(_this.jwtHelper.decodeToken(JSON.parse(sessionStorage.getItem(_this.authStorage)).access_token).sala_default_id);
}
else
observable.next('');
observable.complete();
});
};
LosAuthService.prototype.setRoomActive = function (room) {
var _this = this;
return new Rx.Observable(function (observable) {
sessionStorage.setItem(_this.roomStorage, room);
observable.next(true);
observable.complete();
});
};
LosAuthService.prototype.getRoomActive = function () {
var _this = this;
return new Rx.Observable(function (observable) {
observable.next(sessionStorage.getItem(_this.roomStorage));
observable.complete();
});
};
LosAuthService.decorators = [
{ type: core.Injectable },
];
/** @nocollapse */
LosAuthService.ctorParameters = function () { return [
{ type: http.HttpClient, },
]; };
return LosAuthService;
}());
var LosAuthGuardService = /** @class */ (function () {
function LosAuthGuardService(authService, route) {
this.authService = authService;
this.route = route;
}
LosAuthGuardService.prototype.canActivate = function (next, state) {
var _this = this;
this.authService.isLogged().subscribe(function (logged) {
if (logged) {
_this.authService.hasPermission(next.losAuthRoles).subscribe(function (hasPermission) {
if (hasPermission) {
return true;
}
else {
//Redirect to home
return false;
}
});
}
else {
//Redirect to login
return false;
}
});
return true;
};
LosAuthGuardService.decorators = [
{ type: core.Injectable },
];
/** @nocollapse */
LosAuthGuardService.ctorParameters = function () { return [
{ type: LosAuthService, },
{ type: router.Router, },
]; };
return LosAuthGuardService;
}());
var LosAuthInterceptor = /** @class */ (function () {
function LosAuthInterceptor(injector) {
this.injector = injector;
}
LosAuthInterceptor.prototype.intercept = function (request, next) {
var auth = this.injector.get(LosAuthService);
request = request.clone({
setHeaders: {
Authorization: "Bearer " + auth.getToken()
}
});
return next.handle(request);
};
LosAuthInterceptor.decorators = [
{ type: core.Injectable },
];
/** @nocollapse */
LosAuthInterceptor.ctorParameters = function () { return [
{ type: core.Injector, },
]; };
return LosAuthInterceptor;
}());
var LosAuthModule = /** @class */ (function () {
function LosAuthModule() {
}
LosAuthModule.forRoot = function () {
return {
ngModule: LosAuthModule,
providers: [
LosAuthService,
LosAuthGuardService,
{
provide: http.HTTP_INTERCEPTORS,
useClass: LosAuthInterceptor,
multi: true
}
]
};
};
LosAuthModule.decorators = [
{ type: core.NgModule, args: [{
imports: [http.HttpClientModule],
exports: [http.HttpClientModule]
},] },
];
/** @nocollapse */
LosAuthModule.ctorParameters = function () { return []; };
return LosAuthModule;
}());
var LosAuthRouting = /** @class */ (function () {
function LosAuthRouting() {
}
LosAuthRouting.Routes = function (inRoutes) {
var routers = [];
inRoutes.forEach(function (router$$1) {
var authGuards = [];
if (router$$1.losAuthActivate)
authGuards.push(LosAuthGuardService);
router$$1.canActivate = authGuards;
routers.push(router$$1);
});
return router.RouterModule.forRoot(routers, { useHash: true });
};
return LosAuthRouting;
}());
exports.LosAuthModule = LosAuthModule;
exports.LosAuthService = LosAuthService;
exports.LosAuthGuardService = LosAuthGuardService;
exports.LosAuthRouting = LosAuthRouting;
Object.defineProperty(exports, '__esModule', { value: true });
})));