2016-05-09 23:04:56 +08:00
|
|
|
import {Injectable} from "@angular/core";
|
2016-12-27 23:09:47 +08:00
|
|
|
import {UserDTO, UserRoles} from "../../../../common/entities/UserDTO";
|
2016-05-01 00:01:54 +08:00
|
|
|
import {Event} from "../../../../common/event/Event";
|
2016-12-27 06:36:38 +08:00
|
|
|
import {UserService} from "./user.service";
|
2016-05-01 00:01:54 +08:00
|
|
|
import {LoginCredential} from "../../../../common/entities/LoginCredential";
|
|
|
|
import {Message} from "../../../../common/entities/Message";
|
2016-12-27 06:36:38 +08:00
|
|
|
import {Cookie} from "ng2-cookies";
|
2016-05-05 00:34:54 +08:00
|
|
|
import {ErrorCodes} from "../../../../common/entities/Error";
|
2016-07-07 18:19:08 +08:00
|
|
|
import {Config} from "../../config/Config";
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
declare module ServerInject {
|
2016-12-27 23:09:47 +08:00
|
|
|
export let user: UserDTO;
|
2016-04-10 00:06:29 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-12-27 23:09:47 +08:00
|
|
|
private _user: UserDTO = null;
|
|
|
|
public OnUserChanged: Event<UserDTO>;
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
constructor(private _userService: UserService) {
|
2016-05-17 05:15:03 +08:00
|
|
|
this.OnUserChanged = new Event();
|
2016-03-21 03:06:14 +08:00
|
|
|
|
|
|
|
//picking up session..
|
2016-05-26 15:44:13 +08:00
|
|
|
if (this.isAuthenticated() == false && Cookie.get('pigallery2-session') != null) {
|
2016-05-09 23:04:56 +08:00
|
|
|
if (typeof ServerInject !== "undefined" && typeof ServerInject.user !== "undefined") {
|
2016-04-10 00:06:29 +08:00
|
|
|
this.setUser(ServerInject.user);
|
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
this.getSessionUser();
|
2016-12-27 06:36:38 +08:00
|
|
|
} else {
|
|
|
|
this.OnUserChanged.trigger(this._user);
|
2016-03-21 03:06:14 +08:00
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2016-03-21 03:06:14 +08:00
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
|
|
|
private getSessionUser() {
|
2016-12-27 23:09:47 +08:00
|
|
|
this._userService.getSessionUser().then((message: Message<UserDTO>) => {
|
2016-05-09 23:04:56 +08:00
|
|
|
if (message.error) {
|
2016-03-26 18:19:10 +08:00
|
|
|
console.log(message.error);
|
2016-05-09 23:04:56 +08:00
|
|
|
} else {
|
2016-03-21 03:06:14 +08:00
|
|
|
this._user = message.result;
|
2016-05-17 05:15:03 +08:00
|
|
|
this.OnUserChanged.trigger(this._user);
|
2016-03-21 03:06:14 +08:00
|
|
|
}
|
|
|
|
});
|
2016-03-13 18:28:29 +08:00
|
|
|
}
|
|
|
|
|
2016-12-27 23:09:47 +08:00
|
|
|
private setUser(user: UserDTO) {
|
2016-05-05 04:20:54 +08:00
|
|
|
this._user = user;
|
2016-05-17 05:15:03 +08:00
|
|
|
this.OnUserChanged.trigger(this._user);
|
2016-05-05 04:20:54 +08:00
|
|
|
}
|
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
public login(credential: LoginCredential) {
|
2016-12-27 23:09:47 +08:00
|
|
|
return this._userService.login(credential).then((message: Message<UserDTO>) => {
|
2016-05-09 23:04:56 +08:00
|
|
|
if (message.error) {
|
2016-12-27 06:36:38 +08:00
|
|
|
console.log(ErrorCodes[message.error.code] + ", message: ", message.error.message);
|
2016-05-09 23:04:56 +08:00
|
|
|
} else {
|
2016-04-10 00:06:29 +08:00
|
|
|
this.setUser(message.result);
|
2016-03-19 16:58:27 +08:00
|
|
|
}
|
2016-07-09 21:08:36 +08:00
|
|
|
return message;
|
2016-03-19 16:58:27 +08:00
|
|
|
});
|
|
|
|
}
|
2016-05-05 04:20:54 +08:00
|
|
|
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
public isAuthenticated(): boolean {
|
2016-07-07 18:19:08 +08:00
|
|
|
if (Config.Client.authenticationRequired === false) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-12-27 06:36:38 +08:00
|
|
|
return !!(this._user && this._user != null);
|
2016-03-13 18:28:29 +08:00
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
|
|
|
public getUser() {
|
2016-07-07 18:19:08 +08:00
|
|
|
if (Config.Client.authenticationRequired === false) {
|
2016-12-27 23:09:47 +08:00
|
|
|
return <UserDTO>{name: "", password: "", role: UserRoles.Admin};
|
2016-07-07 18:19:08 +08:00
|
|
|
}
|
2016-05-02 03:30:43 +08:00
|
|
|
return this._user;
|
|
|
|
}
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2016-05-17 05:15:03 +08:00
|
|
|
public logout() {
|
|
|
|
this._userService.logout();
|
|
|
|
this.setUser(null);
|
|
|
|
}
|
|
|
|
|
2016-03-13 18:28:29 +08:00
|
|
|
|
|
|
|
}
|