public async Task <IActionResult> Register(RegisterViewModel model, Uri returnUrl = null) { ViewData["Title"] = "Register"; // ViewData["Menu"] = "navAccount"; ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model?.UserName, FirstName = model.FirstName, LastName = model.LastName, Email = model.Email }; var result = await UserManagerAgent.CreateAsync(user, model.Password).ConfigureAwait(false); if (result.Succeeded) { if (!await RoleManagerAgent.RoleExistsAsync(_appSettings.SecRole.Level1).ConfigureAwait(false)) { ApplicationRole role = new ApplicationRole { Name = _appSettings.SecRole.Level1, Description = "Perform basic operations." }; IdentityResult roleResult = await RoleManagerAgent.CreateAsync(role).ConfigureAwait(false); if (!roleResult.Succeeded) { ModelState.AddModelError(string.Empty, "Error while creating role!"); return(View(model)); } } UserManagerAgent.AddToRoleAsync(user, _appSettings.SecRole.Level1).Wait(); // send confirmation email string confirmationToken = await UserManagerAgent.GenerateEmailConfirmationTokenAsync(user).ConfigureAwait(false); string confirmationLink = Url.Action("ConfirmEmail", "Account", new { userid = user.Id, token = confirmationToken }, protocol: HttpContext.Request.Scheme); string[] emailAddresses = { _appSettings.SMTP.AdminEmail, user.Email }; var emailName = string.IsNullOrWhiteSpace(user.FirstName) ? user.UserName : $"{user.FirstName} {user.LastName}".Trim(); await _emailAgent.SendEmailAsync(_appSettings.SMTP.FromEmail, _appSettings.SMTP.FromEmail, emailAddresses, "Welcome to Winemakers Software - Please verify your email.", CreateVerifyEmail(confirmationLink, emailName), true, null).ConfigureAwait(false); // redirect to limbo page return(RedirectToAction("RegisterLimbo", "Account")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IActionResult> AddRole(ApplicationRole model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } // Create Role if (!await RoleManagerAgent.RoleExistsAsync(model.Name).ConfigureAwait(false)) { IdentityResult roleResult = await RoleManagerAgent.CreateAsync(model).ConfigureAwait(false); if (!roleResult.Succeeded) { Danger(_localizer["ErrorAddRole"], true); return(View(model)); } } return(RedirectToAction("Index", "Admin", new { id = "roles" })); }
public async Task <IActionResult> Index(string id) { ViewData["Title"] = _localizer["PageTitle"]; ViewData["PageDesc"] = _localizer["PageDesc"]; var getYeastQuery = _yeastQueryFactory.CreateYeastsQuery(); var getYeastPairs = _yeastQueryFactory.CreateYeastPairQuery(); var getCategoriesQuery = _recipeQueryFactory.CreateCategoriesQuery(); var getVarietiesQuery = _recipeQueryFactory.CreateVarietiesQuery(); var getRecipesQuery = _recipeQueryFactory.CreateRecipesQuery(); var getMaloCulturesQuery = _maloQueryFactory.CreateMaloCulturesQuery(); var getJournalsQuery = _journalQueryFactory.CreateBatchesQuery(); // using TPL to parallel call gets List <Task> tasks = new List <Task>(); var categoryTask = Task.Run(async() => await getCategoriesQuery.ExecuteAsync().ConfigureAwait(false)); tasks.Add(categoryTask); var cList = await categoryTask.ConfigureAwait(false); var varietyTask = Task.Run(async() => await getVarietiesQuery.ExecuteAsync().ConfigureAwait(false)); tasks.Add(varietyTask); var vList = await varietyTask.ConfigureAwait(false); var yeastTask = Task.Run(async() => await getYeastQuery.ExecuteAsync().ConfigureAwait(false)); tasks.Add(yeastTask); var yList = await yeastTask.ConfigureAwait(false); var pairsTask = Task.Run(async() => await getYeastPairs.ExecuteAsync().ConfigureAwait(false)); tasks.Add(pairsTask); var ypList = await pairsTask.ConfigureAwait(false); var recipeTask = Task.Run(async() => await getRecipesQuery.ExecuteAsync().ConfigureAwait(false)); tasks.Add(recipeTask); var rList = await recipeTask.ConfigureAwait(false); var maloTask = Task.Run(async() => await getMaloCulturesQuery.ExecuteAsync().ConfigureAwait(false)); tasks.Add(maloTask); var mList = await maloTask.ConfigureAwait(false); var journalTask = Task.Run(async() => await getJournalsQuery.ExecuteAsync().ConfigureAwait(false)); tasks.Add(journalTask); var jList = await journalTask.ConfigureAwait(false); Task.WaitAll(tasks.ToArray()); // build model var model = _modelFactory.CreateAdminModel(id); // make sure admin security role exist if (!await RoleManagerAgent.RoleExistsAsync(_appSettings.SecRole.Admin).ConfigureAwait(false)) { ApplicationRole role = new ApplicationRole { Name = _appSettings.SecRole.Admin, Description = "Perform all operations." }; IdentityResult roleResult = await RoleManagerAgent.CreateAsync(role).ConfigureAwait(false); if (!roleResult.Succeeded) { ModelState.AddModelError(string.Empty, "Error while creating role!"); return(View(model)); } } // gather users data var users = UserManagerAgent.Users.ToList(); var userVms = _mapper.Map <List <UserViewModel> >(users); model.UsersViewModel.Users.Clear(); model.UsersViewModel.Users.AddRange(userVms); foreach (var user in model.UsersViewModel.Users) { user.IsAdmin = await UserManagerAgent.IsInRoleAsync(user, _appSettings.SecRole.Admin).ConfigureAwait(false); user.IsLockedOut = await UserManagerAgent.IsLockedOutAsync(user).ConfigureAwait(false); } // gather roles data var roles = await RoleManagerAgent.Roles.OrderBy(r => r.Name).ToListAsync().ConfigureAwait(false); var roleVms = _mapper.Map <List <RoleViewModel> >(roles); model.RolesViewModel.Roles.Clear(); model.RolesViewModel.Roles.AddRange(roleVms); // gather category / variety data model.CategoriesViewModel.Categories.Clear(); model.CategoriesViewModel.Categories.AddRange(_modelFactory.CreateCategoryViewModel(cList)); model.VarietiesViewModel.Varieties.Clear(); model.VarietiesViewModel.Varieties.AddRange(_modelFactory.CreateVarietyViewModel(vList)); // gather yeast data model.YeastsViewModel.Yeasts.Clear(); model.YeastsViewModel.Yeasts.AddRange(_modelFactory.CreateYeastViewModel(yList)); // gather malolactic data model.MaloCulturesViewModel.Cultures.Clear(); model.MaloCulturesViewModel.Cultures.AddRange(_modelFactory.CreateMaloCultureViewModel(mList)); // gather recipe data model.RecipesViewModel.Recipes.Clear(); model.RecipesViewModel.Recipes.AddRange(_modelFactory.CreateRecipeViewModel(rList)); // gather journal data model.JournalsViewModel.Journals.Clear(); model.JournalsViewModel.Journals.AddRange(_modelFactory.CreateJournalViewModel(jList, userVms)); return(View(model)); }