Skip to content

Commit

Permalink
Introduce AddUsers() extension method (#12932)
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco authored Dec 18, 2022
1 parent 5ccff8b commit 77b22be
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
21 changes: 1 addition & 20 deletions src/OrchardCore.Modules/OrchardCore.Users/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@
using OrchardCore.Users.Controllers;
using OrchardCore.Users.Drivers;
using OrchardCore.Users.Handlers;
using OrchardCore.Users.Indexes;
using OrchardCore.Users.Liquid;
using OrchardCore.Users.Models;
using OrchardCore.Users.Services;
using OrchardCore.Users.ViewModels;
using YesSql.Filters.Query;
using YesSql.Indexes;

namespace OrchardCore.Users
{
Expand Down Expand Up @@ -166,15 +164,7 @@ public override void ConfigureServices(IServiceCollection services)
// This is required for security modules like the OpenID module (that uses SignOutAsync()) to work correctly.
services.AddAuthentication(options => options.DefaultSignOutScheme = IdentityConstants.ApplicationScheme);

services.TryAddScoped<UserStore>();
services.TryAddScoped<IUserStore<IUser>>(sp => sp.GetRequiredService<UserStore>());
services.TryAddScoped<IUserRoleStore<IUser>>(sp => sp.GetRequiredService<UserStore>());
services.TryAddScoped<IUserPasswordStore<IUser>>(sp => sp.GetRequiredService<UserStore>());
services.TryAddScoped<IUserEmailStore<IUser>>(sp => sp.GetRequiredService<UserStore>());
services.TryAddScoped<IUserSecurityStampStore<IUser>>(sp => sp.GetRequiredService<UserStore>());
services.TryAddScoped<IUserLoginStore<IUser>>(sp => sp.GetRequiredService<UserStore>());
services.TryAddScoped<IUserClaimStore<IUser>>(sp => sp.GetRequiredService<UserStore>());
services.TryAddScoped<IUserAuthenticationTokenStore<IUser>>(sp => sp.GetRequiredService<UserStore>());
services.AddUsers();

services.ConfigureApplicationCookie(options =>
{
Expand All @@ -191,14 +181,8 @@ public override void ConfigureServices(IServiceCollection services)
options.AccessDeniedPath = "/Error/403";
});

services.AddSingleton<IIndexProvider, UserIndexProvider>();
services.AddSingleton<IIndexProvider, UserByRoleNameIndexProvider>();
services.AddSingleton<IIndexProvider, UserByLoginInfoIndexProvider>();
services.AddSingleton<IIndexProvider, UserByClaimIndexProvider>();
services.AddDataMigration<Migrations>();

services.AddScoped<IUserService, UserService>();
services.AddScoped<IUserClaimsPrincipalFactory<IUser>, DefaultUserClaimsPrincipalProviderFactory>();
services.AddScoped<IUserClaimsProvider, EmailClaimsProvider>();
services.AddSingleton<IUserIdGenerator, DefaultUserIdGenerator>();

Expand Down Expand Up @@ -243,9 +227,6 @@ public override void ConfigureServices(IServiceCollection services)
});

services.AddTransient<IUsersAdminListFilterProvider, DefaultUsersAdminListFilterProvider>();

services.AddScoped<IUserEventHandler, UserDisabledEventHandler>();

services.AddTransient<IConfigureOptions<ResourceManagementOptions>, UserOptionsConfiguration>();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection.Extensions;
using OrchardCore.Users.Services;
using OrchardCore.Users;
using OrchardCore.Users.Indexes;
using YesSql.Indexes;
using OrchardCore.Users.Handlers;

namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Provides extension methods for <see cref="IServiceCollection"/>.
/// </summary>
public static class UsersServiceCollectionExtensions
{
/// <summary>
/// Registers the users services.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
public static IServiceCollection AddUsers(this IServiceCollection services)
{
services.TryAddScoped<UserStore>();
services.TryAddScoped<IUserStore<IUser>>(sp => sp.GetRequiredService<UserStore>());
services.TryAddScoped<IUserRoleStore<IUser>>(sp => sp.GetRequiredService<UserStore>());
services.TryAddScoped<IUserPasswordStore<IUser>>(sp => sp.GetRequiredService<UserStore>());
services.TryAddScoped<IUserEmailStore<IUser>>(sp => sp.GetRequiredService<UserStore>());
services.TryAddScoped<IUserSecurityStampStore<IUser>>(sp => sp.GetRequiredService<UserStore>());
services.TryAddScoped<IUserLoginStore<IUser>>(sp => sp.GetRequiredService<UserStore>());
services.TryAddScoped<IUserClaimStore<IUser>>(sp => sp.GetRequiredService<UserStore>());
services.TryAddScoped<IUserAuthenticationTokenStore<IUser>>(sp => sp.GetRequiredService<UserStore>());

services.AddSingleton<IIndexProvider, UserIndexProvider>();
services.AddSingleton<IIndexProvider, UserByRoleNameIndexProvider>();
services.AddSingleton<IIndexProvider, UserByLoginInfoIndexProvider>();
services.AddSingleton<IIndexProvider, UserByClaimIndexProvider>();

services.AddScoped<IUserService, UserService>();
services.AddScoped<IUserClaimsPrincipalFactory<IUser>, DefaultUserClaimsPrincipalProviderFactory>();
services.AddScoped<IUserEventHandler, UserDisabledEventHandler>();

return services;
}
}
}

0 comments on commit 77b22be

Please sign in to comment.