From 1dc88658e8966b0ca3f0d4c636f96b5854a07f95 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 4 Jan 2018 18:32:25 +1100 Subject: [PATCH] Updates how the user.controller saves a user and instead of using the contentEditingHelper we just call the code ourselves which is actually much simpler. Then once the user is saved we call any extendedSave methods that have been registered. --- src/SolutionInfo.cs | 2 +- .../src/views/users/user.controller.js | 98 +++++++++++++------ src/Umbraco.Web/Editors/UsersController.cs | 1 + 3 files changed, 70 insertions(+), 31 deletions(-) diff --git a/src/SolutionInfo.cs b/src/SolutionInfo.cs index a3d52dd9e8..f6ca8ba95f 100644 --- a/src/SolutionInfo.cs +++ b/src/SolutionInfo.cs @@ -12,4 +12,4 @@ using System.Resources; [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyFileVersion("7.8.0")] -[assembly: AssemblyInformationalVersion("7.8.0-beta")] \ No newline at end of file +[assembly: AssemblyInformationalVersion("7.8.0-beta001")] \ No newline at end of file diff --git a/src/Umbraco.Web.UI.Client/src/views/users/user.controller.js b/src/Umbraco.Web.UI.Client/src/views/users/user.controller.js index f2376e3acc..dfd809431f 100644 --- a/src/Umbraco.Web.UI.Client/src/views/users/user.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/users/user.controller.js @@ -1,7 +1,7 @@ (function () { "use strict"; - function UserEditController($scope, $timeout, $location, $routeParams, formHelper, usersResource, userService, contentEditingHelper, localizationService, notificationsService, mediaHelper, Upload, umbRequestHelper, usersHelper, authResource, dateHelper) { + function UserEditController($scope, $q, $timeout, $location, $routeParams, formHelper, usersResource, userService, contentEditingHelper, localizationService, notificationsService, mediaHelper, Upload, umbRequestHelper, usersHelper, authResource, dateHelper) { var vm = this; @@ -33,6 +33,9 @@ vm.unlockUser = unlockUser; vm.clearAvatar = clearAvatar; vm.save = save; + //a list of methods invoked with promises during the save operation, each must have a key + vm.extendedSaveMethods = {}; + vm.toggleChangePassword = toggleChangePassword; function init() { @@ -117,38 +120,73 @@ function save() { - vm.page.saveButtonState = "busy"; - vm.user.resetPasswordValue = null; + if (formHelper.submitForm({ scope: $scope, statusMessage: vm.labels.saving })) { - //anytime a user is changing another user's password, we are in effect resetting it so we need to set that flag here - if(vm.user.changePassword) { - vm.user.changePassword.reset = !vm.user.changePassword.oldPassword && !vm.user.isCurrentUser; + //anytime a user is changing another user's password, we are in effect resetting it so we need to set that flag here + if (vm.user.changePassword) { + vm.user.changePassword.reset = !vm.user.changePassword.oldPassword && !vm.user.isCurrentUser; + } + + vm.page.saveButtonState = "busy"; + vm.user.resetPasswordValue = null; + + usersResource.saveUser(vm.user) + .then(function (saved) { + + //if the user saved, then try to execute all extended save options + extendedSave().then(function(result) { + //if all is good, then reset the form + formHelper.resetForm({ scope: $scope, notifications: saved.notifications }); + }, function(err) { + //otherwise show the notifications for the user being saved + formHelper.showNotifications(saved); + }); + + vm.user = saved; + setUserDisplayState(); + formatDatesToLocal(vm.user); + + vm.changePasswordModel.isChanging = false; + //the user has a password if they are not states: Invited, NoCredentials + vm.changePasswordModel.config.hasPassword = vm.user.userState !== 3 && vm.user.userState !== 4; + + vm.page.saveButtonState = "success"; + + }, function (err) { + + contentEditingHelper.handleSaveError({ + redirectOnFailure: false, + err: err + }); + //show any notifications + if (err.data) { + formHelper.showNotifications(err.data); + } + vm.page.saveButtonState = "error"; + }); } - - contentEditingHelper.contentEditorPerformSave({ - statusMessage: vm.labels.saving, - saveMethod: usersResource.saveUser, - scope: $scope, - content: vm.user, - // We do not redirect on failure for users - this is because it is not possible to actually save a user - // when server side validation fails - as opposed to content where we are capable of saving the content - // item if server side validation fails - redirectOnFailure: false, - rebindCallback: function (orignal, saved) { } - }).then(function (saved) { - - vm.user = saved; - setUserDisplayState(); - formatDatesToLocal(vm.user); - - vm.changePasswordModel.isChanging = false; - vm.page.saveButtonState = "success"; - - //the user has a password if they are not states: Invited, NoCredentials - vm.changePasswordModel.config.hasPassword = vm.user.userState !== 3 && vm.user.userState !== 4; - }, function (err) { - vm.page.saveButtonState = "error"; + } + + function extendedSave() { + //create a promise for each save method + var savePromises = {}; + angular.forEach(vm.extendedSaveMethods, function (val, key) { + var deferred = $q.defer(); + savePromises[key] = deferred; }); + var allPromises = _.map(savePromises, function (p) { + return p.promise; + }) + + //await all promises to complete + var resultPromise = $q.all(allPromises); + + //execute all promises by passing them to the save methods + angular.forEach(vm.extendedSaveMethods, function (func, key) { + func(savePromises[key]); + }); + + return resultPromise; } function goToPage(ancestor) { diff --git a/src/Umbraco.Web/Editors/UsersController.cs b/src/Umbraco.Web/Editors/UsersController.cs index e4f79a21a6..2e6cd5dbbf 100644 --- a/src/Umbraco.Web/Editors/UsersController.cs +++ b/src/Umbraco.Web/Editors/UsersController.cs @@ -495,6 +495,7 @@ namespace Umbraco.Web.Editors /// /// /// + [OutgoingEditorModelEvent] public async Task PostSaveUser(UserSave userSave) { if (userSave == null) throw new ArgumentNullException("userSave");