From 27d7bc2ae1c69f02262932c6ceb7b8d14097067f Mon Sep 17 00:00:00 2001 From: Shannon Date: Fri, 28 Jul 2017 16:02:46 +1000 Subject: [PATCH] Adds ability to remove gravatar --- src/Umbraco.Core/Models/UserExtensions.cs | 10 ++++++++-- src/Umbraco.Web/Editors/UsersController.cs | 20 +++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Umbraco.Core/Models/UserExtensions.cs b/src/Umbraco.Core/Models/UserExtensions.cs index b087d6cee1..9fd97585c8 100644 --- a/src/Umbraco.Core/Models/UserExtensions.cs +++ b/src/Umbraco.Core/Models/UserExtensions.cs @@ -23,7 +23,13 @@ namespace Umbraco.Core.Models /// A list of 5 different sized avatar URLs /// internal static string[] GetCurrentUserAvatarUrls(this IUser user, IUserService userService, ICacheProvider staticCache) - { + { + //check if the user has explicitly removed all avatars including a gravatar, this will be possible and the value will be "none" + if (user.Avatar == "none") + { + return new string[0]; + } + if (user.Avatar.IsNullOrWhiteSpace()) { var gravatarHash = user.Email.ToMd5(); @@ -60,7 +66,7 @@ namespace Umbraco.Core.Models }; } - return null; + return new string[0]; } //use the custom avatar diff --git a/src/Umbraco.Web/Editors/UsersController.cs b/src/Umbraco.Web/Editors/UsersController.cs index 0039128e86..e77bd2b2e4 100644 --- a/src/Umbraco.Web/Editors/UsersController.cs +++ b/src/Umbraco.Web/Editors/UsersController.cs @@ -149,12 +149,26 @@ namespace Umbraco.Web.Editors var filePath = found.Avatar; - found.Avatar = null; + //if the filePath is already null it will mean that the user doesn't have a custom avatar and their gravatar is currently + //being used (if they have one). This means they want to remove their gravatar too which we can do by setting a special value + //for the avatar. + if (filePath.IsNullOrWhiteSpace() == false) + { + found.Avatar = null; + } + else + { + //set a special value to indicate to not have any avatar + found.Avatar = "none"; + } Services.UserService.Save(found); - if (FileSystemProviderManager.Current.MediaFileSystem.FileExists(filePath)) - FileSystemProviderManager.Current.MediaFileSystem.DeleteFile(filePath); + if (filePath.IsNullOrWhiteSpace() == false) + { + if (FileSystemProviderManager.Current.MediaFileSystem.FileExists(filePath)) + FileSystemProviderManager.Current.MediaFileSystem.DeleteFile(filePath); + } return Request.CreateResponse(HttpStatusCode.OK, found.GetCurrentUserAvatarUrls(Services.UserService, ApplicationContext.ApplicationCache.StaticCache)); }