Beispiel #1
0
        public async Task <IActionResult> ManageUserRoles(string userId)
        {
            ViewBag.userId = userId;
            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                ViewBag.ErrorMessage = $"User with id={userId} cannot be found";
                return(View("NotFound"));
            }
            var model = new List <RolesUserViewModel>();

            foreach (var role in _roleManager.Roles)
            {
                var RolesUser = new RolesUserViewModel
                {
                    RoleId   = role.Id,
                    RoleName = role.Name
                };
                if (await _userManager.IsInRoleAsync(user, role.Name))
                {
                    RolesUser.IsSelected = true;
                }
                else
                {
                    RolesUser.IsSelected = false;
                }
                model.Add(RolesUser);
            }
            return(View(model));
        }
        public async Task <bool> EditUserRoles(RolesUserViewModel rolesUserVm)
        {
            var userRoles = await _userService.GetUserRolesByGuid(rolesUserVm.UserId);

            var currentEditUser = await _userService.FindByIdAsync(rolesUserVm.UserId);

            using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    await _userService.RemoveFromRolesAsync(currentEditUser, userRoles.ToArray());

                    if (rolesUserVm.CurrentUserRoles.Any())
                    {
                        await _userService.AddUserToRolesAsync(currentEditUser, rolesUserVm.CurrentUserRoles.ToList());
                    }

                    transaction.Complete();
                }
                catch (Exception)
                {
                    _unitOfWork.Rollback();
                }
            }

            return(true);
        }
        public async Task <ActionResult> RolesUser(string id)
        {
            ViewBag.userId = id;
            var user = await UserManager.FindByIdAsync(id);

            if (user == null)
            {
                ViewBag.NotFound = "El usuario no existe";
                return(View());
            }
            var model = new List <RolesUserViewModel>();

            foreach (var rol in roleManager.Roles)
            {
                var ViewModel = new RolesUserViewModel
                {
                    Id      = rol.Id,
                    RolName = rol.Name
                };
                if (await UserManager.IsInRoleAsync(id, rol.Name))
                {
                    ViewModel.isSelected = true;
                }
                else
                {
                    ViewModel.isSelected = false;
                }
                model.Add(ViewModel);
            }

            return(View(model));
        }
Beispiel #4
0
        public async Task <IActionResult> EditUserRoles([FromBody] RolesUserViewModel rolesUserVm)
        {
            var result = await _accountRepository.EditUserRoles(rolesUserVm);

            if (result)
            {
                return(Ok());
            }

            return(BadRequest());
        }
        public async Task <RolesUserViewModel> GetUserRolesById(string userId)
        {
            var userRoles = await _userService.GetUserRolesById(userId);

            var roles = await GetUserRoles();

            var roleUserVm = new RolesUserViewModel
            {
                CurrentUserRoles = userRoles,
                RoleList         = roles
            };

            return(roleUserVm);
        }
Beispiel #6
0
        public async Task <bool> EditUserRoles(RolesUserViewModel rolesUserVm)
        {
            var userRoles = await _userService.GetUserRolesByGuid(rolesUserVm.UserId);

            var currentEditUser = await _userService.FindByIdAsync(rolesUserVm.UserId);

            await _userService.RemoveFromRolesAsync(currentEditUser, userRoles.ToArray());

            if (rolesUserVm.CurrentUserRoles.Any())
            {
                await _userService.AddUserToRolesAsync(currentEditUser, rolesUserVm.CurrentUserRoles.ToList());
            }

            return(true);
        }
Beispiel #7
0
        public async Task <IActionResult> EditRolesInUser(string userId)
        {
            ViewBag.userId = userId;

            var user = await userManager.FindByIdAsync(userId);

            if (user == null)
            {
                ViewBag.ErrorMessage = $"Пользователи с ID = {userId} не найдены";
                return(View("NotFound"));
            }

            var model = new List <RolesUserViewModel>();
            var roles = await roleManager.Roles.ToListAsync();

            foreach (var role in roles)
            {
                var rolesUserViewModel = new RolesUserViewModel
                {
                    RoleId   = role.Id,
                    RoleName = role.Name
                };

                if (await userManager.IsInRoleAsync(user, role.Name))
                {
                    rolesUserViewModel.IsSelected = true;
                }
                else
                {
                    rolesUserViewModel.IsSelected = false;
                }

                model.Add(rolesUserViewModel);
            }

            return(View(model));
        }
Beispiel #8
0
        public async Task<IActionResult> ManageUserRoles(string userID)
        {
            ViewBag.UserID = userID;
            var user = await userManager.FindByIdAsync(userID);

            if (user == null)
            {
                ViewBag.ErrorMessage = $"User Id {user.Id} is not here";

                return View("NotFound");
            }

            var model = new List<RolesUserViewModel>();


            foreach (var role in roleManager.Roles)
            {
                var userRolesviewmodel = new RolesUserViewModel
                {
                    RoleId = role.Id,
                    RoleName = role.Name
                };

                if (await userManager.IsInRoleAsync(user, role.Name))
                {
                    userRolesviewmodel.IsSelected = true;
                }
                else
                {
                    userRolesviewmodel.IsSelected = false;
                }

                model.Add(userRolesviewmodel);
            }

            return View(model);
        }
        public async Task <List <RolesUserViewModel> > getListUserRoles(Utilisateur user)
        {
            try
            {
                List <RolesUserViewModel> lRoles = new List <RolesUserViewModel>();

                foreach (var role in roleManager.Roles)
                {
                    var rolesUserViewModel = new RolesUserViewModel
                    {
                        RoleId   = role.Id,
                        RoleName = role.Name
                    };
                    rolesUserViewModel.isSelected = await userManager.IsInRoleAsync(user, role.Name);

                    lRoles.Add(rolesUserViewModel);
                }
                return(lRoles);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }