private static bool UserCreationValidation(ApplicationUserRequest user, ApplicationUserResponse response)
        {
            var isOk = true;

            if (user == null)
            {
                isOk = false;

                response.ResponseStatus  = ResponseStatus.BadRequest;
                response.ResponseMessage = MessageDescription.MessageDescription.CreateUserFailed.GetDescription();
            }
            else if (user.ApplicationUser == null)
            {
                isOk = false;
                response.ResponseStatus  = ResponseStatus.BadRequest;
                response.ResponseMessage = MessageDescription.MessageDescription.ApplicationUserDTOIsNull.GetDescription();
            }
            else if (user.ApplicationUser.RoleId <= 0)
            {
                isOk = false;
                response.ResponseStatus  = ResponseStatus.BadRequest;
                response.ResponseMessage = MessageDescription.MessageDescription.CreateUserFailed.GetDescription();
            }
            return(isOk);
        }
Beispiel #2
0
        public Task <ApplicationUser> CreateAsync(ApplicationUserRequest request)
        {
            return(CommonOperationAsync(async() =>
            {
                var result = new ApplicationUser
                {
                    UserId = request.UserId,
                    ApplicationId = request.ApplicationId,
                };

                /******************References Table Check Values****************/
                /***************************************************************/
                (await _uow.Roles.GetByIdAsync(result.ApplicationId)).CheckRecordIsExist(typeof(Application).Name);
                (await _uow.Users.GetByIdAsync(result.UserId)).CheckRecordIsExist(typeof(User).Name);
                /***************************************************************/
                /***************************************************************/

                var tempResult = await _uow.ApplicationUsers.GetByUserIdAndApplicationIdAsync(result.UserId, result.ApplicationId);
                tempResult.CheckUniqueValue(GetType().Name);

                _uow.ApplicationUsers.Add(result, GetLoggedInUserId());
                await _uow.SaveChangesAsync();
                return result;
            }, new BusinessBaseRequest {
                MethodBase = MethodBase.GetCurrentMethod()
            }));
        }
Beispiel #3
0
        public async Task <bool> RegisterUser(ApplicationUserRequest request)
        {
            var user = request.GetModel();

            user.Id = Guid.NewGuid().ToString();

            var response = await _userManager.CreateAsync(user, request.Password);

            return(response.Succeeded);
        }
        public async Task <ActionResult> SignUp([FromBody] ApplicationUserRequest requestBody)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new { errors = new { global = "Request body is invalid" } }));
            }

            var testEmailUnique = await this.userManager.FindByEmailAsync(requestBody.Email);

            if (testEmailUnique != null)
            {
                return(BadRequest(new { errors = new { global = "This e-mail is already taken" } }));
            }

            var applicationUser = new ApplicationUser
            {
                UserName = requestBody.UserName,
                Email    = requestBody.Email,
                FullName = requestBody.FullName
            };

            try
            {
                var result = await this.userManager.CreateAsync(applicationUser, requestBody.Password);

                // Manually added Roles in DB = "1 - Admin" & "2 - Customer"
                if (requestBody.AdminKey != null && requestBody.AdminKey == this.config["ADMIN_KEY"])
                {
                    await this.userManager.AddToRoleAsync(applicationUser, "Admin");
                }
                else
                {
                    await this.userManager.AddToRoleAsync(applicationUser, "Customer");
                }

                if (result.Succeeded)
                {
                    return(Ok(new { email = requestBody.Email }));
                }
                else
                {
                    var error = result.Errors.ToList().First();
                    var code  = error.Code;
                    var desc  = error.Description;
                    return(BadRequest(new { errors = new { global = code + ": " + desc } }));
                }
            }
            catch (Exception ex) { throw ex; }
        }
        public ApplicationUserResponse CreateUser(ApplicationUserRequest user)
        {
            var response = new ApplicationUserResponse();

            try
            {
                if (!UserCreationValidation(user, response))
                {
                    return(response);
                }
                var userApp = MapperHelper.ApplicationUserMapper(user.ApplicationUser);
                var res     = UserManager.Create(userApp, user.ApplicationUser.Password);
                if (res.Errors.Count() > 0)
                {
                    response.ResponseStatus  = ResponseStatus.BadRequest;
                    response.ResponseMessage = MessageDescription.MessageDescription.CreateUserFailed.GetDescription();
                }
                else
                {
                    if (user.ApplicationUser.RoleId > 0)
                    {
                        //TODO:
                        var role = user.ApplicationUser.RoleId;
                        var r    = RoleManager.FindById(role.ToString());
                        if (r == null)
                        {
                            response.ResponseStatus  = ResponseStatus.BadRequest;
                            response.ResponseMessage = MessageDescription.MessageDescription.RoleNotFound.GetDescription();
                            UserManager.Delete(userApp);
                        }
                        else
                        {
                            UserManager.AddToRole(userApp.Id, r.Name);
                            response.UserId          = userApp.Id;
                            response.ResponseStatus  = ResponseStatus.Ok;
                            response.ResponseMessage = MessageDescription.MessageDescription.UserStatusIsOk.GetDescription();
                        }
                    }
                }
                return(response);
            }
            catch (Exception ex)
            {
                response.ResponseStatus  = ResponseStatus.ExpectationFailed;
                response.ResponseMessage = MessageDescription.MessageDescription.TransactionFailed.GetDescription();
                Logger.ErrorException(ex.Message, ex);
                return(response);
            }
        }
Beispiel #6
0
        private static bool UserCreationValidation(ApplicationUserRequest user, ApplicationUserResponse response)
        {
            var isOk = true;

            if (user == null)
            {
                isOk = false;
                response.ResponseStatus = new ResponseStatus()
                {
                    StatusCode = (int)HttpStatusCode.BadRequest,
                    HttpStatus = HttpStatusCode.BadRequest.ToString(),
                    Message    = MessageDescription.ApplicationUserRequestIsNull.GetDescription(),
                };
            }
            else if (user.ApplicationUser == null)
            {
                isOk = false;
                response.ResponseStatus = new ResponseStatus()
                {
                    StatusCode = (int)HttpStatusCode.BadRequest,
                    HttpStatus = HttpStatusCode.BadRequest.ToString(),
                    Message    = MessageDescription.ApplicationUserDTOIsNull.GetDescription(),
                };
            }
            //else if (user.ApplicationUser.RoleId .IsNullOrEmpty())
            //{
            //    isOk = false;
            //    response.ResponseStatus = new ResponseStatus()
            //    {
            //        StatusCode = (int)HttpStatusCode.BadRequest,
            //        HttpStatus = HttpStatusCode.BadRequest.ToString(),
            //        Message = MessageDescription.ApplicationUserRequestIsNull.GetDescription(),
            //    };
            //}
            return(isOk);
        }
        public async Task <IActionResult> CreateUser(ApplicationUserRequest request)
        {
            var isCreated = await _registrationRepository.RegisterUser(request);

            return(Ok(isCreated));
        }
Beispiel #8
0
        public ApplicationUserResponse CreateUser(ApplicationUserRequest user)
        {
            var response = new ApplicationUserResponse();

            try
            {
                if (!UserCreationValidation(user, response))
                {
                    return(response);
                }
                var userApp = MapperHelper.ApplicationUserMapper(user.ApplicationUser);
                var res     = UserManager.Create(userApp, user.ApplicationUser.Password);
                if (res.Errors.Count() > 0)
                {
                    response.ResponseStatus = new ResponseStatus()
                    {
                        StatusCode = (int)HttpStatusCode.BadRequest,
                        HttpStatus = HttpStatusCode.BadRequest.ToString(),
                        Message    = MessageDescription.CreateUserFailed.GetDescription(),
                        Errors     = new List <ResponseError>
                        {
                            new ResponseError
                            {
                                Message = res.Errors.FirstOrDefault()
                            }
                        }
                    };
                }
                else
                {
                    if (!user.ApplicationUser.RoleId.IsNullOrEmpty())
                    {
                        //TODO:
                        var r = RoleManager.FindById(user.ApplicationUser.RoleId);
                        if (r == null)
                        {
                            response.ResponseStatus = new ResponseStatus()
                            {
                                StatusCode = (int)HttpStatusCode.BadRequest,
                                HttpStatus = HttpStatusCode.BadRequest.ToString(),
                                Message    = MessageDescription.RoleNotFound.GetDescription(),
                                Errors     = new List <ResponseError>()
                                {
                                    new ResponseError()
                                    {
                                        Message = MessageDescription.RoleNotFound.GetDescription()
                                    }
                                }
                            };
                            UserManager.Delete(userApp);
                        }
                        else
                        {
                            UserManager.AddToRole(userApp.Id, r.Name);
                            response.UserId         = userApp.Id;
                            response.ResponseStatus = new ResponseStatus
                            {
                                StatusCode = (int)HttpStatusCode.OK,
                                HttpStatus = HttpStatusCode.OK.ToString(),
                                Message    = MessageDescription.RoleNotFound.GetDescription()
                            };
                        }
                    }
                }
                return(response);
            }
            catch (Exception ex)
            {
                response.ResponseStatus = new ResponseStatus
                {
                    StatusCode = (int)HttpStatusCode.ExpectationFailed,
                    HttpStatus = HttpStatusCode.ExpectationFailed.ToString(),
                    Message    = MessageDescription.TransactionFailed.GetDescription()
                };
                return(response);
            }
        }