salix/modules/account/front/descriptor/index.js

146 lines
3.9 KiB
JavaScript

import ngModule from '../module';
import Descriptor from 'salix/components/descriptor';
import UserError from 'core/lib/user-error';
class Controller extends Descriptor {
get user() {
return this.entity;
}
set user(value) {
this.entity = value;
}
get entity() {
return super.entity;
}
set entity(value) {
super.entity = value;
this.hasAccount = null;
if (!value) return;
this.$http.get(`Accounts/${value.id}/exists`)
.then(res => this.hasAccount = res.data.exists);
}
loadData() {
const filter = {
where: {id: this.$params.id},
include: {
relation: 'role',
scope: {
fields: ['id', 'name']
}
}
};
return Promise.all([
this.$http.get(`VnUsers/preview`, {filter})
.then(res => {
const [user] = res.data;
this.user = user;
}),
this.$http.get(`Accounts/${this.$params.id}/exists`)
.then(res => this.hasAccount = res.data.exists)
]);
}
onDelete() {
return this.$http.delete(`VnUsers/${this.id}`)
.then(() => this.$state.go('account.index'))
.then(() => this.vnApp.showSuccess(this.$t('User removed')));
}
onChangePassClick(askOldPass) {
this.$http.get('UserPasswords/findOne')
.then(res => {
this.passRequirements = res.data;
this.askOldPass = askOldPass;
this.$.changePass.show();
});
}
onPassChange() {
if (!this.newPassword)
throw new UserError(`You must enter a new password`);
if (this.newPassword != this.repeatPassword)
throw new UserError(`Passwords don't match`);
let method;
const params = {newPassword: this.newPassword};
if (this.askOldPass) {
method = 'changePassword';
params.oldPassword = this.oldPassword;
} else
method = 'setPassword';
return this.$http.patch(`Accounts/${this.id}/${method}`, params)
.then(() => {
this.emit('change');
this.vnApp.showSuccess(this.$t('Password changed succesfully!'));
});
}
onPassClose() {
this.oldPassword = '';
this.newPassword = '';
this.repeatPassword = '';
this.$.$apply();
}
onEnableAccount() {
return this.$http.post(`Accounts`, {id: this.id})
.then(() => this.onSwitchAccount(true));
}
onDisableAccount() {
return this.$http.delete(`Accounts/${this.id}`)
.then(() => this.onSwitchAccount(false));
}
onSwitchAccount(enable) {
this.hasAccount = enable;
const message = enable
? 'Account enabled!'
: 'Account disabled!';
this.emit('change');
this.vnApp.showSuccess(this.$t(message));
}
onSetActive(active) {
return this.$http.patch(`VnUsers/${this.id}`, {active})
.then(() => {
this.user.active = active;
const message = active
? 'User activated!'
: 'User deactivated!';
this.emit('change');
this.vnApp.showSuccess(this.$t(message));
});
}
onSync() {
const params = {force: true};
if (this.shouldSyncPassword)
params.password = this.syncPassword;
return this.$http.patch(`Accounts/${this.user.name}/sync`, params)
.then(() => this.vnApp.showSuccess(this.$t('User synchronized!')));
}
onSyncClose() {
this.shouldSyncPassword = false;
this.syncPassword = undefined;
}
}
ngModule.component('vnUserDescriptor', {
template: require('./index.html'),
controller: Controller,
bindings: {
user: '<'
}
});