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

75 lines
2.2 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";
import {User, UserRoles} from "../../../../common/entities/User";
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 {
private newUser = new User();
2016-12-27 06:36:38 +08:00
private userRoles: Array<any> = [];
private users: Array<User> = [];
2016-12-27 06:36:38 +08:00
constructor(private _authService: AuthenticationService, private _router: Router, private _userSettings: UserManagerSettingsService) {
}
ngOnInit() {
if (!this._authService.isAuthenticated() || this._authService.getUser().role < UserRoles.Admin) {
this._router.navigate(['Login']);
return;
}
this.userRoles = Utils.enumToArray(UserRoles).filter(r => r.key <= this._authService.getUser().role);
this.getUsersList();
}
private getUsersList() {
2016-12-27 06:36:38 +08:00
this._userSettings.getUsers().then((result: Message<Array<User>>) => {
this.users = result.result;
});
}
2016-12-27 06:36:38 +08:00
canModifyUser(user: User): boolean {
let currentUser = this._authService.getUser();
if (!currentUser) {
return false;
}
return currentUser.name != user.name && currentUser.role >= user.role;
}
initNewUser() {
this.newUser = new User();
this.newUser.role = UserRoles.User;
}
addNewUser() {
this._userSettings.createUser(this.newUser).then(() => {
this.getUsersList();
});
}
2016-12-27 06:36:38 +08:00
updateRole(user: User) {
this._userSettings.updateRole(user).then(() => {
this.getUsersList();
});
}
2016-12-27 06:36:38 +08:00
deleteUser(user: User) {
this._userSettings.deleteUser(user).then(() => {
this.getUsersList();
});
}
}