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

77 lines
2.2 KiB
TypeScript
Raw Normal View History

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