Browse Source

Fixed deletion of user's account due to invite codes.

Added button to initiate user search on admin page.
Add ability to delete user from admin page.
tags/3.0.0
Teknikode 1 year ago
parent
commit
c8b0c1624c

+ 1
- 0
Teknik/Areas/Admin/AdminAreaRegistration.cs View File

@@ -70,6 +70,7 @@ namespace Teknik.Areas.Admin
"~/Areas/Admin/Scripts/UploadSearch.js"));

BundleTable.Bundles.Add(new CdnScriptBundle("~/bundles/UserInfo", config.CdnHost).Include(
"~/Scripts/bootbox/bootbox.min.js",
"~/Areas/Admin/Scripts/UserInfo.js"));
}
}

+ 23
- 0
Teknik/Areas/Admin/Controllers/AdminController.cs View File

@@ -151,5 +151,28 @@ namespace Teknik.Areas.Admin.Controllers
}
return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DeleteAccount(string username)
{
try
{
using (TeknikEntities db = new TeknikEntities())
{
User user = UserHelper.GetUser(db, username);
if (user != null)
{
UserHelper.DeleteAccount(db, Config, user);
return Json(new { result = true });
}
}
}
catch (Exception ex)
{
return Json(new { error = ex.GetFullMessage(true) });
}
return Json(new { error = "Unable to delete user" });
}
}
}

+ 21
- 0
Teknik/Areas/Admin/Scripts/UserInfo.js View File

@@ -64,4 +64,25 @@ $(function () {
}
});
});

$('#delete_account').click(function () {
bootbox.confirm("Are you sure you want to delete this account?", function (result) {
if (result) {
$.ajax({
type: "POST",
url: deleteUserURL,
data: AddAntiForgeryToken({ username: username }),
success: function (response) {
if (response.result) {
window.location.replace(homeUrl);
}
else {
$("#top_msg").css('display', 'inline', 'important');
$("#top_msg").html('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + parseErrorMessage(response) + '</div>');
}
}
});
}
});
});
});

+ 2
- 2
Teknik/Areas/Admin/Scripts/UserSearch.js View File

@@ -1,6 +1,6 @@
$(document).ready(function () {
$('#Query').on('input', function (e) {
query = $(this).val();
$('#search').click(function () {
query = $('#query').val();
$.ajax({
type: "POST",
url: userSearchResultsURL,

+ 17
- 9
Teknik/Areas/Admin/Views/Admin/UserInfo.cshtml View File

@@ -4,6 +4,8 @@

<script>
// We need to define the action URLs for the script
var homeUrl = '@Url.SubRouteUrl("admin", "Admin.UserSearch")';
var deleteUserURL = '@Url.SubRouteUrl("admin", "Admin.Action", new { action = "DeleteAccount" })';
var editAccountType = '@Url.SubRouteUrl("admin", "Admin.Action", new { action = "EditUserAccountType" })';
var editAccountStatus = '@Url.SubRouteUrl("admin", "Admin.Action", new { action = "EditUserAccountStatus" })';
var createInviteCode = '@Url.SubRouteUrl("admin", "Admin.Action", new { action = "CreateInviteCode" })';
@@ -29,10 +31,10 @@
<div class="col-sm-8">
<select class="userAccountType">
@{
foreach (AccountType value in Enum.GetValues(typeof(AccountType)))
{
<option @(value == Model.AccountType ? "selected" : string.Empty)>@value.ToString()</option>
}
foreach (AccountType value in Enum.GetValues(typeof(AccountType)))
{
<option @(value == Model.AccountType ? "selected" : string.Empty)>@value.ToString()</option>
}
}
</select>
</div>
@@ -45,10 +47,10 @@
<div class="col-sm-8">
<select class="userAccountStatus">
@{
foreach (AccountStatus value in Enum.GetValues(typeof(AccountStatus)))
{
<option @(value == Model.AccountStatus ? "selected" : string.Empty)>@value.ToString()</option>
}
foreach (AccountStatus value in Enum.GetValues(typeof(AccountStatus)))
{
<option @(value == Model.AccountStatus ? "selected" : string.Empty)>@value.ToString()</option>
}
}
</select>
</div>
@@ -56,7 +58,13 @@
<br />
<div class="row">
<div class="col-sm-2 col-sm-offset-1">
<button type="button" class="list-group-item btn-info" id="createInviteCode">Create Invite Code</button>
<button type="button" class="btn btn-info" id="createInviteCode">Create Invite Code</button>
</div>
</div>
<br />
<div class="row">
<div class="col-sm-2 col-sm-offset-1">
<button type="button" class="btn btn-danger" id="delete_account">Delete Account</button>
</div>
</div>
</div>

+ 4
- 1
Teknik/Areas/Admin/Views/Admin/UserSearch.cshtml View File

@@ -14,10 +14,13 @@
<div class="col-sm-6 col-sm-offset-3">
<form>
<div class="form-group center-block">
<input type="text" class="form-control" id="Query" name="Query" placeholder="Username" />
<input type="text" class="form-control" id="query" name="query" placeholder="Username" />
</div>
</form>
</div>
<div class="col-sm-2">
<button type="button" class="btn btn-info text-center" id="search">Search</button>
</div>
</div>
<div class="row">
<div class="col-sm-10 col-sm-offset-1">

+ 18
- 13
Teknik/Areas/User/Utility/UserHelper.cs View File

@@ -572,7 +572,7 @@ namespace Teknik.Areas.Users.Utility
{
// Update uploads
List<Upload.Models.Upload> uploads = db.Uploads.Where(u => u.User.Username == user.Username).ToList();
if (uploads != null)
if (uploads.Any())
{
foreach (Upload.Models.Upload upload in uploads)
{
@@ -584,7 +584,7 @@ namespace Teknik.Areas.Users.Utility

// Update pastes
List<Paste.Models.Paste> pastes = db.Pastes.Where(u => u.User.Username == user.Username).ToList();
if (pastes != null)
if (pastes.Any())
{
foreach (Paste.Models.Paste paste in pastes)
{
@@ -596,7 +596,7 @@ namespace Teknik.Areas.Users.Utility

// Update shortened urls
List<ShortenedUrl> shortUrls = db.ShortenedUrls.Where(u => u.User.Username == user.Username).ToList();
if (shortUrls != null)
if (shortUrls.Any())
{
foreach (ShortenedUrl shortUrl in shortUrls)
{
@@ -608,7 +608,7 @@ namespace Teknik.Areas.Users.Utility

// Update vaults
List<Vault.Models.Vault> vaults = db.Vaults.Where(u => u.User.Username == user.Username).ToList();
if (vaults != null)
if (vaults.Any())
{
foreach (Vault.Models.Vault vault in vaults)
{
@@ -628,7 +628,7 @@ namespace Teknik.Areas.Users.Utility

// Delete post comments
List<BlogPostComment> postComments = db.BlogComments.Where(u => u.User.Username == user.Username).ToList();
if (postComments != null)
if (postComments.Any())
{
foreach (BlogPostComment postComment in postComments)
{
@@ -639,7 +639,7 @@ namespace Teknik.Areas.Users.Utility

// Delete podcast comments
List<Podcast.Models.PodcastComment> podComments = db.PodcastComments.Where(u => u.User.Username == user.Username).ToList();
if (podComments != null)
if (podComments.Any())
{
foreach (Podcast.Models.PodcastComment podComment in podComments)
{
@@ -650,7 +650,7 @@ namespace Teknik.Areas.Users.Utility

// Delete Recovery Email Verifications
List<RecoveryEmailVerification> verCodes = db.RecoveryEmailVerifications.Where(r => r.User.Username == user.Username).ToList();
if (verCodes != null)
if (verCodes.Any())
{
foreach (RecoveryEmailVerification verCode in verCodes)
{
@@ -661,7 +661,7 @@ namespace Teknik.Areas.Users.Utility

// Delete Password Reset Verifications
List<ResetPasswordVerification> verPass = db.ResetPasswordVerifications.Where(r => r.User.Username == user.Username).ToList();
if (verPass != null)
if (verPass.Any())
{
foreach (ResetPasswordVerification ver in verPass)
{
@@ -671,9 +671,10 @@ namespace Teknik.Areas.Users.Utility
}

// Delete Owned Invite Codes
if (user.OwnedInviteCodes != null)
List<InviteCode> ownedCodes = db.InviteCodes.Where(i => i.Owner.Username == user.Username).ToList();
if (ownedCodes.Any())
{
foreach (InviteCode code in user.OwnedInviteCodes)
foreach (InviteCode code in ownedCodes)
{
db.InviteCodes.Remove(code);
}
@@ -681,15 +682,19 @@ namespace Teknik.Areas.Users.Utility
}

// Delete Claimed Invite Code
if (user.ClaimedInviteCode != null)
List<InviteCode> claimedCodes = db.InviteCodes.Where(i => i.ClaimedUser.Username == user.Username).ToList();
if (claimedCodes.Any())
{
db.InviteCodes.Remove(user.ClaimedInviteCode);
foreach (InviteCode code in claimedCodes)
{
db.InviteCodes.Remove(code);
}
db.SaveChanges();
}

// Delete Auth Tokens
List<AuthToken> authTokens = db.AuthTokens.Where(t => t.User.UserId == user.UserId).ToList();
if (authTokens != null)
if (authTokens.Any())
{
foreach (AuthToken authToken in authTokens)
{

Loading…
Cancel
Save