1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/settings/usermanager/usermanager.settings.component.ts

71 lines
1.9 KiB
TypeScript
Raw Normal View History

import {Component, OnInit} from "@angular/core";
2016-12-27 06:36:38 +08:00
import {AuthenticationService} from "../../model/network/authentication.service";
import {Router} from "@angular/router";
2016-12-27 23:09:47 +08:00
import {UserDTO, UserRoles} from "../../../../common/entities/UserDTO";
import {Utils} from "../../../../common/Utils";
import {UserManagerSettingsService} from "./usermanager.settings.service";
@Component({
selector: 'settings-usermanager',
templateUrl: './usermanager.settings.component.html',
styleUrls: ['./usermanager.settings.component.css'],
providers: [UserManagerSettingsService],
})
export class UserMangerSettingsComponent implements OnInit {
public newUser = <UserDTO>{};
public userRoles: Array<any> = [];
public users: Array<UserDTO> = [];
constructor(private _authService: AuthenticationService, private _router: Router, private _userSettings: UserManagerSettingsService) {
}
ngOnInit() {
2017-07-04 01:17:49 +08:00
if (!this._authService.isAuthenticated() || this._authService.user.value.role < UserRoles.Admin) {
this._router.navigate(['login']);
return;
}
2017-07-04 01:17:49 +08:00
this.userRoles = Utils.enumToArray(UserRoles).filter(r => r.key <= this._authService.user.value.role);
this.getUsersList();
}
2017-07-04 01:17:49 +08:00
private async getUsersList() {
this.users = await this._userSettings.getUsers();
}
canModifyUser(user: UserDTO): boolean {
2017-07-04 01:17:49 +08: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};
}
addNewUser() {
this._userSettings.createUser(this.newUser).then(() => {
this.getUsersList();
});
}
updateRole(user: UserDTO) {
this._userSettings.updateRole(user).then(() => {
this.getUsersList();
});
}
deleteUser(user: UserDTO) {
this._userSettings.deleteUser(user).then(() => {
this.getUsersList();
});
}
}