2018-03-31 03:30:30 +08:00
|
|
|
import {Component, OnInit, ViewChild} from '@angular/core';
|
|
|
|
import {AuthenticationService} from '../../model/network/authentication.service';
|
|
|
|
import {UserDTO, UserRoles} from '../../../../common/entities/UserDTO';
|
|
|
|
import {Utils} from '../../../../common/Utils';
|
|
|
|
import {UserManagerSettingsService} from './usermanager.settings.service';
|
|
|
|
import {ModalDirective} from 'ngx-bootstrap/modal';
|
|
|
|
import {NavigationService} from '../../model/navigation.service';
|
|
|
|
import {NotificationService} from '../../model/notification.service';
|
|
|
|
import {ErrorCodes, ErrorDTO} from '../../../../common/entities/Error';
|
|
|
|
import {I18n} from '@ngx-translate/i18n-polyfill';
|
2016-07-09 21:08:36 +08:00
|
|
|
|
|
|
|
@Component({
|
2018-05-04 07:17:08 +08:00
|
|
|
selector: 'app-settings-usermanager',
|
2017-06-11 04:32:56 +08:00
|
|
|
templateUrl: './usermanager.settings.component.html',
|
2017-07-15 18:47:11 +08:00
|
|
|
styleUrls: ['./usermanager.settings.component.css',
|
|
|
|
'./../_abstract/abstract.settings.component.css'],
|
2017-06-11 04:32:56 +08:00
|
|
|
providers: [UserManagerSettingsService],
|
2016-07-09 21:08:36 +08:00
|
|
|
})
|
|
|
|
export class UserMangerSettingsComponent implements OnInit {
|
2017-07-08 18:43:42 +08:00
|
|
|
@ViewChild('userModal') public childModal: ModalDirective;
|
2017-06-11 04:32:56 +08:00
|
|
|
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;
|
2016-07-09 21:08:36 +08:00
|
|
|
|
2018-03-30 08:30:23 +08:00
|
|
|
|
|
|
|
text = {
|
2018-03-31 03:30:30 +08:00
|
|
|
Enabled: 'Enabled',
|
|
|
|
Disabled: 'Disabled',
|
|
|
|
Low: 'Low',
|
|
|
|
High: 'High'
|
2018-03-30 08:30:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-07-15 18:47:11 +08:00
|
|
|
constructor(private _authService: AuthenticationService,
|
|
|
|
private _navigation: NavigationService,
|
|
|
|
private _userSettings: UserManagerSettingsService,
|
2018-03-30 08:30:23 +08:00
|
|
|
private notification: NotificationService,
|
|
|
|
public i18n: I18n) {
|
2018-03-31 03:30:30 +08:00
|
|
|
this.text.Enabled = i18n('Enabled');
|
|
|
|
this.text.Disabled = i18n('Disabled');
|
|
|
|
this.text.Low = i18n('Low');
|
|
|
|
this.text.High = i18n('High');
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-07-09 21:08:36 +08:00
|
|
|
|
2017-07-15 18:47:11 +08:00
|
|
|
|
2017-06-11 04:32:56 +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();
|
2017-06-11 04:32:56 +08:00
|
|
|
return;
|
2016-07-09 21:08:36 +08:00
|
|
|
}
|
2017-07-08 18:43:42 +08:00
|
|
|
this.userRoles = Utils
|
|
|
|
.enumToArray(UserRoles)
|
2018-05-13 00:19:51 +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();
|
2017-06-11 04:32:56 +08:00
|
|
|
this.getUsersList();
|
|
|
|
}
|
2016-07-09 21:08:36 +08:00
|
|
|
|
2018-05-13 00:19:51 +08:00
|
|
|
canModifyUser(user: UserDTO): boolean {
|
|
|
|
const currentUser = this._authService.user.value;
|
|
|
|
if (!currentUser) {
|
|
|
|
return false;
|
2017-07-15 18:47:11 +08:00
|
|
|
}
|
2018-05-13 00:19:51 +08:00
|
|
|
|
|
|
|
return currentUser.name !== user.name && currentUser.role >= user.role;
|
2017-07-15 18:47:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private async getSettings() {
|
|
|
|
this.enabled = await this._userSettings.getSettings();
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-07-09 21:08:36 +08:00
|
|
|
|
2018-05-13 00:19:51 +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(this.i18n('Password protection enabled'), this.i18n('Success'));
|
|
|
|
this.getUsersList();
|
|
|
|
} else {
|
|
|
|
this.notification.success(this.i18n('Password protection disabled'), this.i18n('Success'));
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
if (err.message) {
|
|
|
|
this.error = (<ErrorDTO>err).message;
|
|
|
|
}
|
2016-07-09 21:08:36 +08:00
|
|
|
}
|
2018-05-13 00:19:51 +08:00
|
|
|
this.inProgress = false;
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-07-09 21:08:36 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
initNewUser() {
|
|
|
|
this.newUser = <UserDTO>{role: UserRoles.User};
|
2017-07-08 18:43:42 +08:00
|
|
|
this.childModal.show();
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-07-09 21:08:36 +08:00
|
|
|
|
2017-07-08 18:43:42 +08:00
|
|
|
async addNewUser() {
|
|
|
|
await this._userSettings.createUser(this.newUser);
|
|
|
|
await this.getUsersList();
|
|
|
|
this.childModal.hide();
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-07-09 21:08:36 +08:00
|
|
|
|
2017-07-08 18:43:42 +08:00
|
|
|
async updateRole(user: UserDTO) {
|
|
|
|
await this._userSettings.updateRole(user);
|
|
|
|
await this.getUsersList();
|
|
|
|
this.childModal.hide();
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
|
|
|
|
2017-07-08 18:43:42 +08:00
|
|
|
async deleteUser(user: UserDTO) {
|
|
|
|
await this._userSettings.deleteUser(user);
|
|
|
|
await this.getUsersList();
|
|
|
|
this.childModal.hide();
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2017-07-15 18:47:11 +08:00
|
|
|
|
2018-05-13 00:19:51 +08:00
|
|
|
private async getUsersList() {
|
2017-07-15 18:47:11 +08:00
|
|
|
try {
|
2018-05-13 00:19:51 +08:00
|
|
|
this.users = await this._userSettings.getUsers();
|
2017-07-15 18:47:11 +08:00
|
|
|
} catch (err) {
|
2018-05-13 00:19:51 +08:00
|
|
|
this.users = [];
|
|
|
|
if ((<ErrorDTO>err).code !== ErrorCodes.USER_MANAGEMENT_DISABLED) {
|
|
|
|
throw err;
|
2017-07-15 18:47:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-09 21:08:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|