class UserRole { constructor() { this._table = this._prepTbl(); this._prepBtn(); this._form = document.querySelector("#userRoleForm"); this._initEventListeners(); } _prepTbl() { const dtable = $('#user_role_list').DataTable({ "autoWidth": false, "responsive": true, "processing": true, //Feature control the processing indicator. "order": [0], //Initial no order. "oLanguage": fr_onload_lang.oLanguage, "serverSide": true, "ajax": { url: app.get_vars().baseurl+"user_role/fetch", type: "POST" }, "columnDefs":[ { "targets":[0], "visible": false, "searchable": false } ], "columns": [ // { "data": "event_order" }, { "data": "role_id", render : function(data, type, full, meta){ return full.role_id; }}, { "data": "name", render : function(data, type, full, meta){ return full.name; }}, { "data": "action" , "orderable": false, render : function(data, type, full, meta) { let btns = ""; if (authorize(PAGE_CODE['sys_settings'], PRIVS[PAGE_CODE['sys_settings']]['user_role_edit'])) { btns += ``; } if (authorize(PAGE_CODE['sys_settings'], PRIVS[PAGE_CODE['sys_settings']]['user_role_delete'])) { btns += ` `; } return btns; } }, { "data": "privs", render : function(data, type, full, meta){ let privs = JSON.parse(full.privs); let privTbl = `
| ${PAGE_NAME[key]} | `; privTbl += ``;
privTbl += `
| `;
privTbl += `
Enregistrez les changements ?
', "_continue": function() { userRoleObj.saveRole(); }, "id": "user_role_conf_modal", "className": "user_role_conf_modal" }); }); $(document).on('click', '#delete-role', function() { if(USER_ROLE == this.dataset.roleId) { app._notify('warning', 'Cannot delete own user role'); } else { userRoleObj.deleteRole(this.dataset.roleId, this.dataset.role) } }); this._addBtn.addEventListener('click', () => { this._form.reset(); /* Set modal title*/ $('#userRoleModalTitle').html("Ajouter un nouveau rôle d'utilisateur"); /* remove role id */ $('input#role_id').remove(); $('#userRoleModal').modal("show"); }); /* Edit user role modal */ let form = this._form; $(document).on('click', '#toggle-edit-modal', function(event) { event.preventDefault(); if(USER_ROLE == this.dataset.roleId) { app._notify('warning', 'Cannot edit own user role'); } else { /* Act on the event */ $('#userRoleModalTitle').html("Modifier le rôle de l'utilisateur"); $('#userRoleModal').modal("show"); /* Add the role id */ $(`#${form.id}`).append(``); /* Check all that match the user privileges*/ form.reset(); $('input#role').val(this.dataset.role); const privs = JSON.parse(this.dataset.privs); Object.getOwnPropertyNames(privs).forEach(key => { privs[key].forEach((el) => { let privName = key+el; $(`#${privName}`).prop("checked", true); }); //use key and value here }); $(`[type=checkbox]`).trigger("change"); } }); } verifyRolePageAccess( page ) { // These are the menu sidebar in BO page that have submenu/subpages const pages = ['email', 'sys_set', 'pers']; return pages.includes(page) } saveRole() { let formData = new FormData(document.querySelector("#userRoleForm")); let data = { privs: {} }; this.verifyRolePageAccess() /* Store privileges per section in an array, store everything else as string */ for (let [key, value] of formData.entries()) { if (["role", "role_id"].indexOf(key) != -1) { data[key] = value; } else { if (!(data.privs[key] instanceof Array)) { data.privs[key] = []; } /** * This is the remedy to pages that have submenu, when no page is assigned to a role, * Meaning All the submenu is not accessible,the main menu as well should not be displayed. * * This is applicable to this main menu ['email', 'sys_set', 'pers'] * * email = (page = 8) * sys_set = (page = 18) * pers = (view = 0) */ if ( this.verifyRolePageAccess(key) && !data.privs[key].includes(value) ) { let val = 18 if ( key === 'email') val = 8 else if (key === 'pers') val = 0 data.privs[key].push(parseInt(val)) } data.privs[key].push(parseInt(value)); } } let table = this._table; let form = this._form; let url = app.get_vars().baseurl+"user_role/add"; if (data.role_id) { url = app.get_vars().baseurl+"user_role/update"; } $.ajax({ url : url, data : data, type: "POST", success : function(response) { app._notify(response.mtype, response.message); if (response.mtype == 'success') { table.ajax.reload(); form.reset(); $('#userRoleModal').modal("hide"); } } }); } deleteRole (roleId, roleName) { app.modal.confirm_box({ "message" : "Delete user role "+" "+roleName+"?