From 2de781a8be214821c8f9ce546dff7c58ace235a4 Mon Sep 17 00:00:00 2001 From: Amos Folz <33728190+amosfolz@users.noreply.github.com> Date: Wed, 19 Jun 2019 14:54:32 +0000 Subject: [PATCH 1/2] Create UpdateUsersTable.php --- .../Migrations/v430/UpdateUsersTable.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 app/sprinkles/account/src/Database/Migrations/v430/UpdateUsersTable.php diff --git a/app/sprinkles/account/src/Database/Migrations/v430/UpdateUsersTable.php b/app/sprinkles/account/src/Database/Migrations/v430/UpdateUsersTable.php new file mode 100644 index 000000000..b419a8474 --- /dev/null +++ b/app/sprinkles/account/src/Database/Migrations/v430/UpdateUsersTable.php @@ -0,0 +1,58 @@ +schema->hasTable('users')) { + $this->schema->table('users', function (Blueprint $table) { + $table->dropIndex(['group_id']); + $table->unsignedInteger('group_id')->comment('The id of the user group.')->nullable()->change(); + }); + } + } + + /** + * {@inheritdoc} + */ + public function down() + { + $this->schema->table('users', function (Blueprint $table) { + $table->unsignedInteger('group_id')->default(1)->comment('The id of the user group.')->nullable(false)->change(); + }); + } +} From 13fa23c1a283bba7d6bd47e85df8d850eb58ffce Mon Sep 17 00:00:00 2001 From: Amos Folz <33728190+amosfolz@users.noreply.github.com> Date: Wed, 19 Jun 2019 19:00:16 +0000 Subject: [PATCH 2/2] Migration, controller, template, and asset updates --- .../Migrations/v430/UpdateUsersTable.php | 3 +- .../assets/userfrosting/js/widgets/users.js | 82 ++++++++++--------- .../admin/src/Controller/UserController.php | 4 + .../admin/templates/forms/user.html.twig | 3 +- 4 files changed, 49 insertions(+), 43 deletions(-) diff --git a/app/sprinkles/account/src/Database/Migrations/v430/UpdateUsersTable.php b/app/sprinkles/account/src/Database/Migrations/v430/UpdateUsersTable.php index b419a8474..94a61e64e 100644 --- a/app/sprinkles/account/src/Database/Migrations/v430/UpdateUsersTable.php +++ b/app/sprinkles/account/src/Database/Migrations/v430/UpdateUsersTable.php @@ -40,8 +40,7 @@ public function up() { if ($this->schema->hasTable('users')) { $this->schema->table('users', function (Blueprint $table) { - $table->dropIndex(['group_id']); - $table->unsignedInteger('group_id')->comment('The id of the user group.')->nullable()->change(); + $table->unsignedInteger('group_id')->default(null)->comment('The id of the user group.')->nullable()->change(); }); } } diff --git a/app/sprinkles/admin/assets/userfrosting/js/widgets/users.js b/app/sprinkles/admin/assets/userfrosting/js/widgets/users.js index 6c05b91c7..9f57d7c5c 100644 --- a/app/sprinkles/admin/assets/userfrosting/js/widgets/users.js +++ b/app/sprinkles/admin/assets/userfrosting/js/widgets/users.js @@ -6,7 +6,7 @@ * Set up the form in a modal after being successfully attached to the body. */ function attachUserForm() { - $("body").on('renderSuccess.ufModal', function (data) { + $("body").on('renderSuccess.ufModal', function(data) { var modal = $(this).ufModal('getModal'); var form = modal.find('.js-form'); @@ -15,6 +15,8 @@ function attachUserForm() { width: '100%' }); + + // Set up the form for submission form.ufForm({ validator: page.validators @@ -42,14 +44,14 @@ function toggleChangePasswordMode(el, userName, changePasswordMode) { if (validator) { //Iterate through named elements inside of the form, and mark them as error free el.find("input[type='password']").each(function() { - validator.successList.push(this); //mark as error free + validator.successList.push(this); //mark as error free }); - validator.resetForm();//remove error class on name elements and clear history - validator.reset();//remove all error and success data + validator.resetForm(); //remove error class on name elements and clear history + validator.reset(); //remove all error and success data } el.find("input[type='password']").closest('.form-group') - .removeClass('has-error has-success'); - el.find('.form-control-feedback').each(function () { + .removeClass('has-error has-success'); + el.find('.form-control-feedback').each(function() { $(this).remove(); }); } else { @@ -66,7 +68,7 @@ function toggleChangePasswordMode(el, userName, changePasswordMode) { * Update user field(s) */ function updateUser(userName, fieldName, fieldValue) { - var data = { + var data = { 'value': fieldValue }; @@ -92,19 +94,19 @@ function updateUser(userName, fieldName, fieldValue) { return $.parseJSON(result); } } catch (e) { - // statements to handle any exceptions - console.log("Warning: Could not parse expected JSON response."); - return {}; + // statements to handle any exceptions + console.log("Warning: Could not parse expected JSON response."); + return {}; } } } - }).fail(function (jqXHR) { + }).fail(function(jqXHR) { // Error messages if (debugAjax && jqXHR.responseText) { document.write(jqXHR.responseText); document.close(); } else { - console.log("Error (" + jqXHR.status + "): " + jqXHR.responseText ); + console.log("Error (" + jqXHR.status + "): " + jqXHR.responseText); // Display errors on failure // TODO: ufAlerts widget should have a 'destroy' method @@ -113,12 +115,12 @@ function updateUser(userName, fieldName, fieldValue) { } else { $("#alerts-page").ufAlerts('clear'); } - + $("#alerts-page").ufAlerts('fetch').ufAlerts('render'); } return jqXHR; - }).done(function (response) { + }).done(function(response) { window.location.reload(); }); } @@ -128,8 +130,8 @@ function updateUser(userName, fieldName, fieldValue) { * @param {module:jQuery} el jQuery wrapped element to target. * @param {{delete_redirect: string}} options Options used to modify behaviour of button actions. */ - function bindUserButtons(el, options) { - if (!options) options = {}; +function bindUserButtons(el, options) { + if (!options) options = {}; /** * Buttons that launch a modal dialog @@ -162,38 +164,38 @@ function updateUser(userName, fieldName, fieldValue) { msgTarget: $("#alerts-page") }); - $("body").on('renderSuccess.ufModal', function (data) { + $("body").on('renderSuccess.ufModal', function(data) { var modal = $(this).ufModal('getModal'); var form = modal.find('.js-form'); // Set up collection widget var roleWidget = modal.find('.js-form-roles'); roleWidget.ufCollection({ - dropdown : { + dropdown: { ajax: { - url : site.uri.public + '/api/roles' + url: site.uri.public + '/api/roles' }, - placeholder : "Select a role" + placeholder: "Select a role" }, dropdownTemplate: modal.find('#user-roles-select-option').html(), - rowTemplate : modal.find('#user-roles-row').html() + rowTemplate: modal.find('#user-roles-row').html() }); // Get current roles and add to widget $.getJSON(site.uri.public + '/api/users/u/' + userName + '/roles') - .done(function (data) { - $.each(data.rows, function (idx, role) { - role.text = role.name; - roleWidget.ufCollection('addRow', role); + .done(function(data) { + $.each(data.rows, function(idx, role) { + role.text = role.name; + roleWidget.ufCollection('addRow', role); + }); }); - }); // Set up form for submission form.ufForm() - .on("submitSuccess.ufForm", function() { - // Reload page on success - window.location.reload(); - }); + .on("submitSuccess.ufForm", function() { + // Reload page on success + window.location.reload(); + }); }); }); @@ -210,7 +212,7 @@ function updateUser(userName, fieldName, fieldValue) { msgTarget: $("#alerts-page") }); - $("body").on('renderSuccess.ufModal', function () { + $("body").on('renderSuccess.ufModal', function() { var modal = $(this).ufModal('getModal'); var form = modal.find('.js-form'); @@ -246,16 +248,16 @@ function updateUser(userName, fieldName, fieldValue) { msgTarget: $("#alerts-page") }); - $("body").on('renderSuccess.ufModal', function () { + $("body").on('renderSuccess.ufModal', function() { var modal = $(this).ufModal('getModal'); var form = modal.find('.js-form'); form.ufForm() - .on("submitSuccess.ufForm", function() { - // Navigate or reload page on success - if (options.delete_redirect) window.location.href = options.delete_redirect; - else window.location.reload(); - }); + .on("submitSuccess.ufForm", function() { + // Navigate or reload page on success + if (options.delete_redirect) window.location.href = options.delete_redirect; + else window.location.reload(); + }); }); }); @@ -269,12 +271,12 @@ function updateUser(userName, fieldName, fieldValue) { updateUser(btn.data('user_name'), 'flag_verified', '1'); }); - el.find('.js-user-enable').click(function () { + el.find('.js-user-enable').click(function() { var btn = $(this); updateUser(btn.data('user_name'), 'flag_enabled', '1'); }); - el.find('.js-user-disable').click(function () { + el.find('.js-user-disable').click(function() { var btn = $(this); updateUser(btn.data('user_name'), 'flag_enabled', '0'); }); @@ -292,4 +294,4 @@ function bindUserCreationButton(el) { attachUserForm(); }); -}; +}; \ No newline at end of file diff --git a/app/sprinkles/admin/src/Controller/UserController.php b/app/sprinkles/admin/src/Controller/UserController.php index caf9caba3..8037af948 100644 --- a/app/sprinkles/admin/src/Controller/UserController.php +++ b/app/sprinkles/admin/src/Controller/UserController.php @@ -1211,6 +1211,10 @@ public function updateInfo(Request $request, Response $response, $args) return $response->withJson([], 400); } + if (isset($data['group_id']) && $data['group_id'] == 0) { + $data['group_id'] = null; + } + // Begin transaction - DB will be rolled back if an exception occurs Capsule::transaction(function () use ($data, $user, $currentUser) { // Update the user and generate success messages diff --git a/app/sprinkles/admin/templates/forms/user.html.twig b/app/sprinkles/admin/templates/forms/user.html.twig index 3ee7fc9d4..707d0c1ae 100644 --- a/app/sprinkles/admin/templates/forms/user.html.twig +++ b/app/sprinkles/admin/templates/forms/user.html.twig @@ -24,7 +24,8 @@ {% if 'group' in form.fields.disabled %} {% else %} - + {% endfor %}