190 lines
4.2 KiB
TypeScript
190 lines
4.2 KiB
TypeScript
/** @file
|
|
* @author Edouard DUPIN
|
|
* @copyright 2018, Edouard DUPIN, all right reserved
|
|
* @license PROPRIETARY (see license file)
|
|
*/
|
|
|
|
import { Injectable, Output, EventEmitter, Inject } from '@angular/core';
|
|
import { Router } from '@angular/router';
|
|
import { Environment } from '../model/environment';
|
|
import { isNullOrUndefined } from '../utils';
|
|
|
|
export enum UserRoles222 {
|
|
admin = 'admin',
|
|
user = 'user',
|
|
guest = 'guest',
|
|
}
|
|
|
|
@Injectable()
|
|
export class SessionService {
|
|
private tokenJwt?: string;
|
|
public userLogin?: string;
|
|
public userId?: string;
|
|
public applName?: string;
|
|
public right: any = {};
|
|
|
|
@Output() change: EventEmitter<boolean> = new EventEmitter();
|
|
|
|
constructor(
|
|
@Inject('ENVIRONMENT') environment: Environment,
|
|
) {
|
|
console.log("Start SessionService");
|
|
this.applName = environment.applName;
|
|
}
|
|
|
|
/**
|
|
* @brief Create a new session.
|
|
*
|
|
* @param userId -
|
|
* @param userLogin -
|
|
* @param tokenJwt -
|
|
*/
|
|
create({
|
|
userId,
|
|
userLogin,
|
|
tokenJwt,
|
|
}: {
|
|
userId: string;
|
|
userLogin: string;
|
|
tokenJwt: string;
|
|
}) {
|
|
console.log(
|
|
`Session Create: userId=${userId} userLogin=${userLogin} tokenJwt = ${tokenJwt}`
|
|
);
|
|
this.tokenJwt = tokenJwt;
|
|
this.userId = userId;
|
|
this.userLogin = userLogin;
|
|
this.right = this.parseToken(tokenJwt);
|
|
console.log(`Retrieve right: ${JSON.stringify(this.right, null, 4)}`);
|
|
this.change.emit(true);
|
|
}
|
|
|
|
b64_to_utf8(str: string): string {
|
|
return decodeURIComponent(window.atob(str));
|
|
}
|
|
parseToken(token: string): any {
|
|
const cut = token.split('.');
|
|
const decoded = this.b64_to_utf8(cut[1]);
|
|
const jsonModel = JSON.parse(decoded);
|
|
if (isNullOrUndefined(jsonModel.right)) {
|
|
return {};
|
|
}
|
|
if (isNullOrUndefined(jsonModel.right[this.applName])) {
|
|
return {};
|
|
}
|
|
return jsonModel.right[this.applName];
|
|
}
|
|
/**
|
|
* @brief destroy the current session.
|
|
*/
|
|
destroy() {
|
|
console.log('Session REMOVE');
|
|
this.tokenJwt = undefined;
|
|
this.userId = undefined;
|
|
this.userLogin = undefined;
|
|
this.right = {};
|
|
this.change.emit(false);
|
|
}
|
|
getToken(): string | undefined {
|
|
return this.tokenJwt;
|
|
}
|
|
isLogged() {
|
|
return this.userId !== null;
|
|
}
|
|
hasRight(type: UserRoles222): boolean {
|
|
if (type === UserRoles222.admin) {
|
|
if (isNullOrUndefined(this.right.ADMIN)) {
|
|
return false;
|
|
}
|
|
return this.right.ADMIN;
|
|
}
|
|
if (type === UserRoles222.user) {
|
|
if (isNullOrUndefined(this.right.USER)) {
|
|
return false;
|
|
}
|
|
return this.right.USER;
|
|
}
|
|
if (type === UserRoles222.guest) {
|
|
// all the other ... maybe unneeded
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
hasNotRight(rightType: UserRoles222) {
|
|
return !this.hasRight(rightType);
|
|
}
|
|
getLogin() {
|
|
return this.userLogin;
|
|
}
|
|
getAvatar() {
|
|
return 'assets/images/avatar_generic.svg';
|
|
/* This is not ready:
|
|
if(this.userAvatar === false) {
|
|
return 'assets/images/avatar_generic.svg';
|
|
}
|
|
return this.userId;
|
|
*/
|
|
}
|
|
}
|
|
|
|
@Injectable()
|
|
export class OnlyUsersGuard {
|
|
constructor(private sessionService: SessionService, private router: Router) { }
|
|
|
|
canActivate() {
|
|
console.log(`OnlyUsersGuard : ${this.sessionService}`)
|
|
console.log('OnlyLoggedInUsers');
|
|
if (this.sessionService.hasRight(UserRoles222.user)
|
|
|| this.sessionService.hasRight(UserRoles222.admin)) {
|
|
return true;
|
|
}
|
|
this.router.navigateByUrl('/forbidden');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
@Injectable()
|
|
export class OnlyUsersGuardHome {
|
|
constructor(private sessionService: SessionService, private router: Router) { }
|
|
|
|
canActivate() {
|
|
if (!this.sessionService.isLogged()) {
|
|
this.router.navigateByUrl('/unregistered');
|
|
return false;
|
|
}
|
|
if (this.sessionService.hasRight(UserRoles222.user)
|
|
|| this.sessionService.hasRight(UserRoles222.admin)) {
|
|
return true;
|
|
}
|
|
this.router.navigateByUrl('/forbidden');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
@Injectable()
|
|
export class OnlyUnregisteredGuardHome {
|
|
constructor(private sessionService: SessionService, private router: Router) { }
|
|
|
|
canActivate() {
|
|
if (this.sessionService.isLogged()) {
|
|
this.router.navigateByUrl('/home');
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
@Injectable()
|
|
export class OnlyAdminGuard {
|
|
constructor(private sessionService: SessionService, private router: Router) { }
|
|
|
|
canActivate() {
|
|
if (!this.sessionService.hasRight(UserRoles222.user)) {
|
|
this.router.navigateByUrl('/forbidden');
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|