public static IApplicationBuilder UseFolkeCore( this IApplicationBuilder app, IFolkeConnection connection, IHostingEnvironment env, RoleManager <Role> roleManager, UserManager <User> userManager, ApplicationPartManager applicationPartManager, Action <FolkeCoreOptions> optionsAction) { app.UseIdentity(); app.UseDefaultFiles(); app.UseStaticFiles(); app.UseMvc(); app.UseRequestLocalization(); connection.UpdateIdentityUserSchema <int, User>(); connection.UpdateIdentityRoleSchema <int, User>(); connection.UpdateSchema(typeof(User).GetTypeInfo().Assembly); using (var transaction = connection.BeginTransaction()) { var options = new FolkeCoreOptions(); optionsAction(options); CreateAdministrator(roleManager, userManager, options).GetAwaiter().GetResult(); transaction.Commit(); } if (env.IsDevelopment()) { CreateTypeScriptServices(applicationPartManager); } return(app); }
public static IServiceCollection AddFolkeCore <TDataBaseDriver>( this IServiceCollection serviceCollection, Action <FolkeCoreOptions> optionsAction) where TDataBaseDriver : class, IDatabaseDriver { var coreServiceOptions = new FolkeCoreOptions(); optionsAction(coreServiceOptions); serviceCollection.AddElm <TDataBaseDriver>(coreServiceOptions.Elm); serviceCollection.AddIdentity <User, Role>(coreServiceOptions.Identity).AddDefaultTokenProviders(); var mvcBuilder = serviceCollection.AddMvc().AddFolkeCore(); coreServiceOptions.MvcBuilder?.Invoke(mvcBuilder); serviceCollection.AddAuthorization(options => { options.AddPolicy("UserList", policy => { policy.RequireRole(RoleNames.Administrator); }); options.AddPolicy("Role", policy => { policy.RequireRole(RoleNames.Administrator); }); coreServiceOptions.Authorization?.Invoke(options); }); serviceCollection.AddElmIdentity <User, Role, int>(); serviceCollection.AddIdentityServer <User, int, EmailService, UserService, UserViewModel>(); serviceCollection.AddRoleIdentityServer <Role, RoleService, RoleViewModel>(); return(serviceCollection); }
private static async Task CreateAdministrator(RoleManager <Role> roleManager, UserManager <User> userManager, FolkeCoreOptions options) { var administrateur = await roleManager.FindByNameAsync(RoleNames.Administrator); if (administrateur == null) { await roleManager.CreateAsync(new Role { Name = RoleNames.Administrator }); } var users = await userManager.GetUsersInRoleAsync(RoleNames.Administrator); if (users.Count == 0) { var result = await userManager.CreateAsync(new User { UserName = options.AdministratorEmail, Email = options.AdministratorEmail }, options.AdministratorPassword); if (result.Succeeded) { var user = await userManager.FindByNameAsync(options.AdministratorEmail); await userManager.AddToRoleAsync(user, RoleNames.Administrator); } } }