1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/model/network/authentication.service.ts

93 lines
2.5 KiB
TypeScript
Raw Normal View History

2018-03-31 03:30:30 +08:00
import {Injectable} from '@angular/core';
import {UserDTO, UserRoles} from '../../../../common/entities/UserDTO';
2018-05-23 08:27:07 +08:00
import {BehaviorSubject} from 'rxjs';
2018-03-31 03:30:30 +08:00
import {UserService} from './user.service';
import {LoginCredential} from '../../../../common/entities/LoginCredential';
import {Cookie} from 'ng2-cookies';
import {Config} from '../../../../common/config/public/Config';
import {NetworkService} from './network.service';
import {ErrorCodes, ErrorDTO} from '../../../../common/entities/Error';
import {CookieNames} from '../../../../common/CookieNames';
2016-03-13 18:28:29 +08:00
2016-05-09 23:04:56 +08:00
declare module ServerInject {
export let user: UserDTO;
}
2016-03-13 18:28:29 +08:00
@Injectable()
2016-05-09 23:04:56 +08:00
export class AuthenticationService {
2016-03-13 18:28:29 +08:00
2017-07-04 01:17:49 +08:00
public user: BehaviorSubject<UserDTO>;
2016-03-13 18:28:29 +08:00
constructor(private _userService: UserService,
private _networkService: NetworkService) {
2017-07-04 01:17:49 +08:00
this.user = new BehaviorSubject(null);
2016-05-09 23:04:56 +08:00
2018-05-04 06:23:48 +08:00
// picking up session..
if (this.isAuthenticated() === false && Cookie.get(CookieNames.session) != null) {
2018-03-31 03:30:30 +08:00
if (typeof ServerInject !== 'undefined' && typeof ServerInject.user !== 'undefined') {
2017-07-04 01:17:49 +08:00
this.user.next(ServerInject.user);
}
this.getSessionUser();
} else {
2017-07-04 01:17:49 +08:00
if (Config.Client.authenticationRequired === false) {
this.user.next(<UserDTO>{name: UserRoles[Config.Client.unAuthenticatedUserRole], role: Config.Client.unAuthenticatedUserRole});
2017-07-04 01:17:49 +08:00
}
}
2016-05-09 23:04:56 +08:00
_networkService.addGlobalErrorHandler((error: ErrorDTO) => {
2018-05-04 06:23:48 +08:00
if (error.code === ErrorCodes.NOT_AUTHENTICATED) {
this.user.next(null);
return true;
}
2018-05-04 06:23:48 +08:00
if (error.code === ErrorCodes.NOT_AUTHORISED) {
this.logout();
return true;
}
return false;
});
}
2016-03-13 18:28:29 +08:00
2017-07-04 01:17:49 +08:00
private async getSessionUser(): Promise<void> {
try {
this.user.next(await this._userService.getSessionUser());
} catch (error) {
console.log(error);
}
2016-05-05 04:20:54 +08:00
}
2016-05-05 04:20:54 +08:00
2017-07-04 01:17:49 +08:00
public async login(credential: LoginCredential): Promise<UserDTO> {
2017-07-09 18:03:17 +08:00
const user = await this._userService.login(credential);
this.user.next(user);
return user;
}
public async shareLogin(password: string): Promise<UserDTO> {
const user = await this._userService.shareLogin(password);
this.user.next(user);
return user;
}
2016-03-13 18:28:29 +08:00
2016-05-09 23:04:56 +08:00
public isAuthenticated(): boolean {
if (Config.Client.authenticationRequired === false) {
return true;
}
2017-07-04 01:17:49 +08:00
return !!(this.user.value && this.user.value != null);
}
2016-03-13 18:28:29 +08:00
2017-07-09 18:03:17 +08:00
public isAuthorized(role: UserRoles) {
return this.user.value && this.user.value.role >= role;
}
public logout() {
this._userService.logout();
2017-07-04 01:17:49 +08:00
this.user.next(null);
}
2016-05-17 05:15:03 +08:00
2016-03-13 18:28:29 +08:00
}