From bef2ec628094265a64ac02a083d5ead657d129f5 Mon Sep 17 00:00:00 2001 From: Uncled1023 Date: Sat, 9 Apr 2016 11:52:14 -0700 Subject: [PATCH] Fixed issue where deletions were failing if email was missing, or git user didnt exist. Fixed issue where shortened URLs weren't being updated from deleted user. --- .../Profile/Controllers/ProfileController.cs | 45 ++++++++++++++++--- Teknik/Areas/Profile/Scripts/Profile.js | 9 +++- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/Teknik/Areas/Profile/Controllers/ProfileController.cs b/Teknik/Areas/Profile/Controllers/ProfileController.cs index 6691634..cc6c9a0 100644 --- a/Teknik/Areas/Profile/Controllers/ProfileController.cs +++ b/Teknik/Areas/Profile/Controllers/ProfileController.cs @@ -6,6 +6,7 @@ using System.Runtime.InteropServices; using System.Web; using System.Web.Mvc; using System.Web.Security; +using Teknik.Areas.Shortener.Models; using Teknik.Areas.Blog.Models; using Teknik.Areas.Error.Controllers; using Teknik.Areas.Error.ViewModels; @@ -361,21 +362,40 @@ namespace Teknik.Areas.Profile.Controllers app.Authenticate(Config.EmailConfig.Username, Config.EmailConfig.Password); var domain = app.Domains.ItemByName[Config.EmailConfig.Domain]; var account = domain.Accounts.ItemByAddress[string.Format("{0}@{1}", User.Identity.Name, Config.EmailConfig.Domain)]; - account.Delete(); + if (account != null) + { + account.Delete(); + } } catch (COMException) - { } + { + } + catch (Exception) + { + return Json(new { error = "Unable to delete email account." }); + } } // Delete Git if (Config.GitConfig.Enabled) { - Uri baseUri = new Uri(Config.GitConfig.Host); - Uri finalUri = new Uri(baseUri, "api/v1/admin/users/" + User.Identity.Name + "?token=" + Config.GitConfig.AccessToken); - WebRequest request = WebRequest.Create(finalUri); - request.Method = "DELETE"; + try + { + Uri baseUri = new Uri(Config.GitConfig.Host); + Uri finalUri = new Uri(baseUri, "api/v1/admin/users/" + User.Identity.Name + "?token=" + Config.GitConfig.AccessToken); + WebRequest request = WebRequest.Create(finalUri); + request.Method = "DELETE"; - HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + if (response.StatusCode != HttpStatusCode.NotFound && response.StatusCode != HttpStatusCode.OK) + { + return Json(new { error = "Unable to delete git account. Response Code: " + response.StatusCode }); + } + } + catch (Exception) + { + return Json(new { error = "Unable to delete git account." }); + } } // Update uploads @@ -400,6 +420,17 @@ namespace Teknik.Areas.Profile.Controllers } } + // Update shortened urls + List shortUrls = db.ShortenedUrls.Include("User").Where(u => u.User.Username == User.Identity.Name).ToList(); + if (shortUrls != null) + { + foreach (ShortenedUrl shortUrl in shortUrls) + { + shortUrl.UserId = null; + db.Entry(shortUrl).State = EntityState.Modified; + } + } + // Delete Blogs Blog.Models.Blog blog = db.Blogs.Include("BlogPosts").Include("BlogPosts.Comments").Include("User").Where(u => u.User.Username == User.Identity.Name).FirstOrDefault(); if (blog != null) diff --git a/Teknik/Areas/Profile/Scripts/Profile.js b/Teknik/Areas/Profile/Scripts/Profile.js index ca45e41..f842368 100644 --- a/Teknik/Areas/Profile/Scripts/Profile.js +++ b/Teknik/Areas/Profile/Scripts/Profile.js @@ -14,8 +14,15 @@ window.location.replace(homeUrl); } else { + errorMsg = html; + if (html.error) { + errorMsg = html.error; + if (html.error.message) { + errorMsg = html.error.message; + } + } $("#top_msg").css('display', 'inline', 'important'); - $("#top_msg").html('
Unable to delete your account. Please contact an Administrator.
'); + $("#top_msg").html('
' + errorMsg + '
'); } } });