Skip to content
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
merged 43 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
ca45294
User Accounts Deployment
SzymonSel Aug 25, 2023
0f80b89
CustomUserSettings deployment
SzymonSel Aug 25, 2023
2e38e17
Cleanup
SzymonSel Aug 25, 2023
69a2eb3
Code review changes
SzymonSel Aug 28, 2023
40a490f
Further changes
SzymonSel Aug 28, 2023
0625ffc
Formatting and bracket
SzymonSel Aug 28, 2023
e5476a6
Further refactor
SzymonSel Aug 28, 2023
6c7a4f8
Registration
SzymonSel Aug 28, 2023
153473c
User roles import
SzymonSel Aug 28, 2023
9044980
Vanilla js
SzymonSel Aug 28, 2023
d2a650c
Update src/OrchardCore.Modules/OrchardCore.Users/Views/Items/CustomUs…
SzymonSel Aug 29, 2023
f41f59b
Update src/OrchardCore.Modules/OrchardCore.Users/Views/Items/CustomUs…
SzymonSel Aug 29, 2023
08272f0
Ongoing refactoring hell
SzymonSel Aug 29, 2023
f390d4f
Bang bang
SzymonSel Aug 29, 2023
b852da8
Update src/OrchardCore.Modules/OrchardCore.Users/Deployment/AllUsersD…
SzymonSel Aug 31, 2023
2a9b759
Merge branch 'dev' into szymonsel/users-deployment
SzymonSel Sep 28, 2023
1cd46e6
Update src/OrchardCore.Modules/OrchardCore.Users/Views/Items/CustomUs…
SzymonSel Sep 28, 2023
1aaabdf
CR changes
SzymonSel Sep 28, 2023
1fc8b05
CR changes
SzymonSel Sep 28, 2023
f7a9538
CR Changes
SzymonSel Sep 28, 2023
94050b0
Quick refactor
SzymonSel Sep 28, 2023
bf4b21c
CR cleanup
SzymonSel Sep 28, 2023
173c933
CR cleanup
SzymonSel Sep 28, 2023
55e81e4
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
hishamco Oct 21, 2023
60fa9d4
Update src/OrchardCore.Modules/OrchardCore.Users/Recipes/UsersStep.cs
hishamco Oct 21, 2023
fddb800
Update src/OrchardCore.Modules/OrchardCore.Users/ViewModels/CustomUse…
hishamco Oct 21, 2023
fc58742
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
hishamco Oct 21, 2023
b51dc2c
Update src/OrchardCore.Modules/OrchardCore.Users/ViewModels/CustomUse…
hishamco Oct 21, 2023
480e01d
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
hishamco Oct 21, 2023
28ac1df
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel Oct 21, 2023
b37e14e
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel Oct 21, 2023
89d3589
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel Oct 21, 2023
f126fa9
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
hishamco Oct 21, 2023
5cd5a0c
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel Oct 21, 2023
b1ac1e1
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel Oct 21, 2023
2cef4ed
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel Oct 21, 2023
ed777b7
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel Oct 21, 2023
090d2d9
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel Oct 21, 2023
683d429
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel Oct 21, 2023
5fea5d8
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel Oct 21, 2023
5bd124f
Update src/OrchardCore.Modules/OrchardCore.Users/Models/UsersStepUser…
SzymonSel Oct 21, 2023
bf483bc
Update src/OrchardCore.Modules/OrchardCore.Users/Deployment/AllUsersD…
hishamco Oct 21, 2023
9cd2372
Apply suggestions from code review
hishamco Oct 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Collections.Generic;
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
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
{
public class AllUsersDeploymentSource : IDeploymentSource
{
private readonly ISession _session;

public AllUsersDeploymentSource(ISession session)
{
_session = session;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
{
var allRolesStep = step as AllUsersDeploymentStep;

if (allRolesStep == null)
{
return;
}

// todo: Get all users
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
var allUsers = await _session.Query<User>().ListAsync(); ;
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
var users = new JArray();
var tasks = new List<Task>();

SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
foreach (var user in allUsers)
{
/*
var currentRole = (Role)await _roleManager.FindByNameAsync(_roleManager.NormalizeKey(role.RoleName));
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved

if (currentRole != null)
{
*/
users.Add(JObject.FromObject(
new UsersStepUserModel
{
UserName = user.UserName,
UserId = user.UserId,
Email = user.Email,
Id = user.Id,
EmailConfirmed = user.EmailConfirmed,
PasswordHash = user.PasswordHash,
IsEnabled = user.IsEnabled,
NormalizedEmail = user.NormalizedEmail,
NormalizedUserName = user.NormalizedUserName,
SecurityStamp = user.SecurityStamp,
ResetToken = user.ResetToken
//Permissions = currentRole.RoleClaims.Where(x => x.ClaimType == Permission.ClaimType).Select(x => x.ClaimValue).ToArray()
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
}));

//}
}

result.Steps.Add(new JObject(
new JProperty("name", "Users"),
new JProperty("Users", users)
));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using OrchardCore.Deployment;

namespace OrchardCore.Users.Deployment
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Adds users to a <see cref="DeploymentPlanResult"/>.
/// </summary>
public class AllUsersDeploymentStep : DeploymentStep
{
public AllUsersDeploymentStep()
{
Name = "AllUsers";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using OrchardCore.Deployment;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.Views;

namespace OrchardCore.Users.Deployment
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
{
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");
}
}
}
86 changes: 86 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Users/Recipes/UsersStep.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using OrchardCore.Recipes.Models;
using OrchardCore.Recipes.Services;
using OrchardCore.Users.Models;
using OrchardCore.Users.Services;
using YesSql;

namespace OrchardCore.Users.Recipes
{
public class UsersStep : IRecipeStepHandler {
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
private readonly UserManager<IUser> _userManager;
private readonly IUserService _userService;
private ISession _session;
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
public UsersStep(UserManager<IUser> userManager, IUserService userService, ISession session)
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
{
_userManager = userManager;
_userService = userService;
_session = session;
}

public async Task ExecuteAsync(RecipeExecutionContext context) {

if (!String.Equals(context.Name, "Users", StringComparison.OrdinalIgnoreCase)) {
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
return;
}

var model = context.Step.ToObject<UsersStepModel>();

foreach (var importedUser in model.Users) {
if (String.IsNullOrWhiteSpace(importedUser.UserName))
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
continue;

var iUser = await _userManager.FindByIdAsync(importedUser.UserId);
User user;
var isNewUser = iUser == null;

if (isNewUser) {
user = new User { UserId = importedUser.UserId };
} else {
user = iUser as User;
}
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved

user.Email = importedUser.Email;
user.UserName = importedUser.UserName;
//user.Id = importedUser.Id; // This is problematic because it's an identity column
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
user.EmailConfirmed = importedUser.EmailConfirmed;
user.PasswordHash = importedUser.PasswordHash;
user.IsEnabled = importedUser.IsEnabled;
user.NormalizedEmail = importedUser.NormalizedEmail;
user.NormalizedUserName = importedUser.NormalizedUserName;
user.SecurityStamp = importedUser.SecurityStamp;
user.ResetToken = importedUser.ResetToken;

if (isNewUser) {
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
_session.Save(user);
} else {
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
_session.Save(user);
}
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
}
}

public class UsersStepModel {
public UsersStepUserModel[] Users { get; set; }
}
}

public class UsersStepUserModel {
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved

public long Id { get; set; }

public string UserId { get; set; }

public string UserName { get; set; }

public string Email { get; set; }
public string PasswordHash { get; set; }
public bool EmailConfirmed { get; set; }
public bool IsEnabled { get; set; } = true;
public string NormalizedEmail { get; set; }
public string NormalizedUserName { get; set; }
public string SecurityStamp { get; set; }
public string ResetToken { get; set; }
}
}
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
14 changes: 14 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Users/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using OrchardCore.Admin;
using OrchardCore.Data;
using OrchardCore.Data.Migration;
using OrchardCore.Deployment;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.Theming;
using OrchardCore.Environment.Commands;
Expand All @@ -26,6 +27,7 @@
using OrchardCore.Modules;
using OrchardCore.Mvc.Core.Utilities;
using OrchardCore.Navigation;
using OrchardCore.Recipes;
using OrchardCore.Recipes.Services;
using OrchardCore.ResourceManagement;
using OrchardCore.Security;
Expand All @@ -36,11 +38,13 @@
using OrchardCore.Sms;
using OrchardCore.Users.Commands;
using OrchardCore.Users.Controllers;
using OrchardCore.Users.Deployment;
using OrchardCore.Users.Drivers;
using OrchardCore.Users.Handlers;
using OrchardCore.Users.Indexes;
using OrchardCore.Users.Liquid;
using OrchardCore.Users.Models;
using OrchardCore.Users.Recipes;
using OrchardCore.Users.Services;
using OrchardCore.Users.ViewModels;
using YesSql.Filters.Query;
Expand Down Expand Up @@ -244,6 +248,8 @@ public override void ConfigureServices(IServiceCollection services)

services.AddTransient<IUsersAdminListFilterProvider, DefaultUsersAdminListFilterProvider>();
services.AddTransient<IConfigureOptions<ResourceManagementOptions>, UserOptionsConfiguration>();

services.AddRecipeExecutionStep<UsersStep>();
}
}

Expand Down Expand Up @@ -493,4 +499,12 @@ public override void ConfigureServices(IServiceCollection services)
services.AddScoped<IPermissionProvider, CustomUserSettingsPermissions>();
}
}

public class UserDeploymentStartup : StartupBase {
public override void ConfigureServices(IServiceCollection services) {
services.AddTransient<IDeploymentSource, AllUsersDeploymentSource>();
services.AddSingleton<IDeploymentStepFactory>(new DeploymentStepFactory<AllUsersDeploymentStep>());
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved
services.AddScoped<IDisplayDriver<DeploymentStep>, AllUsersDeploymentStepDriver>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@model dynamic
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved

<h5>@T["All Users"]</h5>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@model dynamic
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved

<h5>@T["All Users"]</h5>

<span class="hint">@T["Adds all users to the plan."]</span>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@model dynamic
SzymonSel marked this conversation as resolved.
Show resolved Hide resolved

<h4 class="card-title">@T["All Users"]</h4>
<p>@T["Exports every user."]</p>
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
@using OrchardCore.Users.TimeZone
@using OrchardCore.Users.TimeZone.ViewModels
@using OrchardCore.Workflows.Helpers
@using OrchardCore.Users.Deployment