karideo/front/src/app/service/session.ts

113 lines
2.4 KiB
TypeScript

/** @file
* @author Edouard DUPIN
* @copyright 2018, Edouard DUPIN, all right reserved
* @license PROPRIETARY (see license file)
*/
import { Injectable, Output, EventEmitter } from '@angular/core';
export enum UserRoles222 {
admin = 10000,
user = 1,
guest = 10
}
@Injectable()
export class SessionService {
private tokenJwt = null;
public sessionId = null;
public userLogin = null;
public userAdmin = null;
public userEMail = null;
public userAvatar = null;
public userId = null;
@Output() change: EventEmitter<boolean> = new EventEmitter();
constructor() {
}
/**
* @brief Create a new session.
*
* @param sessionId -
* @param userId -
* @param userLogin -
* @param userEMail -
* @param userAdmin -
* @param userAvatar -
*/
create(sessionId,
userId: string,
userLogin: string,
userEMail: string,
userAdmin: boolean,
userAvatar: string) {
console.log(`Session Create: userId=${userId} userLogin=${userLogin} userEMail=${userEMail} userAdmin=${userAdmin} userAvatar=${userAvatar} sessionId = ${sessionId}`);
this.tokenJwt = undefined;
this.sessionId = sessionId;
this.userId = userId;
this.userLogin = userLogin;
this.userAdmin = userAdmin;
this.userEMail = userEMail;
this.userAvatar = userAvatar;
this.change.emit(true);
}
/**
* @brief destroy the current session.
*/
destroy() {
console.log('Session REMOVE');
let last = this.sessionId;
this.sessionId = null;
this.tokenJwt = undefined;
this.userId = null;
this.userLogin = null;
this.userAdmin = null;
this.userEMail = null;
this.userAvatar = null;
this.change.emit(false);
}
setToken(jwt: string) {
this.tokenJwt = jwt;
}
getToken(): string | undefined {
return this.tokenJwt;
}
islogged() {
return this.sessionId !== null;
}
hasRight(type) {
if(type === UserRoles222.admin) {
// sometime needed...
return this.userAdmin;
}
if(type === UserRoles222.user) {
// is connected ==> is user
return this.sessionId !== null;
}
if(type === UserRoles222.guest) {
// all the other ... maybe unneeded
return true;
}
return false;
}
hasNotRight(type) {
return !this.hasRight(type);
}
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;
*/
}
}