1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-16 11:37:13 +02:00
pigallery2/frontend/app/admin/admin.component.ts

88 lines
2.6 KiB
TypeScript
Raw Normal View History

///<reference path="../../browser.d.ts"/>
2016-05-04 17:20:21 +02:00
import {Component, OnInit} from "@angular/core";
import {AuthenticationService} from "../model/network/authentication.service.ts";
2016-05-04 17:20:21 +02:00
import {Router} from "@angular/router-deprecated";
import {FrameComponent} from "../frame/frame.component";
import {User, UserRoles} from "../../../common/entities/User";
2016-05-04 17:20:21 +02:00
import {FORM_DIRECTIVES} from "@angular/common";
import {Utils} from "../../../common/Utils";
import {AdminService} from "./admin.service";
import {Message} from "../../../common/entities/Message";
2016-05-09 17:04:56 +02:00
import {StringifyRole} from "./../pipes/StringifyRolePipe";
2016-07-07 12:26:36 +02:00
import {Config} from "../config/Config";
@Component({
selector: 'admin',
templateUrl: 'app/admin/admin.component.html',
styleUrls: ['app/admin/admin.component.css'],
directives: [FrameComponent, FORM_DIRECTIVES],
providers: [AdminService],
pipes: [StringifyRole]
})
export class AdminComponent implements OnInit {
private newUser = new User();
private userRoles:Array<any> = [];
private users:Array<User> = [];
2016-07-07 12:26:36 +02:00
userManagementEnable:boolean = false;
constructor(private _authService:AuthenticationService, private _router:Router, private _adminService:AdminService) {
2016-07-07 12:26:36 +02:00
this.userManagementEnable = Config.Client.authenticationRequired;
}
ngOnInit() {
if (!this._authService.isAuthenticated() || this._authService.getUser().role < UserRoles.Admin) {
this._router.navigate(['Login']);
return;
}
2016-07-07 12:26:36 +02:00
if (Config.Client.authenticationRequired === true) {
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>>) => {
this.users = result.result;
});
}
2016-05-04 17:20:21 +02:00
canModifyUser(user:User):boolean {
let currentUser = this._authService.getUser();
if (!currentUser) {
return false;
}
2016-05-04 17:20:21 +02:00
return currentUser.name != user.name && currentUser.role >= user.role;
}
2016-05-04 17:20:21 +02:00
initNewUser() {
this.newUser = new User();
2016-05-04 17:20:21 +02:00
this.newUser.role = UserRoles.User;
}
2016-05-04 17:20:21 +02:00
addNewUser() {
this._adminService.createUser(this.newUser).then(() => {
this.getUsersList();
});
}
2016-05-04 17:20:21 +02:00
updateRole(user:User) {
this._adminService.updateRole(user).then(() => {
this.getUsersList();
});
}
2016-05-04 17:20:21 +02:00
deleteUser(user:User) {
this._adminService.deleteUser(user).then(() => {
this.getUsersList();
2016-05-04 17:20:21 +02:00
});
}
}