Example #1
0
        public async Task <IActionResult> Account([FromBody] AccountViewModel model, [FromServices] ApplicationDbContext applicationDbContext,
                                                  [FromServices] IApplicationManager applicationManager, [FromServices] IModuleManager moduleManager,
                                                  [FromServices] UserManager <ApplicationUser> userManager, [FromServices] RoleManager <ApplicationRole> roleManager, [FromServices] SignInManager <ApplicationUser> signInManager)
        {
            if (ModelState.IsValid)
            {
                string connectionString = _configuration.GetConnectionString("DefaultConnection");

                if (String.IsNullOrEmpty(connectionString) || DatabaseExists(connectionString, true))
                {
                    return(BadRequest());
                }

                try
                {
                    // Create identity tables
                    applicationDbContext.Database.Migrate();

                    // Create host user
                    ApplicationUser user = new ApplicationUser {
                        UserName = model.Email, Email = model.Email, EmailConfirmed = true
                    };
                    IdentityResult result = await userManager.CreateAsync(user, model.Password);

                    if (!result.Succeeded)
                    {
                        return(BadRequest(result.Errors));
                    }

                    // Install roles
                    ApplicationRole role = new ApplicationRole();
                    role.Name = "Administrator";
                    await roleManager.CreateAsync(role);

                    IdentityRoleClaim <String> roleClaim = new IdentityRoleClaim <String>();
                    roleClaim.ClaimType  = "GlobalSettingsEdition";
                    roleClaim.ClaimValue = "GlobalSettingsEdition";
                    roleClaim.RoleId     = role.Id;
                    await roleManager.AddClaimAsync(role, roleClaim.ToClaim());

                    // Add user to admin role
                    await userManager.AddToRoleAsync(user, role.Name);

                    // Create kastra tables
                    applicationManager.Install();

                    // Install default template
                    applicationManager.InstallDefaultTemplate();

                    // Install default page
                    applicationManager.InstallDefaultPage();

                    // Install default permissions
                    applicationManager.InstallDefaultPermissions();

                    // Install modules
                    moduleManager.InstallModules();
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex.Message));
                }
            }

            return(Ok());
        }