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

123 lines
3.6 KiB
TypeScript
Raw Normal View History

2017-07-08 18:43:42 +08:00
import {Component, OnInit, ViewChild} from "@angular/core";
2016-12-27 06:36:38 +08:00
import {AuthenticationService} from "../../model/network/authentication.service";
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";
2017-07-08 18:43:42 +08:00
import {ModalDirective} from "ngx-bootstrap/modal";
2017-07-09 18:03:17 +08:00
import {NavigationService} from "../../model/navigation.service";
2017-07-15 18:47:11 +08:00
import {NotificationService} from "../../model/notification.service";
import {ErrorCodes, ErrorDTO} from "../../../../common/entities/Error";
@Component({
selector: 'settings-usermanager',
templateUrl: './usermanager.settings.component.html',
2017-07-15 18:47:11 +08:00
styleUrls: ['./usermanager.settings.component.css',
'./../_abstract/abstract.settings.component.css'],
providers: [UserManagerSettingsService],
})
export class UserMangerSettingsComponent implements OnInit {
2017-07-08 18:43:42 +08:00
@ViewChild('userModal') public childModal: ModalDirective;
public newUser = <UserDTO>{};
public userRoles: Array<any> = [];
public users: Array<UserDTO> = [];
2017-07-15 18:47:11 +08:00
public enabled = true;
public error: string = null;
public inProgress = false;
2017-07-15 18:47:11 +08:00
constructor(private _authService: AuthenticationService,
private _navigation: NavigationService,
private _userSettings: UserManagerSettingsService,
private notification: NotificationService) {
}
2017-07-15 18:47:11 +08:00
ngOnInit() {
2017-07-09 18:03:17 +08:00
if (!this._authService.isAuthenticated() ||
this._authService.user.value.role < UserRoles.Admin) {
this._navigation.toLogin();
return;
}
2017-07-08 18:43:42 +08:00
this.userRoles = Utils
.enumToArray(UserRoles)
2017-07-09 18:03:17 +08:00
.filter(r => r.key != UserRoles.LimitedGuest)
2017-07-08 18:43:42 +08:00
.filter(r => r.key <= this._authService.user.value.role)
.sort((a, b) => a.key - b.key);
2017-07-15 18:47:11 +08:00
this.getSettings();
this.getUsersList();
}
2017-07-04 01:17:49 +08:00
private async getUsersList() {
2017-07-15 18:47:11 +08:00
try {
this.users = await this._userSettings.getUsers();
} catch (err) {
this.users = [];
if ((<ErrorDTO>err).code != ErrorCodes.USER_MANAGEMENT_DISABLED) {
throw err;
}
}
}
private async getSettings() {
this.enabled = await this._userSettings.getSettings();
}
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};
2017-07-08 18:43:42 +08:00
this.childModal.show();
}
2017-07-08 18:43:42 +08:00
async addNewUser() {
await this._userSettings.createUser(this.newUser);
await this.getUsersList();
this.childModal.hide();
}
2017-07-08 18:43:42 +08:00
async updateRole(user: UserDTO) {
await this._userSettings.updateRole(user);
await this.getUsersList();
this.childModal.hide();
}
2017-07-08 18:43:42 +08:00
async deleteUser(user: UserDTO) {
await this._userSettings.deleteUser(user);
await this.getUsersList();
this.childModal.hide();
}
2017-07-15 18:47:11 +08:00
async switched(event: { previousValue: false, currentValue: true }) {
this.inProgress = true;
this.error = "";
this.enabled = event.currentValue;
try {
await this._userSettings.updateSettings(this.enabled);
await this.getSettings();
if (this.enabled == true) {
this.notification.success('Password protection enabled', "Success");
this.getUsersList();
} else {
this.notification.success('Password protection disabled', "Success");
}
} catch (err) {
console.log(err);
if (err.message) {
this.error = (<ErrorDTO>err).message;
}
}
this.inProgress = false;
}
}