-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
User Accounts and Custom User Settings Deployment #14208
Merged
hishamco
merged 43 commits into
OrchardCMS:main
from
SzymonSel:szymonsel/users-deployment
Oct 21, 2023
Merged
Changes from 42 commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
ca45294
User Accounts Deployment
SzymonSel 0f80b89
CustomUserSettings deployment
SzymonSel 2e38e17
Cleanup
SzymonSel 69a2eb3
Code review changes
SzymonSel 40a490f
Further changes
SzymonSel 0625ffc
Formatting and bracket
SzymonSel e5476a6
Further refactor
SzymonSel 6c7a4f8
Registration
SzymonSel 153473c
User roles import
SzymonSel 9044980
Vanilla js
SzymonSel d2a650c
Update src/OrchardCore.Modules/OrchardCore.Users/Views/Items/CustomUs…
SzymonSel f41f59b
Update src/OrchardCore.Modules/OrchardCore.Users/Views/Items/CustomUs…
SzymonSel 08272f0
Ongoing refactoring hell
SzymonSel f390d4f
Bang bang
SzymonSel b852da8
Update src/OrchardCore.Modules/OrchardCore.Users/Deployment/AllUsersD…
SzymonSel 2a9b759
Merge branch 'dev' into szymonsel/users-deployment
SzymonSel 1cd46e6
Update src/OrchardCore.Modules/OrchardCore.Users/Views/Items/CustomUs…
SzymonSel 1aaabdf
CR changes
SzymonSel 1fc8b05
CR changes
SzymonSel f7a9538
CR Changes
SzymonSel 94050b0
Quick refactor
SzymonSel bf4b21c
CR cleanup
SzymonSel 173c933
CR cleanup
SzymonSel 55e81e4
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
hishamco 60fa9d4
Update src/OrchardCore.Modules/OrchardCore.Users/Recipes/UsersStep.cs
hishamco fddb800
Update src/OrchardCore.Modules/OrchardCore.Users/ViewModels/CustomUse…
hishamco fc58742
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
hishamco b51dc2c
Update src/OrchardCore.Modules/OrchardCore.Users/ViewModels/CustomUse…
hishamco 480e01d
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
hishamco 28ac1df
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel b37e14e
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel 89d3589
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel f126fa9
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
hishamco 5cd5a0c
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel b1ac1e1
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel 2cef4ed
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel ed777b7
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel 090d2d9
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel 683d429
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel 5fea5d8
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel 5bd124f
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel bf483bc
Update src/OrchardCore.Modules/OrchardCore.Users/Deployment/AllUsersD…
hishamco 9cd2372
Apply suggestions from code review
hishamco 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
58 changes: 58 additions & 0 deletions
58
src/OrchardCore.Modules/OrchardCore.Users/Deployment/AllUsersDeploymentSource.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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json.Linq; | ||
using OrchardCore.Deployment; | ||
using OrchardCore.Users.Models; | ||
using OrchardCore.Users.Recipes; | ||
using YesSql; | ||
|
||
namespace OrchardCore.Users.Deployment; | ||
|
||
public class AllUsersDeploymentSource : IDeploymentSource | ||
{ | ||
private readonly ISession _session; | ||
|
||
public AllUsersDeploymentSource(ISession session) | ||
{ | ||
_session = session; | ||
} | ||
|
||
public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result) | ||
{ | ||
if (step is not AllUsersDeploymentStep allRolesStep) | ||
{ | ||
return; | ||
} | ||
|
||
var allUsers = await _session.Query<User>().ListAsync(); | ||
var users = new JArray(); | ||
|
||
foreach (var user in allUsers) | ||
{ | ||
users.Add(JObject.FromObject( | ||
new UsersStepUserModel | ||
{ | ||
UserName = user.UserName, | ||
UserId = user.UserId, | ||
Id = user.Id, | ||
Email = user.Email, | ||
EmailConfirmed = user.EmailConfirmed, | ||
PasswordHash = user.PasswordHash, | ||
Enabled = user.Enabled, | ||
hishamco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
NormalizedEmail = user.NormalizedEmail, | ||
NormalizedUserName = user.NormalizedUserName, | ||
SecurityStamp = user.SecurityStamp, | ||
ResetToken = user.ResetToken, | ||
PhoneNumber = user.PhoneNumber, | ||
PhoneNumberConfirmed = user.PhoneNumberConfirmed, | ||
TwoFactorEnabled = user.TwoFactorEnabled, | ||
IsLockoutEnabled = user.IsLockoutEnabled, | ||
AccessFailedCount = user.AccessFailedCount, | ||
RoleNames = user.RoleNames, | ||
})); | ||
} | ||
|
||
result.Steps.Add(new JObject( | ||
new JProperty("name", "Users"), | ||
new JProperty("Users", users))); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/OrchardCore.Modules/OrchardCore.Users/Deployment/AllUsersDeploymentStep.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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using OrchardCore.Deployment; | ||
|
||
namespace OrchardCore.Users.Deployment; | ||
|
||
/// <summary> | ||
/// Adds users to a <see cref="DeploymentPlanResult"/>. | ||
/// </summary> | ||
public class AllUsersDeploymentStep : DeploymentStep | ||
{ | ||
public AllUsersDeploymentStep() | ||
{ | ||
Name = "AllUsers"; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/OrchardCore.Modules/OrchardCore.Users/Deployment/AllUsersDeploymentStepDriver.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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using OrchardCore.Deployment; | ||
using OrchardCore.DisplayManagement.Handlers; | ||
using OrchardCore.DisplayManagement.Views; | ||
|
||
namespace OrchardCore.Users.Deployment; | ||
|
||
public class AllUsersDeploymentStepDriver : DisplayDriver<DeploymentStep, AllUsersDeploymentStep> | ||
{ | ||
public override IDisplayResult Display(AllUsersDeploymentStep step) | ||
{ | ||
return Combine( | ||
View("AllUsersDeploymentStep_Summary", step).Location("Summary", "Content"), | ||
View("AllUsersDeploymentStep_Thumbnail", step).Location("Thumbnail", "Content")); | ||
} | ||
|
||
public override IDisplayResult Edit(AllUsersDeploymentStep step) | ||
{ | ||
return View("AllUsersDeploymentStep_Edit", step).Location("Content"); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/OrchardCore.Modules/OrchardCore.Users/Deployment/CustomUserSettingsDeploymentSource.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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json.Linq; | ||
using OrchardCore.Deployment; | ||
using OrchardCore.Users.Models; | ||
using OrchardCore.Users.Services; | ||
using YesSql; | ||
|
||
namespace OrchardCore.Users.Deployment; | ||
|
||
public class CustomUserSettingsDeploymentSource : IDeploymentSource | ||
{ | ||
private readonly CustomUserSettingsService _customUserSettingsService; | ||
private readonly ISession _session; | ||
|
||
public CustomUserSettingsDeploymentSource(CustomUserSettingsService customUserSettingsService, ISession session) | ||
{ | ||
_customUserSettingsService = customUserSettingsService; | ||
_session = session; | ||
} | ||
|
||
public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result) | ||
{ | ||
var customUserSettingsStep = step as CustomUserSettingsDeploymentStep; | ||
if (customUserSettingsStep == null) | ||
SzymonSel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return; | ||
} | ||
|
||
var settingsTypes = customUserSettingsStep.IncludeAll | ||
? _customUserSettingsService.GetAllSettingsTypes().ToList() | ||
: _customUserSettingsService.GetSettingsTypes(customUserSettingsStep.SettingsTypeNames).ToList(); | ||
|
||
// Todo: check permissions for each settings type | ||
SzymonSel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var userData = new JArray(); | ||
var allUsers = await _session.Query<User>().ListAsync(); | ||
|
||
foreach (var user in allUsers) | ||
{ | ||
var userSettingsData = new JArray(); | ||
foreach (var settingsType in settingsTypes) | ||
{ | ||
var userSetting = await _customUserSettingsService.GetSettingsAsync(user, settingsType); | ||
userSettingsData.Add(JObject.FromObject(userSetting)); | ||
} | ||
|
||
userData.Add(new JObject( | ||
new JProperty("userId", user.UserId), | ||
new JProperty("user-custom-user-settings", userSettingsData))); | ||
} | ||
|
||
// Adding custom user settings | ||
result.Steps.Add(new JObject( | ||
new JProperty("name", "custom-user-settings"), | ||
new JProperty("custom-user-settings", userData))); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/OrchardCore.Modules/OrchardCore.Users/Deployment/CustomUserSettingsDeploymentStep.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using OrchardCore.Deployment; | ||
|
||
namespace OrchardCore.Users.Deployment; | ||
|
||
public class CustomUserSettingsDeploymentStep : DeploymentStep | ||
{ | ||
public CustomUserSettingsDeploymentStep() | ||
{ | ||
Name = "CustomUserSettings"; | ||
} | ||
|
||
public bool IncludeAll { get; set; } = true; | ||
|
||
public string[] SettingsTypeNames { get; set; } | ||
} |
53 changes: 53 additions & 0 deletions
53
...rchardCore.Modules/OrchardCore.Users/Deployment/CustomUserSettingsDeploymentStepDriver.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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using OrchardCore.Deployment; | ||
using OrchardCore.DisplayManagement.Handlers; | ||
using OrchardCore.DisplayManagement.ModelBinding; | ||
using OrchardCore.DisplayManagement.Views; | ||
using OrchardCore.Users.Services; | ||
using OrchardCore.Users.ViewModels; | ||
|
||
namespace OrchardCore.Users.Deployment | ||
{ | ||
public class CustomUserSettingsDeploymentStepDriver : DisplayDriver<DeploymentStep, CustomUserSettingsDeploymentStep> | ||
{ | ||
private readonly CustomUserSettingsService _customUserSettingsService; | ||
|
||
public CustomUserSettingsDeploymentStepDriver(CustomUserSettingsService customUserSettingsService) | ||
{ | ||
_customUserSettingsService = customUserSettingsService; | ||
} | ||
|
||
public override IDisplayResult Display(CustomUserSettingsDeploymentStep step) | ||
{ | ||
return Combine( | ||
View("CustomUserSettingsDeploymentStep_Fields_Summary", step).Location("Summary", "Content"), | ||
View("CustomUserSettingsDeploymentStep_Fields_Thumbnail", step).Location("Thumbnail", "Content")); | ||
} | ||
|
||
public override IDisplayResult Edit(CustomUserSettingsDeploymentStep step) | ||
{ | ||
return Initialize<CustomUserSettingsDeploymentStepViewModel>("CustomUserSettingsDeploymentStep_Fields_Edit", model => | ||
{ | ||
model.IncludeAll = step.IncludeAll; | ||
model.SettingsTypeNames = step.SettingsTypeNames; | ||
model.AllSettingsTypeNames = _customUserSettingsService.GetAllSettingsTypeNames().ToArray(); | ||
}) | ||
.Location("Content"); | ||
} | ||
|
||
public override async Task<IDisplayResult> UpdateAsync(CustomUserSettingsDeploymentStep step, IUpdateModel updater) | ||
{ | ||
step.SettingsTypeNames = Array.Empty<string>(); | ||
await updater.TryUpdateModelAsync(step, Prefix); | ||
|
||
if (step.IncludeAll) | ||
{ | ||
step.SettingsTypeNames = Array.Empty<string>(); | ||
} | ||
|
||
return Edit(step); | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepModel.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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace OrchardCore.Users.Models; | ||
|
||
public class UsersStepModel | ||
{ | ||
public UsersStepUserModel[] Users { get; set; } | ||
} |
39 changes: 39 additions & 0 deletions
39
src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUserModel.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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace OrchardCore.Users.Models; | ||
|
||
public class UsersStepUserModel | ||
{ | ||
public long Id { get; set; } | ||
hishamco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public string UserId { get; set; } | ||
SzymonSel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public string UserName { get; set; } | ||
SzymonSel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public string Email { get; set; } | ||
SzymonSel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public string PasswordHash { get; set; } | ||
SzymonSel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public bool EmailConfirmed { get; set; } | ||
SzymonSel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public bool Enabled { get; set; } = true; | ||
hishamco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public string NormalizedEmail { get; set; } | ||
SzymonSel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public string NormalizedUserName { get; set; } | ||
SzymonSel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public string SecurityStamp { get; set; } | ||
SzymonSel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public string ResetToken { get; set; } | ||
hishamco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public string PhoneNumber { get; set; } | ||
SzymonSel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public bool PhoneNumberConfirmed { get; set; } | ||
hishamco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public bool TwoFactorEnabled { get; set; } | ||
SzymonSel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public bool IsLockoutEnabled { get; set; } | ||
hishamco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public int AccessFailedCount { get; set; } | ||
SzymonSel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public IList<string> RoleNames { get; set; } | ||
} |
70 changes: 70 additions & 0 deletions
70
src/OrchardCore.Modules/OrchardCore.Users/Recipes/CustomUserSettingsStep.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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json.Linq; | ||
using OrchardCore.ContentManagement; | ||
using OrchardCore.Recipes.Models; | ||
using OrchardCore.Recipes.Services; | ||
using OrchardCore.Users.Models; | ||
using YesSql; | ||
|
||
namespace OrchardCore.Users.Recipes; | ||
|
||
/// <summary> | ||
/// This recipe step updates the custom user settings. | ||
/// </summary> | ||
public class CustomUserSettingsStep : IRecipeStepHandler | ||
{ | ||
private readonly ISession _session; | ||
|
||
public CustomUserSettingsStep(ISession session) | ||
{ | ||
_session = session; | ||
} | ||
|
||
public async Task ExecuteAsync(RecipeExecutionContext context) | ||
{ | ||
if (!string.Equals(context.Name, "custom-user-settings", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return; | ||
} | ||
|
||
var model = context.Step; | ||
|
||
var customUserSettingsList = (JArray)model | ||
SzymonSel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.Properties() | ||
.Where(p => p.Name != "name") | ||
.FirstOrDefault() | ||
?.Value; | ||
|
||
var allUsers = await _session.Query<User>().ListAsync(); | ||
|
||
foreach (JObject userCustomUserSettings in customUserSettingsList.Cast<JObject>()) | ||
{ | ||
var userId = userCustomUserSettings | ||
.Properties() | ||
.FirstOrDefault(p => p.Name == "userId")? | ||
.Value | ||
?.ToString(); | ||
|
||
var user = allUsers.FirstOrDefault(u => u.UserId == userId); | ||
if (user is not User _) | ||
{ | ||
continue; | ||
} | ||
|
||
var userSettings = (JArray)userCustomUserSettings | ||
.Properties() | ||
.FirstOrDefault(p => p.Name == "user-custom-user-settings") | ||
SzymonSel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
?.Value; | ||
|
||
foreach (JObject userSetting in userSettings.Cast<JObject>()) | ||
{ | ||
var contentItem = userSetting.ToObject<ContentItem>(); | ||
user.Properties[contentItem.ContentType] = userSetting; | ||
} | ||
|
||
_session.Save(user); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
Just for info I'm working on #7891 to be able to securely exchange secrets as passwords, credentials for email, RSA keys in place of X509 certificates for OpenId and so on.
Not sure that it could be applied to user passwords, I would need to re-focus on #7891 (didn't work on it the last 2 weeks). Hmm, if it can be applied maybe in a separate PR if we agree to merge this one in the meantime.
Hmm, to deploy secrets we share a symetric key which is itself encrypted by an assymetric key (in fact one to encrypt data and onother one for signing), so maybe we could use the same technic to exchange users without having to encrypt their passwords differently.
This would mean that it is not incompatible with this PR and could be done afterwards, or maybe not, we could think that we first need #7891 and that this PR would need to use it, meaning that deploying users would need 2 RSA secrets to be defined, one for encryption and one for signing, as will be done to deploy secrets.
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.
Does that mean we need to postpone merging this PR?
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.
Not necessarily ;)
I'm not against merging this PR first, was just for info, I let others decide.