Exemple #1
0
        public async Task <IActionResult> DeleteUser(string UserName)
        {
            var user = await UserManagerAgent.FindByNameAsync(UserName).ConfigureAwait(false);

            if (user != null)
            {
                if (!await UserManagerAgent.IsInRoleAsync(user, _appSettings.SecRole.Admin).ConfigureAwait(false))
                {
                    await UserManagerAgent.DeleteAsync(user).ConfigureAwait(false);
                }
            }

            return(RedirectToAction("Index", "Admin", new { id = "users" }));
        }
Exemple #2
0
        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));
        }
Exemple #3
0
        public async Task <IActionResult> DeleteUserRole(UserViewModel model)
        {
            ViewData["Title"] = "Edit a User";

            var user = await UserManagerAgent.FindByNameAsync(model?.UserName).ConfigureAwait(false);

            if (await RoleManagerAgent.RoleExistsAsync(model.NewRole).ConfigureAwait(false) && await UserManagerAgent.IsInRoleAsync(user, model.NewRole).ConfigureAwait(false))
            {
                await UserManagerAgent.RemoveFromRoleAsync(user, model.NewRole).ConfigureAwait(false);
            }
            model = _mapper.Map <UserViewModel>(user);
            model.MemberRoles.AddRange(await UserManagerAgent.GetRolesAsync(user).ConfigureAwait(false));
            model.AllRoles.AddRange(GetAllRolesAsSelectList());

            return(View("EditUser", model));
        }