/** * ====================================================================== * LICENSE: This file is subject to the terms and conditions defined in * * file 'license.txt', which is part of this source code package. * * ====================================================================== */ /** * * @param {type} $ * @returns {undefined} */ (function ($) { /** * * @param {type} id * @param {type} btn * @param {type} rowAction * @returns {undefined} */ function switchToUser(id, btn, rowAction) { $.ajax(getLocal().ajaxurl, { type: 'POST', dataType: 'json', data: { action: 'aam', sub_action: 'Subject_User.switchToUser', _ajax_nonce: getLocal().nonce, user: id }, beforeSend: function () { $(btn).attr( 'class', 'icon-spin4 animate-spin ' + (rowAction ? 'aam-row-action' : 'aam-switch-user') ); }, success: function (response) { if (response.status === 'success') { location.href = response.redirect; } else { getAAM().notification('danger', response.reason); } }, error: function () { getAAM().notification('danger'); }, complete: function () { $(btn).attr( 'class', 'icon-exchange ' + (rowAction ? 'aam-row-action text-success' : 'aam-switch-user') ); } }); } /** * * @param {type} id * @param {type} btn * @returns {undefined} */ function applyPolicy(subject, policyId, effect, btn) { //show indicator if (typeof btn !== 'function') { $(btn).attr('class', 'aam-row-action icon-spin4 animate-spin'); } getAAM().queueRequest(function () { $.ajax(getLocal().ajaxurl, { type: 'POST', dataType: 'json', data: { action: 'aam', sub_action: 'Main_Policy.save', subject: subject.type, subjectId: subject.id, _ajax_nonce: getLocal().nonce, id: policyId, effect: effect }, success: function (response) { if (typeof btn === 'function') { btn(response); } else { if (response.status === 'success') { if (effect) { $(btn).attr('class', 'aam-row-action text-success icon-check'); } else { $(btn).attr('class', 'aam-row-action text-muted icon-check-empty'); } } else { if (effect) { getAAM().notification( 'danger', getAAM().__('Failed to apply policy changes') ); $(btn).attr('class', 'aam-row-action text-muted icon-check-empty'); } else { $(btn).attr('class', 'aam-row-action text-success icon-check'); } } } }, error: function () { getAAM().notification('danger'); } }); }); } /** * * @param {type} selected * @returns {undefined} */ function loadRoleList(selected, target) { target = (typeof target === 'undefined' ? '#expiration-change-role' : target); $.ajax(getLocal().ajaxurl, { type: 'POST', dataType: 'json', data: { action: 'aam', sub_action: 'Subject_Role.getList', _ajax_nonce: getLocal().nonce }, beforeSend: function () { $(target).html( '' ); }, success: function (response) { $(target).html( '' ); for (var i in response) { $(target).append( '' ); } $(target).val(selected); } }); } /** * * @returns {undefined} */ function UI() { /** * Role List Interface * * @param {jQuery} $ * * @returns {void} */ (function ($) { /** * * @param {type} id * @returns {Boolean} */ function isCurrent(id) { var subject = getAAM().getSubject(); return (subject.type === 'role' && subject.id === id); } /** * * @param {type} exclude */ function fetchRoleList(exclude) { $.ajax(getLocal().ajaxurl, { type: 'POST', dataType: 'json', data: { action: 'aam', sub_action: 'Subject_Role.getList', _ajax_nonce: getLocal().nonce, exclude: exclude }, beforeSend: function () { $('.inherit-role-list').html( '' ); }, success: function (response) { $('.inherit-role-list').html( '' ); for (var i in response) { $('.inherit-role-list').append( '' ); } if ($.aamEditRole) { $('#inherit-role').val($.aamEditRole[0]); } getAAM().triggerHook('post-get-role-list', { list : response }); //TODO - Rewrite JavaScript to support $.aam $.aamEditRole = null; } }); } /** * * @param {type} container * @returns {undefined} */ function resetForm(container) { $('input,select', container).each(function() { if ($(this).attr('type') === 'checkbox') { $(this).prop('checked', false); } else { $(this).val(''); } }); } //initialize the role list table $('#role-list').DataTable({ autoWidth: false, ordering: true, dom: 'ftrip', pagingType: 'simple', processing: true, stateSave: true, serverSide: false, ajax: { url: getLocal().ajaxurl, type: 'POST', dataType: 'json', data: { action: 'aam', sub_action: 'Subject_Role.getTable', _ajax_nonce: getLocal().nonce, subject: getAAM().getSubject().type, subjectId: getAAM().getSubject().id, ui: getLocal().ui, id: $('#object-id').val() } }, columnDefs: [ {visible: false, targets: [0, 1, 4]}, {orderable: false, targets: [0, 1, 3, 4]} ], language: { search: '_INPUT_', searchPlaceholder: getAAM().__('Search Role'), info: getAAM().__('_TOTAL_ role(s)'), infoFiltered: '' }, initComplete: function () { if (getAAM().isUI('main') && getLocal().caps.create_roles) { var create = $('', { 'href': '#', 'class': 'btn btn-primary' }) .html('') .bind('click', function () { resetForm('#add-role-modal .modal-body'); $('#add-role-modal').modal('show'); }) .attr({ 'data-toggle': "tooltip", 'title': getAAM().__('Create New Role') }); $('.dataTables_filter', '#role-list_wrapper').append(create); } }, createdRow: function (row, data) { if (isCurrent(data[0])) { $('td:eq(0)', row).html( '' + data[2] + '' ); } else { $('td:eq(0)', row).html('' + data[2] + ''); } $(row).attr('data-id', data[0]); //add subtitle $('td:eq(0)', row).append( $('', {'class': 'aam-row-subtitle'}).html( getAAM().applyFilters( 'role-subtitle', getAAM().__('Users') + ': ' + parseInt(data[1]) + '; ID: ' + data[0] + '', data ) ) ); var actions = data[3].split(','); var container = $('
', {'class': 'aam-row-actions'}); $.each(actions, function (i, action) { switch (action) { case 'manage': $(container).append($('', { 'class': 'aam-row-action icon-cog ' + (isCurrent(data[0]) ? 'text-muted': 'text-info') }).bind('click', function () { if (!$(this).prop('disabled')) { $(this).prop('disabled', true); var title = $('td:eq(0) span', row).html(); getAAM().setSubject('role', data[0], title, data[4]); $('td:eq(0) span', row).replaceWith( '' + title + '' ); $('i.icon-cog', container).attr( 'class', 'aam-row-action icon-cog text-muted' ); if (getAAM().isUI('main')) { $('i.icon-cog', container).attr( 'class', 'aam-row-action icon-spin4 animate-spin' ); getAAM().fetchContent('main'); $('i.icon-spin4', container).attr( 'class', 'aam-row-action icon-cog text-muted' ); } else { getAAM().fetchPartial('postform', function(content) { $('#metabox-post-access-form').html(content); getAAM().loadAccessForm( $('#load-post-object-type').val(), $('#load-post-object').val(), $(this) ); }); } } }).attr({ 'data-toggle': "tooltip", 'title': getAAM().__('Manage Role') }).prop('disabled', (isCurrent(data[0]) ? true: false))); break; case 'edit': if (getAAM().isUI('main')) { $(container).append($('', { 'class': 'aam-row-action icon-pencil text-warning' }).bind('click', function () { resetForm('#edit-role-modal .modal-body'); $('#edit-role-btn').data('role', data[0]); $('#edit-role-name').val(data[2]); $('#edit-role-modal').modal('show'); fetchRoleList(data[0]); //TODO - Rewrite JavaScript to support $.aam $.aamEditRole = data; getAAM().triggerHook('edit-role-modal', data); }).attr({ 'data-toggle': "tooltip", 'title': getAAM().__('Edit Role') })); } break; case 'no-edit': if (getAAM().isUI('main')) { $(container).append($('', { 'class': 'aam-row-action icon-pencil text-muted' })); } break; case 'clone': if (getAAM().isUI('main')) { $(container).append($('', { 'class': 'aam-row-action icon-clone text-success' }).bind('click', function () { //TODO - Rewrite JavaScript to support $.aam $.aamEditRole = data; $('#clone-role').prop('checked', true); $('#add-role-modal').modal('show'); }).attr({ 'data-toggle': "tooltip", 'title': getAAM().__('Clone Role') })); } break; case 'no-clone': if (getAAM().isUI('main')) { $(container).append($('', { 'class': 'aam-row-action icon-clone text-muted' })); } break; case 'delete': if (getAAM().isUI('main')) { $(container).append($('', { 'class': 'aam-row-action icon-trash-empty text-danger' }).bind('click', {role: data}, function (event) { $('#delete-role-btn').data('role', data[0]); var message = $('#delete-role-modal .aam-confirm-message').data('message'); $('#delete-role-modal .aam-confirm-message').html( message.replace( '%s', '' + event.data.role[2] + '' ) ); $('#delete-role-modal').modal('show'); }).attr({ 'data-toggle': "tooltip", 'title': getAAM().__('Delete Role') })); } break; case 'no-delete': if (getAAM().isUI('main')) { $(container).append($('', { 'class': 'aam-row-action icon-trash-empty text-muted' })); } break; case 'attach': if (getAAM().isUI('principal')) { $(container).append($('', { 'class': 'aam-row-action icon-check-empty' }).bind('click', function() { applyPolicy( { type: 'role', id: data[0] }, $('#object-id').val(), ($(this).hasClass('icon-check-empty') ? 1 : 0), this ); })); } break; case 'no-attach': if (getAAM().isUI('principal')) { $(container).append($('', { 'class': 'aam-row-action icon-check-empty text-muted' })); } break; case 'detach': if (getAAM().isUI('principal')) { $(container).append($('', { 'class': 'aam-row-action icon-check text-success' }).bind('click', function() { applyPolicy( { type: 'role', id: data[0] }, $('#object-id').val(), ($(this).hasClass('icon-check') ? 0 : 1), this ); })); } break; case 'no-detach': if (getAAM().isUI('principal')) { $(container).append($('', { 'class': 'aam-row-action icon-check text-muted' })); } break; default: if (getAAM().isUI('main')) { getAAM().triggerHook('role-action', { container: container, action : action, data : data }); } break; } }); $('td:eq(1)', row).html(container); getAAM().triggerHook('decorate-role-row', { row : row, data: data }); } }); $('#role-list').on( 'draw.dt', function () { $('tr', '#role-list tbody').each(function() { if (!isCurrent($(this).data('id'))) { $('td:eq(0) strong', this).replaceWith( '' + $('td:eq(0) strong', this).text() + '' ); $('.icon-cog.text-muted', this).attr('disabled', false); $('.icon-cog.text-muted', this).toggleClass('text-muted text-info'); } }); } ); $('#add-role-modal').on('shown.bs.modal', function (e) { fetchRoleList(); //clear add role form first $('input[name="name"]', '#add-role-modal').val('').focus(); }); $('#edit-role-modal').on('shown.bs.modal', function (e) { $('input[name="name"]', '#edit-role-modal').focus(); }); //add role button $('#add-role-btn').bind('click', function () { var _this = this; $('input[name="name"]', '#add-role-modal').parent().removeClass('has-error'); var data = { action: 'aam', sub_action: 'Subject_Role.add', _ajax_nonce: getLocal().nonce }; $('input,select', '#add-role-modal .modal-body').each(function() { if ($(this).attr('name')) { if ($(this).attr('type') === 'checkbox') { data[$(this).attr('name')] = $(this).prop('checked') ? 1 : 0; } else { data[$(this).attr('name')] = $.trim($(this).val()); } } }); if (data.name) { $.ajax(getLocal().ajaxurl, { type: 'POST', dataType: 'json', data: data, beforeSend: function () { $(_this).text(getAAM().__('Saving...')).attr('disabled', true); }, success: function (response) { if (response.status === 'success') { $('#role-list').DataTable().ajax.reload(); getAAM().setSubject( 'role', response.role.id, response.role.name, response.role.level ); getAAM().fetchContent('main'); } else { getAAM().notification( 'danger', response.reason ); } }, error: function () { getAAM().notification('danger'); }, complete: function () { $('#add-role-modal').modal('hide'); $(_this).text(getAAM().__('Add Role')).attr('disabled', false); } }); } else { $('input[name="name"]', '#add-role-modal').focus().parent().addClass('has-error'); } }); //edit role button $('#edit-role-btn').bind('click', function () { var _this = this; $('#edit-role-name').parent().removeClass('has-error'); var data = { action: 'aam', sub_action: 'Subject_Role.edit', _ajax_nonce: getLocal().nonce, subject: 'role', subjectId: $(_this).data('role') }; $('input,select', '#edit-role-modal .modal-body').each(function() { if ($(this).attr('name')) { if ($(this).attr('type') === 'checkbox') { data[$(this).attr('name')] = $(this).prop('checked') ? 1 : 0; } else { data[$(this).attr('name')] = $.trim($(this).val()); } } }); if (data.name) { $.ajax(getLocal().ajaxurl, { type: 'POST', dataType: 'json', data: data, beforeSend: function () { $(_this).text(getAAM().__('Saving...')).attr('disabled', true); }, success: function (response) { if (response.status === 'success') { $('#role-list').DataTable().ajax.reload(); } else { getAAM().notification( 'danger', getAAM().__('Failed to update role') ); } }, error: function () { getAAM().notification('danger'); }, complete: function () { $('#edit-role-modal').modal('hide'); $(_this).text(getAAM().__('Update')).attr('disabled', false); } }); } else { $('#edit-role-name').focus().parent().addClass('has-error'); } }); //edit role button $('#delete-role-btn').bind('click', function () { var _this = this; $.ajax(getLocal().ajaxurl, { type: 'POST', dataType: 'json', data: { action: 'aam', sub_action: 'Subject_Role.delete', _ajax_nonce: getLocal().nonce, subject: 'role', subjectId: $(_this).data('role') }, beforeSend: function () { $(_this).text(getAAM().__('Deleting...')).attr('disabled', true); }, success: function (response) { if (response.status === 'success') { $('#role-list').DataTable().ajax.reload(); } else { getAAM().notification('danger', getAAM().__('Failed to delete role')); } }, error: function () { getAAM().notification('danger'); }, complete: function () { $('#delete-role-modal').modal('hide'); $(_this).text(getAAM().__('Delete Role')).attr('disabled', false); } }); }); //add setSubject hook getAAM().addHook('setSubject', function () { //clear highlight $('tbody tr', '#role-list').each(function () { if ($('strong', $(this)).length) { var highlight = $('strong', $(this)); $('.icon-cog', $(this)).toggleClass('text-muted text-info'); $('.icon-cog', $(this)).prop('disabled', false); highlight.replaceWith($('').text(highlight.text())); } }); }); //in case interface needed to be reloaded getAAM().addHook('refresh', function () { $('#role-list').DataTable().ajax.url(getLocal().ajaxurl).load(); getAAM().fetchContent('main'); }); })(jQuery); /** * User List Interface * * @param {jQuery} $ * * @returns {void} */ (function ($) { /** * * @param {type} id * @returns {Boolean} */ function isCurrent(id) { var subject = getAAM().getSubject(); return (subject.type === 'user' && parseInt(subject.id) === id); } /** * * @param {type} id * @param {type} btn * @returns {undefined} */ function blockUser(id, btn) { var state = ($(btn).hasClass('icon-lock') ? 0 : 1); $.ajax(getLocal().ajaxurl, { type: 'POST', dataType: 'json', data: { action: 'aam', sub_action: 'Subject_User.block', _ajax_nonce: getLocal().nonce, subject: 'user', subjectId: id }, beforeSend: function () { $(btn).attr('class', 'aam-row-action icon-spin4 animate-spin'); }, success: function (response) { if (response.status === 'success') { if (state === 1) { $(btn).attr({ 'class': 'aam-row-action icon-lock text-danger', 'title': getAAM().__('Unlock User'), 'data-original-title': getAAM().__('Unlock User') }); } else { $(btn).attr({ 'class': 'aam-row-action icon-lock-open-alt text-warning', 'title': getAAM().__('Lock User'), 'data-original-title': getAAM().__('Lock User') }); } } else { getAAM().notification('danger', getAAM().__('Failed to block user')); } }, error: function () { getAAM().notification('danger'); } }); } /** * * @param {*} expires * @param {*} action */ function generateJWT() { if ($('#login-url-preview').length === 1) { // Build the trigger var trigger = { action: $('#action-after-expiration').val() } if (trigger.action === 'change-role') { trigger.role = $('#expiration-change-role').val(); } $.ajax(getLocal().ajaxurl, { type: 'POST', dataType: 'json', data: { action: 'aam', sub_action: 'Subject_User.generateJwt', _ajax_nonce: getLocal().nonce, subject: 'user', subjectId: $('#reset-user-expiration-btn').attr('data-user-id'), expires: $('#user-expires').val(), trigger: trigger }, beforeSend: function () { $('#login-url-preview').val(getAAM().__('Generating URL...')); }, success: function (response) { if (response.status === 'success') { $('#login-url-preview').val( $('#login-url-preview').data('url').replace('%s', response.jwt) ); $('#login-jwt').val(response.jwt); } else { getAAM().notification( 'danger', getAAM().__('Failed to generate JWT token') ); } }, error: function () { getAAM().notification('danger'); } }); } } //initialize the user list table $('#user-list').DataTable({ autoWidth: false, ordering: true, dom: 'ftrip', stateSave: true, pagingType: 'simple', serverSide: true, processing: true, ajax: { url: getLocal().ajaxurl, type: 'POST', dataType: 'json', data: function(params) { params.action = 'aam'; params.sub_action = 'Subject_User.getTable'; params._ajax_nonce = getLocal().nonce; params.role = $('#user-list-filter').val(); params.subject = getAAM().getSubject().type; params.subjectId = getAAM().getSubject().id; params.ui = getLocal().ui; params.id = $('#object-id').val(); return params; } }, columnDefs: [ {visible: false, targets: [0, 1, 4, 5]}, {orderable: false, targets: [0, 1, 3, 4, 5]} ], language: { search: '_INPUT_', searchPlaceholder: getAAM().__('Search User'), info: getAAM().__('_TOTAL_ user(s)'), infoFiltered: '' }, initComplete: function () { if (getAAM().isUI('main') && getLocal().caps.create_users) { var create = $('', { 'href': '#', 'class': 'btn btn-primary' }) .html(' ') .bind('click', function () { window.open(getLocal().url.addUser, '_blank'); }) .attr({ 'data-toggle': "tooltip", 'title': getAAM().__('Create New User') }); $('.dataTables_filter', '#user-list_wrapper').append(create); var filter = $('