Exemple #1
0
        public async Task <IActionResult> AddMember(AddUserResource userResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = _mapper.Map <AddUserResource, User>(userResource);

            var createResult = await _userManager.CreateAsync(user, userResource.Password);

            if (createResult.Succeeded)
            {
                return(Created(string.Empty, string.Empty));
            }

            return(Problem(createResult.Errors.First().Description, null, 500));
        }
Exemple #2
0
        public async Task <IActionResult> PostAsync([FromBody] AddUserResource resource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }

            var user = _mapper.Map <AddUserResource, User>(resource);
            // users that are added through this api route are assigned to the 'common' role
            var result = await _userService.AddUserAsync(user, EApplicationRole.Common);

            if (!result.Success)
            {
                return(BadRequest(result.Message));
            }

            var userResource = _mapper.Map <User, UserResource>(result.User);

            return(Ok(userResource));
        }
        public async Task <IActionResult> Create([FromBody] AddUserResource userResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = mapper.Map <Core.Models.User>(userResource);

            // Add user
            userRepository.Add(user, userResource.Password);

            // Copy all right to module right
            var rightLists = await moduleRightsRepository.GetAll();

            foreach (ModuleRight list in rightLists)
            {
                var userModuleRights = new UserModuleRight
                {
                    ModuleRightsId = list.Id,
                    UserId         = user.Id,
                    Read           = false,
                    Write          = false
                };

                var saveUserModule = mapper.Map <UserModuleRight>(userModuleRights);

                userModuleRightsRepository.Add(saveUserModule);
            }

            if (await unitOfWork.CompleteAsync() == false)
            {
                throw new Exception(message: "Create new user fail on save");
            }

            user = await userRepository.GetOne(user.Id);

            var result = mapper.Map <Core.Models.User, ViewUserResource>(user);

            return(Ok(result));
        }