/* Copyright (c) 2018 [Glacies UG, Berlin, Germany] (http://glacies.de) Developer: Thilina Hasantha (http://lk.linkedin.com/in/thilinah | https://github.com/thilinah) */ import FormValidation from '../../../api/FormValidation'; import AdapterBase from '../../../api/AdapterBase'; import ReactModalAdapterBase from '../../../api/ReactModalAdapterBase'; class UserAdapter extends AdapterBase { getDataMapping() { return [ 'id', 'username', 'email', 'employee', 'user_level', ]; } getHeaders() { return [ { sTitle: 'ID' }, { sTitle: 'User Name' }, { sTitle: 'Authentication Email' }, { sTitle: 'Employee' }, { sTitle: 'User Level' }, ]; } getFormFields() { return [ ['id', { label: 'ID', type: 'hidden', validation: '' }], ['username', { label: 'User Name', type: 'text', validation: 'username' }], ['email', { label: 'Email', type: 'text', validation: 'email' }], ['employee', { label: 'Employee', type: 'select2', 'allow-null': true, 'remote-source': ['Employee', 'id', 'first_name+last_name'], }], ['user_level', { label: 'User Level', type: 'select', source: [ ['Admin', 'Admin'], ['Manager', 'Manager'], ['Employee', 'Employee'], ['Restricted Admin', 'Restricted Admin'], ['Restricted Manager', 'Restricted Manager'], ['Restricted Employee', 'Restricted Employee'], ], }, ], ['user_roles', { label: 'User Roles', type: 'select2multi', 'remote-source': ['UserRole', 'id', 'name'] }], ['lang', { label: 'Language', type: 'select2', 'allow-null': true, 'remote-source': ['SupportedLanguage', 'id', 'description'], }], ['default_module', { label: 'Default Module', type: 'select2', 'null-label': 'No Default Module', 'allow-null': true, 'remote-source': ['Module', 'id', 'name', 'getUserModules'], }], ]; } postRenderForm(object, $tempDomObj) { if (object == null || object === undefined) { $tempDomObj.find('#changePasswordBtn').remove(); } } changePassword() { $('#adminUsersModel').modal('show'); $('#adminUsersChangePwd #newpwd').val(''); $('#adminUsersChangePwd #conpwd').val(''); $('#adminUsersChangePwd_error').hide(); } saveUserSuccessCallBack(callBackData, serverData) { const user = callBackData[0]; if (callBackData[1]) { this.showMessage('Create User', `An email has been sent to ${user.email} with a temporary password to login to IceHrm.`); } else { this.showMessage('Create User', 'User created successfully. But there was a problem sending welcome email.'); } this.get([]); } saveUserFailCallBack(callBackData, serverData) { this.showMessage('Error', callBackData); } doCustomValidation(params) { let msg = null; if ((params.user_level !== 'Admin' && params.user_level !== 'Restricted Admin') && params.employee === 'NULL') { msg = 'For this user type, you have to assign an employee when adding or editing the user.
'; msg += " You may create a new employee through 'Admin'->'Employees' menu"; } return msg; } save() { const validator = new FormValidation(`${this.getTableName()}_submit`, true, { ShowPopup: false, LabelErrorClass: 'error' }); if (validator.checkValues()) { const params = validator.getFormParameters(); const msg = this.doCustomValidation(params); if (msg == null) { const id = $(`#${this.getTableName()}_submit #id`).val(); params.csrf = $(`#${this.getTableName()}Form`).data('csrf'); if (id != null && id !== undefined && id !== '') { params.id = id; this.add(params, []); } else { const reqJson = JSON.stringify(params); const callBackData = []; callBackData.callBackData = []; callBackData.callBackSuccess = 'saveUserSuccessCallBack'; callBackData.callBackFail = 'saveUserFailCallBack'; this.customAction('saveUser', 'admin=users', reqJson, callBackData); } } else { // $("#"+this.getTableName()+'Form .label').html(msg); // $("#"+this.getTableName()+'Form .label').show(); this.showMessage('Error Saving User', msg); } } } changePasswordConfirm() { $('#adminUsersChangePwd_error').hide(); const password = $('#adminUsersChangePwd #newpwd').val(); const conPassword = $('#adminUsersChangePwd #conpwd').val(); if (conPassword !== password) { $('#adminUsersChangePwd_error').html("Passwords don't match"); $('#adminUsersChangePwd_error').show(); return; } const validatePasswordResult = this.validatePassword(password); if (validatePasswordResult != null) { $('#adminUsersChangePwd_error').html(validatePasswordResult); $('#adminUsersChangePwd_error').show(); return; } const req = { id: this.currentId, pwd: conPassword }; const reqJson = JSON.stringify(req); const callBackData = []; callBackData.callBackData = []; callBackData.callBackSuccess = 'changePasswordSuccessCallBack'; callBackData.callBackFail = 'changePasswordFailCallBack'; this.customAction('changePassword', 'admin=users', reqJson, callBackData); } closeChangePassword() { $('#adminUsersModel').modal('hide'); } changePasswordSuccessCallBack(callBackData, serverData) { this.closeChangePassword(); this.showMessage('Password Change', 'Password changed successfully'); } changePasswordFailCallBack(callBackData, serverData) { this.closeChangePassword(); this.showMessage('Error', callBackData); } } /** * UserRoleAdapter */ class UserRoleAdapter extends ReactModalAdapterBase { constructor(endPoint, tab, filter, orderBy) { super(endPoint, tab, filter, orderBy); this.tables = []; } getDataMapping() { return [ 'id', 'name', ]; } getHeaders() { return [ { sTitle: 'ID', bVisible: false }, { sTitle: 'Name' }, ]; } getTableColumns() { return [ { title: 'ID', dataIndex: 'id', sorter: true, }, { title: 'Name', dataIndex: 'name', sorter: true, }, ]; } setTables(tables) { this.tables = tables; } getFormFields() { return [ ['id', { label: 'ID', type: 'hidden' }], ['name', { label: 'Name', type: 'text', validation: '' }], ['additional_permissions', { label: 'Additional Permissions', type: 'datagroup', form: [ ['table', { label: 'Table', type: 'select2', source: this.tables, }, ], ['permissions', { label: 'Permissions', type: 'select2multi', 'allow-null': true, source: [ ['get', 'List'], ['element', 'Get Details'], ['save', 'Add/Edit'], ['delete', 'Delete'], ], }, ], ], columns: [ { title: 'Table', dataIndex: 'table', key: 'table', }, { title: 'Permissions', dataIndex: 'permissions', key: 'permissions', }, ], validation: 'none', }], ]; } } module.exports = { UserAdapter, UserRoleAdapter, };