2016-04-26 15:10:05 +02:00
|
|
|
///<reference path="../../browser.d.ts"/>
|
|
|
|
|
2016-05-04 17:20:21 +02:00
|
|
|
import {Component, OnInit} from "@angular/core";
|
2016-04-30 18:01:54 +02:00
|
|
|
import {AuthenticationService} from "../model/network/authentication.service.ts";
|
2016-05-04 17:20:21 +02:00
|
|
|
import {Router} from "@angular/router-deprecated";
|
2016-04-26 15:10:05 +02:00
|
|
|
import {FrameComponent} from "../frame/frame.component";
|
2016-05-01 21:30:43 +02:00
|
|
|
import {User, UserRoles} from "../../../common/entities/User";
|
2016-05-04 17:20:21 +02:00
|
|
|
import {FORM_DIRECTIVES} from "@angular/common";
|
2016-05-01 21:30:43 +02:00
|
|
|
import {Utils} from "../../../common/Utils";
|
|
|
|
import {AdminService} from "./admin.service";
|
2016-05-03 14:29:24 +02:00
|
|
|
import {Message} from "../../../common/entities/Message";
|
2016-05-09 17:04:56 +02:00
|
|
|
import {StringifyRole} from "./../pipes/StringifyRolePipe";
|
2016-05-01 21:30:43 +02:00
|
|
|
|
2016-04-26 15:10:05 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'admin',
|
|
|
|
templateUrl: 'app/admin/admin.component.html',
|
2016-05-01 21:30:43 +02:00
|
|
|
styleUrls: ['app/admin/admin.component.css'],
|
|
|
|
directives: [FrameComponent, FORM_DIRECTIVES],
|
2016-05-03 14:29:24 +02:00
|
|
|
providers: [AdminService],
|
|
|
|
pipes: [StringifyRole]
|
2016-04-26 15:10:05 +02:00
|
|
|
})
|
2016-05-01 21:30:43 +02:00
|
|
|
export class AdminComponent implements OnInit {
|
|
|
|
|
|
|
|
private newUser = new User();
|
2016-05-03 14:29:24 +02:00
|
|
|
private userRoles:Array<any> = [];
|
|
|
|
private users:Array<User> = [];
|
2016-05-01 21:30:43 +02:00
|
|
|
|
|
|
|
constructor(private _authService:AuthenticationService, private _router:Router, private _adminService:AdminService) {
|
2016-04-26 15:10:05 +02:00
|
|
|
}
|
|
|
|
|
2016-05-01 21:30:43 +02:00
|
|
|
ngOnInit() {
|
|
|
|
if (!this._authService.isAuthenticated() || this._authService.getUser().role < UserRoles.Admin) {
|
2016-04-26 15:10:05 +02:00
|
|
|
this._router.navigate(['Login']);
|
|
|
|
return;
|
|
|
|
}
|
2016-05-03 14:29:24 +02:00
|
|
|
this.userRoles = Utils.enumToArray(UserRoles).filter(r => r.key <= this._authService.getUser().role);
|
|
|
|
this.getUsersList();
|
|
|
|
}
|
2016-05-04 17:20:21 +02:00
|
|
|
|
|
|
|
private getUsersList() {
|
|
|
|
this._adminService.getUsers().then((result:Message<Array<User>>) => {
|
2016-05-03 14:29:24 +02:00
|
|
|
this.users = result.result;
|
|
|
|
});
|
2016-04-26 15:10:05 +02:00
|
|
|
}
|
|
|
|
|
2016-05-04 17:20:21 +02:00
|
|
|
|
|
|
|
canModifyUser(user:User):boolean {
|
|
|
|
let currentUser = this._authService.getUser();
|
|
|
|
if (!currentUser) {
|
2016-05-03 14:29:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-04 17:20:21 +02:00
|
|
|
|
2016-05-03 14:29:24 +02:00
|
|
|
return currentUser.name != user.name && currentUser.role >= user.role;
|
|
|
|
}
|
2016-05-04 17:20:21 +02:00
|
|
|
|
|
|
|
initNewUser() {
|
2016-05-01 21:30:43 +02:00
|
|
|
this.newUser = new User();
|
2016-05-04 17:20:21 +02:00
|
|
|
this.newUser.role = UserRoles.User;
|
2016-05-01 21:30:43 +02:00
|
|
|
}
|
2016-04-26 15:10:05 +02:00
|
|
|
|
2016-05-04 17:20:21 +02:00
|
|
|
addNewUser() {
|
|
|
|
this._adminService.createUser(this.newUser).then(() => {
|
2016-05-03 14:29:24 +02:00
|
|
|
this.getUsersList();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-04 17:20:21 +02:00
|
|
|
updateRole(user:User) {
|
|
|
|
this._adminService.updateRole(user).then(() => {
|
2016-05-03 14:29:24 +02:00
|
|
|
this.getUsersList();
|
|
|
|
});
|
|
|
|
}
|
2016-05-04 17:20:21 +02:00
|
|
|
|
|
|
|
deleteUser(user:User) {
|
|
|
|
this._adminService.deleteUser(user).then(() => {
|
2016-05-03 14:29:24 +02:00
|
|
|
this.getUsersList();
|
2016-05-04 17:20:21 +02:00
|
|
|
});
|
2016-05-01 21:30:43 +02:00
|
|
|
}
|
2016-04-26 15:10:05 +02:00
|
|
|
}
|
|
|
|
|
2016-05-03 14:29:24 +02:00
|
|
|
|
|
|
|
|