-
Notifications
You must be signed in to change notification settings - Fork 641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support username casing change in Admin UI #9748
Draft
RiadGahlouz
wants to merge
11
commits into
dev
Choose a base branch
from
mgahlouz-admin-ui-username-casing
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f9ccefc
Change controller flow to support casing update
RiadGahlouz edd8110
Remove unnecessary import
RiadGahlouz 0158a73
Move audit to after username change
RiadGahlouz 69aa0b3
Updading username change check to include identity check
RiadGahlouz 0976baa
Update Index.cshtml
RiadGahlouz 84e2929
Update ChangeUsernameController.cs
RiadGahlouz fb21e0e
Added check for owned packages and some tests
RiadGahlouz 44b0ccf
Remove old code
RiadGahlouz c20bc34
Merge validate & change
RiadGahlouz 5b184eb
Delete commented code
RiadGahlouz 29a6035
Fix tests
RiadGahlouz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ | |||||
|
||||||
using System; | ||||||
using System.Collections.Generic; | ||||||
using System.Linq; | ||||||
using System.Net; | ||||||
using System.Text.RegularExpressions; | ||||||
using System.Threading.Tasks; | ||||||
|
@@ -20,6 +21,7 @@ public class ChangeUsernameController : AdminControllerBase | |||||
private readonly IEntitiesContext _entitiesContext; | ||||||
private readonly IDateTimeProvider _dateTimeProvider; | ||||||
private readonly IAuditingService _auditingService; | ||||||
private readonly IPackageService _packageService; | ||||||
|
||||||
private readonly Regex UsernameValidationRegex = new Regex(GalleryConstants.UsernameValidationRegex); | ||||||
|
||||||
|
@@ -28,13 +30,15 @@ public ChangeUsernameController( | |||||
IEntityRepository<User> userRepository, | ||||||
IEntitiesContext entitiesContext, | ||||||
IDateTimeProvider dateTimeProvider, | ||||||
IAuditingService auditingService) | ||||||
IAuditingService auditingService, | ||||||
IPackageService packageService) | ||||||
{ | ||||||
_userService = userService ?? throw new ArgumentNullException(nameof(userService)); | ||||||
_userRepository = userRepository ?? throw new ArgumentNullException(nameof(userRepository)); | ||||||
_entitiesContext = entitiesContext ?? throw new ArgumentNullException(nameof(entitiesContext)); | ||||||
_dateTimeProvider = dateTimeProvider ?? throw new ArgumentNullException(nameof(dateTimeProvider)); | ||||||
_auditingService = auditingService ?? throw new ArgumentNullException(nameof(auditingService)); | ||||||
_packageService = packageService ?? throw new ArgumentNullException(nameof(packageService)); | ||||||
} | ||||||
|
||||||
[HttpGet] | ||||||
|
@@ -83,14 +87,33 @@ public ActionResult VerifyAccount(string accountEmailOrUsername) | |||||
} | ||||||
|
||||||
[HttpGet] | ||||||
public ActionResult ValidateNewUsername(string newUsername) | ||||||
public ActionResult ValidateNewUsername(string newUsername, bool checkOwnedPackages, string oldUsername) | ||||||
{ | ||||||
if (string.IsNullOrEmpty(newUsername)) | ||||||
{ | ||||||
return Json(HttpStatusCode.BadRequest, "Username cannot be null or empty.", JsonRequestBehavior.AllowGet); | ||||||
} | ||||||
|
||||||
var result = ValidateUsername(newUsername); | ||||||
if (string.IsNullOrEmpty(oldUsername)) | ||||||
{ | ||||||
return Json(HttpStatusCode.BadRequest, "Old username cannot be null or empty.", JsonRequestBehavior.AllowGet); | ||||||
} | ||||||
|
||||||
var oldAccount = _userService.FindByUsername(oldUsername); | ||||||
if (oldAccount == null) | ||||||
{ | ||||||
return Json(HttpStatusCode.NotFound, "Old username account was not found.", JsonRequestBehavior.AllowGet); | ||||||
} | ||||||
|
||||||
var result = ValidateUsernameChange(oldAccount, newUsername); | ||||||
|
||||||
if (checkOwnedPackages) | ||||||
{ | ||||||
var ownedPackages = _packageService.FindPackagesByOwner(oldAccount, includeUnlisted: true) | ||||||
.Where(p => p.PackageStatusKey != PackageStatus.Deleted) | ||||||
.Select(p => p.PackageRegistration.Id); | ||||||
result.OwnedPackageIds = ownedPackages; | ||||||
} | ||||||
|
||||||
return Json(result, JsonRequestBehavior.AllowGet); | ||||||
} | ||||||
|
@@ -116,39 +139,44 @@ public async Task<ActionResult> ChangeUsername(string oldUsername, string newUse | |||||
return Json(HttpStatusCode.NotFound, "Old username account was not found.", JsonRequestBehavior.AllowGet); | ||||||
} | ||||||
|
||||||
var newUsernameValidation = ValidateUsername(newUsername); | ||||||
var newUsernameValidation = ValidateUsernameChange(account, newUsername); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
|
||||||
if (!newUsernameValidation.IsFormatValid || !newUsernameValidation.IsAvailable) | ||||||
{ | ||||||
return Json(HttpStatusCode.BadRequest, "New username validation failed.", JsonRequestBehavior.AllowGet); | ||||||
} | ||||||
|
||||||
var newAccountForOldUsername = new User() | ||||||
if (account.Username.Equals(newUsername, StringComparison.OrdinalIgnoreCase) == false) | ||||||
mariaghiondea marked this conversation as resolved.
Show resolved
Hide resolved
RiadGahlouz marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
{ | ||||||
Username = account.Username, | ||||||
EmailAllowed = false, | ||||||
IsDeleted = true, | ||||||
CreatedUtc = _dateTimeProvider.UtcNow | ||||||
}; | ||||||
// We're doing a full username change and not just a casing change so we need to lock the old username | ||||||
var newAccountForOldUsername = new User() | ||||||
{ | ||||||
Username = account.Username, | ||||||
EmailAllowed = false, | ||||||
IsDeleted = true, | ||||||
CreatedUtc = _dateTimeProvider.UtcNow | ||||||
}; | ||||||
|
||||||
_userRepository.InsertOnCommit(newAccountForOldUsername); | ||||||
} | ||||||
|
||||||
account.Username = newUsername; | ||||||
|
||||||
await _auditingService.SaveAuditRecordAsync(new UserAuditRecord(account, AuditedUserAction.ChangeUsername)); | ||||||
|
||||||
_userRepository.InsertOnCommit(newAccountForOldUsername); | ||||||
|
||||||
await _entitiesContext.SaveChangesAsync(); | ||||||
|
||||||
return Json(HttpStatusCode.OK, "Account renamed successfully!", JsonRequestBehavior.AllowGet); | ||||||
} | ||||||
|
||||||
private ValidateUsernameResult ValidateUsername(string username) | ||||||
private ValidateUsernameResult ValidateUsernameChange(User requestor, string username) | ||||||
{ | ||||||
var result = new ValidateUsernameResult(); | ||||||
result.IsFormatValid = UsernameValidationRegex.IsMatch(username); | ||||||
result.IsAvailable = _userService.FindByUsername(username, includeDeleted: true) == null; | ||||||
var foundUser = _userService.FindByUsername(username, includeDeleted: true); | ||||||
|
||||||
return result; | ||||||
return new ValidateUsernameResult() | ||||||
{ | ||||||
IsFormatValid = UsernameValidationRegex.IsMatch(username), | ||||||
IsAvailable = foundUser == null || (requestor.Key == foundUser.Key && foundUser.Username != username) // The username check is in the event where we found a user in the DB but we're doing a cAsIng change | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
}; | ||||||
} | ||||||
} | ||||||
} |
3 changes: 3 additions & 0 deletions
3
src/NuGetGallery/Areas/Admin/Models/ValidateUsernameResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace NuGetGallery.Areas.Admin.Models | ||
{ | ||
public class ValidateUsernameResult | ||
{ | ||
public bool IsFormatValid { get; set; } | ||
public bool IsAvailable { get; set; } | ||
public IEnumerable<string> OwnedPackageIds { get; set; } = new List<string>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: oldAccount -> account
It may be better to use the same name for local variables under the same class. For example, line 130 uses "account". We can follow the same name convention and also the account itself is still the same but the name will be changed. Personally I think this is more readable and easier to maintain in the future.