public IActionResult PostRoleIdToUserId([FromBody] UserRoleInputModel userRole)
        {
            UserMapper      mapper = new UserMapper();
            AdminDataAccess adm    = new AdminDataAccess();
            var             user   = adm.GetUserByID(userRole.UserID);

            if (user == null)
            {
                return(BadRequest("Пользователя не существует"));
            }
            var role = adm.GetRoleByRoleId(userRole.RoleID);

            if (role == null)
            {
                return(BadRequest("Такой роли не существует"));
            }
            List <UserRoleDTO> roles = adm.GetRolesByUserId(userRole.UserID);
            UserRoleDTO        rl    = mapper.ConvertUserRoleInputModelToUserRoleDTO(userRole);

            if (roles.Contains(rl))
            {
                return(Ok("Данная роль для пользователя уже создана"));
            }
            bool result = adm.UserRoleCreate(mapper.ConvertUserRoleInputModelToUserRoleDTO(userRole));

            if (result)
            {
                return(Ok("Роль пользователя создана"));
            }
            else
            {
                return(BadRequest("Ошибка запроса"));
            }
        }
Exemple #2
0
        public async Task <IActionResult> AddToRole(UserRoleInputModel model)
        {
            var roleExists = await this.roleManager.RoleExistsAsync(model.Role);

            var user = await this.userManager.FindByIdAsync(model.UserId);

            if (!roleExists || user == null)
            {
                this.ModelState.AddModelError(string.Empty, WebAdminConstants.UserInvalidIdentityDetails);
            }

            if (!this.ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Index)));
            }

            await this.userManager.AddToRoleAsync(user, model.Role);

            this.TempData.AddSuccessMessage(string.Format(
                                                WebAdminConstants.UserAddedToRole,
                                                user.UserName,
                                                model.Role));

            return(RedirectToAction(nameof(Index)));
        }
        public void Validate_ThrowsWhenInvalid_RoleId_Invalid()
        {
            var i = new UserRoleInputModel()
            {
                Id     = 1,
                UserId = 2,
                RoleId = -3
            };

            Validator.Validate(i);
        }
        public void Validate_NoThrowsWhenValid()
        {
            var i = new UserRoleInputModel()
            {
                Id     = 1,
                UserId = 2,
                RoleId = 3
            };

            Validator.Validate(i);
        }
        public void UserRoleEndpoint(HttpClient client)
        {
            var endpointBase = "http://testserver/userrole";
            var uri          = new UserRoleInputModel()
            {
                UserId = 1,
                RoleId = 1 //duplicate - should fail.
            };
            var response = EndpointPostFail(client, endpointBase, uri);

            Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode);

            uri.UserId = 1;
            uri.RoleId = 3;
            EndpointPost(client, endpointBase, uri);  //should succeed

            var userrole = EndpointFetch <UserRole[]>(client, $"{endpointBase}?keywords=admin");
        }
Exemple #6
0
        public async Task <ManageUserRolesViewModel> GetManageUserViewModelAsync(ApplicationUser user, ClaimsPrincipal adminUser)
        {
            var model = new ManageUserRolesViewModel();

            model.UserId = user.Id;

            model.UserName   = user.UserName;
            model.RoleModels = new List <UserRoleInputModel>();
            var roles = this.roleManager.Roles.ToList();

            if (!adminUser.IsInRole(RoleNames.Admin.ToString()))
            {
                roles.Remove(roles.First(r => r.Name == RoleNames.Admin.ToString()));
            }

            foreach (var role in roles)
            {
                var userRolesViewModel = new UserRoleInputModel
                {
                    RoleId   = role.Id,
                    RoleName = role.Name,
                };
                if (await this.userManager.IsInRoleAsync(user, role.Name))
                {
                    userRolesViewModel.Selected = true;
                }
                else
                {
                    userRolesViewModel.Selected = false;
                }

                model.RoleModels.Add(userRolesViewModel);
            }

            return(model);
        }
 public IActionResult Post(UserRoleInputModel input) => Ok(Manager.Add(input));
        public void Validate_ThrowsWhenInvalid_NoId_Null()
        {
            var i = new UserRoleInputModel();

            Validator.Validate(i);
        }
        public UserRoleDTO ConvertUserRoleInputModelToUserRoleDTO(UserRoleInputModel userRoleIn)
        {
            UserRoleDTO userRole = new UserRoleDTO(userRoleIn.ID, userRoleIn.UserID, userRoleIn.RoleID);

            return(userRole);
        }