2016-07-09 15:08:36 +02:00
|
|
|
import {Component, OnInit} from "@angular/core";
|
2016-12-27 00:36:38 +02:00
|
|
|
import {AuthenticationService} from "../../model/network/authentication.service";
|
|
|
|
import {Router} from "@angular/router";
|
2016-12-27 17:09:47 +02:00
|
|
|
import {UserDTO, UserRoles} from "../../../../common/entities/UserDTO";
|
2016-07-09 15:08:36 +02:00
|
|
|
import {Utils} from "../../../../common/Utils";
|
|
|
|
import {Message} from "../../../../common/entities/Message";
|
|
|
|
import {UserManagerSettingsService} from "./usermanager.settings.service";
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'settings-usermanager',
|
|
|
|
templateUrl: 'app/settings/usermanager/usermanager.settings.component.html',
|
|
|
|
styleUrls: ['app/settings/usermanager/usermanager.settings.component.css'],
|
|
|
|
providers: [UserManagerSettingsService],
|
|
|
|
})
|
|
|
|
export class UserMangerSettingsComponent implements OnInit {
|
|
|
|
|
2016-12-27 17:09:47 +02:00
|
|
|
private newUser = <UserDTO>{};
|
2016-12-27 00:36:38 +02:00
|
|
|
private userRoles: Array<any> = [];
|
2016-12-27 17:09:47 +02:00
|
|
|
private users: Array<UserDTO> = [];
|
2016-07-09 15:08:36 +02:00
|
|
|
|
2016-12-27 00:36:38 +02:00
|
|
|
constructor(private _authService: AuthenticationService, private _router: Router, private _userSettings: UserManagerSettingsService) {
|
2016-07-09 15:08:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
if (!this._authService.isAuthenticated() || this._authService.getUser().role < UserRoles.Admin) {
|
2016-12-27 17:09:47 +02:00
|
|
|
this._router.navigate(['login']);
|
2016-07-09 15:08:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.userRoles = Utils.enumToArray(UserRoles).filter(r => r.key <= this._authService.getUser().role);
|
|
|
|
this.getUsersList();
|
|
|
|
}
|
|
|
|
|
|
|
|
private getUsersList() {
|
2016-12-27 17:09:47 +02:00
|
|
|
this._userSettings.getUsers().then((result: Message<Array<UserDTO>>) => {
|
2016-07-09 15:08:36 +02:00
|
|
|
this.users = result.result;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-27 17:09:47 +02:00
|
|
|
canModifyUser(user: UserDTO): boolean {
|
2016-07-09 15:08:36 +02:00
|
|
|
let currentUser = this._authService.getUser();
|
|
|
|
if (!currentUser) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return currentUser.name != user.name && currentUser.role >= user.role;
|
|
|
|
}
|
|
|
|
|
|
|
|
initNewUser() {
|
2016-12-27 17:09:47 +02:00
|
|
|
this.newUser = <UserDTO>{role: UserRoles.User};
|
2016-07-09 15:08:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
addNewUser() {
|
|
|
|
this._userSettings.createUser(this.newUser).then(() => {
|
|
|
|
this.getUsersList();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-27 17:09:47 +02:00
|
|
|
updateRole(user: UserDTO) {
|
2016-07-09 15:08:36 +02:00
|
|
|
this._userSettings.updateRole(user).then(() => {
|
|
|
|
this.getUsersList();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-27 17:09:47 +02:00
|
|
|
deleteUser(user: UserDTO) {
|
2016-07-09 15:08:36 +02:00
|
|
|
this._userSettings.deleteUser(user).then(() => {
|
|
|
|
this.getUsersList();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|