예제 #1
0
        // POST api/accounts/signup
        public async Task <IActionResult> Signup([FromBody] RegistrationViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userEmail = model.Email.Trim();

            if (await _accountsService.UserExists(userEmail))
            {
                return(new JsonResult(await Errors.GetGenericErrorResponse(
                                          new SignUpResponse()
                {
                    Id = "no_id",
                    Email = userEmail,
                    StatusCode = 400,
                    Error = "User exists",
                    Description = "Please enter a new user email. This user does already exists.",
                    Code = "user_exists"
                })));
            }

            var userRole = model.Role.Trim();

            if (!await _accountsService.RoleExists(userRole))
            {
                return(new JsonResult(await Errors.GetGenericErrorResponse(
                                          new SignUpResponse()
                {
                    Id = "no_id",
                    Email = userEmail,
                    StatusCode = 400,
                    Error = "Role does not exists",
                    Description = "The role you are trying to link to a user does not exist.",
                    Code = "role_does_not_exists"
                })));
            }

            var userIdentity = _mapper.Map <User>(model);

            IdentityResult result = await _accountsService.CreateUser(userIdentity, model.Password);

            if (!result.Succeeded)
            {
                return(new JsonResult(await Errors.GetGenericErrorResponse(
                                          new SignUpResponse()
                {
                    Error = "Unable to create user",
                    StatusCode = 422,
                    Description = "User could not be created at this time",
                    Email = userEmail,
                    Id = userIdentity.Id ?? "no_id",
                    Code = "unable_to_create_user"
                })));
            }

            IdentityResult addRoleResult = await _accountsService.AddRoleToUser(userIdentity, userRole);

            if (!addRoleResult.Succeeded)
            {
                return(new JsonResult(await Errors.GetGenericErrorResponse(
                                          new SignUpResponse()
                {
                    Error = "Unable to link role to user",
                    StatusCode = 422,
                    Description = "Role could not be linked to the user.",
                    Email = userEmail,
                    Id = userIdentity.Id,
                    Code = "unable_to_link_role_to_user"
                })));
            }

            await _context.SaveChangesAsync();

            return(new OkObjectResult(Wrappyfier.WrapSigupResponse(userIdentity.Id, userIdentity.Email, 200)));
        }