'use strict';
const _ = require('lodash');
var roles = {
name: 'roles'
, label: 'Roles - Groups of People, Devices, etc'
, pluginType: 'admin'
};
function init () {
return roles;
}
module.exports = init;
var $status = null;
roles.actions = [{
description: 'Each role will have a 1 or more permissions. The * permission is a wildcard, permissions are a hierarchy using : as a seperator.'
, buttonLabel: 'Add new Role'
, init: function init (client, callback) {
$status = $('#admin_' + roles.name + '_0_status');
$status.hide().text(client.translate('Loading database ...')).fadeIn('slow');
var table = $('
').css('margin-top', '10px');
$('#admin_' + roles.name + '_0_html').append(table).append(genDialog(client));
reload(client, callback);
}
, preventClose: true
, code: function createNewRole (client, callback) {
var role = {};
openDialog(role, client, callback);
}
}];
function createOrSaveRole (role, client, callback) {
var method = _.isEmpty(role._id) ? 'POST' : 'PUT';
$.ajax({
method: method
, url: '/api/v2/authorization/roles/'
, headers: client.headers()
, data: role
}).done(function success () {
reload(client, callback);
}).fail(function fail (err) {
console.error('Unable to ' + method + ' Role', err.responseText);
window.alert(client.translate('Unable to %1 Role', { params: [method] }));
if (callback) {
callback(err);
}
});
}
function deleteRole (role, client, callback) {
$.ajax({
method: 'DELETE'
, url: '/api/v2/authorization/roles/' + role._id
, headers: client.headers()
}).done(function success () {
reload(client, callback);
}).fail(function fail (err) {
console.error('Unable to delete Role', err.responseText);
window.alert(client.translate('Unable to delete Role'));
if (callback) {
callback(err);
}
});
}
function reload (client, callback) {
$.ajax({
method: 'GET'
, url: '/api/v2/authorization/roles'
, headers: client.headers()
}).done(function success (records) {
roles.records = records;
$status.hide().text(client.translate('Database contains %1 roles', { params: [records.length] })).fadeIn('slow');
showRoles(records, client);
if (callback) {
callback();
}
}).fail(function fail (err) {
$status.hide().text(client.translate('Error loading database')).fadeIn('slow');
roles.records = [];
if (callback) {
callback(err);
}
});
}
function genDialog (client) {
var ret =
'' +
' ' +
client.translate('Name') +
' ' +
' ' +
' ' +
' ' + client.translate('Permissions') + ' ' +
' ' +
' ' +
' ' + client.translate('Additional Notes, Comments') + ' ' +
' ' +
'
';
return $(ret);
}
function openDialog (role, client) {
$('#editroledialog').dialog({
width: 360
, height: 360
, buttons: [
{
text: client.translate('Save')
, class: 'leftButton'
, click: function() {
role.name = $('#edrole_name').val();
role.permissions =
_.chain($('#edrole_permissions').val().toLowerCase().split(/[;, ]/))
.map(_.trim)
.reject(_.isEmpty)
.sort()
.value();
role.notes = $('#edrole_notes').val();
var self = this;
delete role.autoGenerated;
createOrSaveRole(role, client, function callback () {
$(self).dialog('close');
});
}
}
, {
text: client.translate('Cancel')
, click: function() { $(this).dialog('close'); }
}
]
, open: function() {
$(this).parent().css('box-shadow', '20px 20px 20px 0px black');
$(this).parent().find('.ui-dialog-buttonset').css({ 'width': '100%', 'text-align': 'right' });
$(this).parent().find('button:contains("' + client.translate('Save') + '")').css({ 'float': 'left' });
$('#edrole_name').val(role.name || '').focus();
$('#edrole_permissions').val(role.permissions ? role.permissions.join(' ') : '');
$('#edrole_notes').val(role.notes || '');
}
});
}
function showRole (role, table, client) {
var editIcon = $(' ');
editIcon.click(function clicked () {
openDialog(role, client);
});
var deleteIcon = '';
if (role._id) {
deleteIcon = $(' ');
deleteIcon.click(function clicked () {
var ok = window.confirm(client.translate('Are you sure you want to delete: ') + role.name);
if (ok) {
deleteRole(role, client);
}
});
}
table.append($('').css('background-color', '#0f0f0f')
.append($('').attr('width', '20%').append(editIcon).append(deleteIcon).append(role.name))
.append($(' ').attr('width', '20%').append(_.isEmpty(role.permissions) ? '[none]' : role.permissions.join(' ')))
.append($(' ').attr('width', '10%').append(role._id ? (role.notes ? role.notes : '') : '[system default]'))
);
}
function showRoles (roles, client) {
var table = $('#admin_roles_table');
table.empty().append($(' ').css('background', '#040404')
.append($('').css('width', '100px').attr('align', 'left').append(client.translate('Name')))
.append($(' ').css('width', '150px').attr('align', 'left').append(client.translate('Permissions')))
.append($(' ').css('width', '150px').attr('align', 'left').append(client.translate('Notes')))
);
for (var t = 0; t < roles.length; t++) {
showRole(roles[t], table, client);
}
}