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
Don't normalize User.RoleNames #13104
Conversation
@@ -462,13 +462,14 @@ public async Task AddToRoleAsync(IUser user, string normalizedRoleName, Cancella | |||
if (user is User u) | |||
{ | |||
var roleNames = await _roleService.GetRoleNamesAsync(); | |||
var roleName = roleNames?.FirstOrDefault(r => NormalizeKey(r) == normalizedRoleName); | |||
|
|||
if (!roleNames.Any(r => NormalizeKey(r) == normalizedRoleName)) |
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.
if (!roleNames.Any(r => NormalizeKey(r) == normalizedRoleName)) | |
if (String.IsNullOrEmpty(roleName)) |
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.
Here we check RoleNames
not RoleName
For now I just copy paste the code as it was before.
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.
@jtkech yes I understand. you don't need to check names all over again since you are doing the same check in the previous line.
The code should look like this if you want to drop ?
since the GetRoleNameAsync()
would return collection not nullable.
public async Task AddToRoleAsync(IUser user, string normalizedRoleName, CancellationToken cancellationToken)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
if (user is User u)
{
var roleNames = await _roleService.GetRoleNamesAsync();
var roleName = roleNames.FirstOrDefault(name => NormalizeKey(name) == normalizedRoleName);
if (String.IsNullOrEmpty(roleName))
{
throw new InvalidOperationException($"Role {normalizedRoleName} does not exist.");
}
u.RoleNames.Add(roleName);
}
}
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.
But yes better, done
@@ -462,13 +462,14 @@ public async Task AddToRoleAsync(IUser user, string normalizedRoleName, Cancella | |||
if (user is User u) | |||
{ | |||
var roleNames = await _roleService.GetRoleNamesAsync(); | |||
var roleName = roleNames?.FirstOrDefault(r => NormalizeKey(r) == normalizedRoleName); |
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.
var roleName = roleNames?.FirstOrDefault(r => NormalizeKey(r) == normalizedRoleName); | |
var roleName = roleNames.FirstOrDefault(r => NormalizeKey(r) == normalizedRoleName); |
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.
Will check, for now here it is as it was before
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.
Done locally
@@ -482,13 +483,14 @@ public async Task RemoveFromRoleAsync(IUser user, string normalizedRoleName, Can | |||
if (user is User u) | |||
{ | |||
var roleNames = await _roleService.GetRoleNamesAsync(); | |||
var roleName = roleNames?.FirstOrDefault(r => NormalizeKey(r) == normalizedRoleName); |
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.
var roleName = roleNames?.FirstOrDefault(r => NormalizeKey(r) == normalizedRoleName); | |
var roleName = roleNames.FirstOrDefault(r => NormalizeKey(r) == normalizedRoleName); |
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.
idem
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.
done
@@ -482,13 +483,14 @@ public async Task RemoveFromRoleAsync(IUser user, string normalizedRoleName, Can | |||
if (user is User u) | |||
{ | |||
var roleNames = await _roleService.GetRoleNamesAsync(); | |||
var roleName = roleNames?.FirstOrDefault(r => NormalizeKey(r) == normalizedRoleName); | |||
|
|||
if (!roleNames.Any(r => NormalizeKey(r) == normalizedRoleName)) |
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.
if (!roleNames.Any(r => NormalizeKey(r) == normalizedRoleName)) | |
if (String.IsNullOrEmpty(roleName)) |
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.
idem
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.
done
EmailConfirmed = true | ||
}; | ||
|
||
user.RoleNames.Add("Administrator"); |
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.
I think it is safer to use RoleNames = new string[] { "Administrator" },
but up to you. Maybe just undo the change to this class completely.
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.
Here the little fix is to not set the IList RoleNames
to a string array, otherwise it saves in the json document the following.
"RoleNames":{"$type":"System.String[], System.Private.CoreLib","$values":["Administrator"]}
In place of
"RoleNames":["Administrator"]
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.
Then we add a role to an empty list because the user is created just before.
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.
@jtkech I think this should be merged ASAP. I am approving this change.
Okay, just waiting for unit tests |
Please wait, I'm testing it |
As it was before.