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

146 lines
3.9 KiB
JavaScript
Raw Normal View History

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;
2023-01-31 13:57:24 +00:00
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 = 'change-password';
params.oldPassword = this.oldPassword;
} else
method = 'setPassword';
2023-01-31 13:57:24 +00:00
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() {
2023-01-31 13:57:24 +00:00
return this.$http.post(`Accounts`, {id: this.id})
.then(() => this.onSwitchAccount(true));
}
onDisableAccount() {
2023-01-31 13:57:24 +00:00
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));
});
}
2023-11-16 22:07:26 +00:00
onSync() {
2023-11-16 23:08:29 +00:00
const params = {force: true};
if (this.shouldSyncPassword)
params.password = this.syncPassword;
2023-11-16 22:07:26 +00:00
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: '<'
}
});