示例#1
0
        public async Task <IActionResult> EditUserRoles([FromBody] UserSaveRolesResource userResource, int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await userRepository.GetUser(id);

            if (user == null)
            {
                return(NotFound());
            }

            if (await roleRepository.CheckAdmin(userResource, user))
            {
                return(BadRequest("You can not remove administrator role because only one administrator exist."));
            }

            mapper.Map <UserSaveRolesResource, User>(userResource, user);
            await unitOfWork.Complete();

            user = await userRepository.GetUser(id);// do poprawy

            var result = mapper.Map <User, UserRolesResource>(user);

            return(Ok(result));
        }
        public async Task <bool> CheckAdmin(UserSaveRolesResource userSaveRolesResource, User user)
        {
            if (!userSaveRolesResource.Roles.Contains(1) && user.Roles.Any(r => r.RoleId == 1))
            {
                var count = await RoleCount(1);

                if (count <= 1)
                {
                    return(true);
                }
            }
            return(false);
        }