2017-07-08 12:43:42 +02:00
|
|
|
import {Component, OnInit, ViewChild} 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 {UserManagerSettingsService} from "./usermanager.settings.service";
|
2017-07-08 12:43:42 +02:00
|
|
|
import {ModalDirective} from "ngx-bootstrap/modal";
|
2016-07-09 15:08:36 +02:00
|
|
|
|
|
|
|
@Component({
|
2017-06-10 22:32:56 +02:00
|
|
|
selector: 'settings-usermanager',
|
|
|
|
templateUrl: './usermanager.settings.component.html',
|
|
|
|
styleUrls: ['./usermanager.settings.component.css'],
|
|
|
|
providers: [UserManagerSettingsService],
|
2016-07-09 15:08:36 +02:00
|
|
|
})
|
|
|
|
export class UserMangerSettingsComponent implements OnInit {
|
2017-07-08 12:43:42 +02:00
|
|
|
@ViewChild('userModal') public childModal: ModalDirective;
|
2017-06-10 22:32:56 +02:00
|
|
|
public newUser = <UserDTO>{};
|
|
|
|
public userRoles: Array<any> = [];
|
|
|
|
public users: Array<UserDTO> = [];
|
2016-07-09 15:08:36 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
constructor(private _authService: AuthenticationService, private _router: Router, private _userSettings: UserManagerSettingsService) {
|
|
|
|
}
|
2016-07-09 15:08:36 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
ngOnInit() {
|
2017-07-03 19:17:49 +02:00
|
|
|
if (!this._authService.isAuthenticated() || this._authService.user.value.role < UserRoles.Admin) {
|
2017-06-10 22:32:56 +02:00
|
|
|
this._router.navigate(['login']);
|
|
|
|
return;
|
2016-07-09 15:08:36 +02:00
|
|
|
}
|
2017-07-08 12:43:42 +02:00
|
|
|
this.userRoles = Utils
|
|
|
|
.enumToArray(UserRoles)
|
|
|
|
.filter(r => r.key <= this._authService.user.value.role)
|
|
|
|
.sort((a, b) => a.key - b.key);
|
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
this.getUsersList();
|
|
|
|
}
|
2016-07-09 15:08:36 +02:00
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
private async getUsersList() {
|
|
|
|
this.users = await this._userSettings.getUsers();
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
2016-07-09 15:08:36 +02:00
|
|
|
|
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
canModifyUser(user: UserDTO): boolean {
|
2017-07-03 19:17:49 +02:00
|
|
|
let currentUser = this._authService.user.value;
|
2017-06-10 22:32:56 +02:00
|
|
|
if (!currentUser) {
|
|
|
|
return false;
|
2016-07-09 15:08:36 +02:00
|
|
|
}
|
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
return currentUser.name != user.name && currentUser.role >= user.role;
|
|
|
|
}
|
2016-07-09 15:08:36 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
initNewUser() {
|
|
|
|
this.newUser = <UserDTO>{role: UserRoles.User};
|
2017-07-08 12:43:42 +02:00
|
|
|
this.childModal.show();
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
2016-07-09 15:08:36 +02:00
|
|
|
|
2017-07-08 12:43:42 +02:00
|
|
|
async addNewUser() {
|
|
|
|
await this._userSettings.createUser(this.newUser);
|
|
|
|
await this.getUsersList();
|
|
|
|
this.childModal.hide();
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
2016-07-09 15:08:36 +02:00
|
|
|
|
2017-07-08 12:43:42 +02:00
|
|
|
async updateRole(user: UserDTO) {
|
|
|
|
await this._userSettings.updateRole(user);
|
|
|
|
await this.getUsersList();
|
|
|
|
this.childModal.hide();
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
|
|
|
|
2017-07-08 12:43:42 +02:00
|
|
|
async deleteUser(user: UserDTO) {
|
|
|
|
await this._userSettings.deleteUser(user);
|
|
|
|
await this.getUsersList();
|
|
|
|
this.childModal.hide();
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
2016-07-09 15:08:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|