public async Task <IActionResult> Create(CreateAppUserModel model)
        {
            if (ModelState.IsValid)
            {
                var validationData = _service.ValidateCreateAppUser(User, model);
                if (!validationData.IsValid)
                {
                    return(BadRequest(AppResult.FailValidation(data: validationData)));
                }
                IdentityResult result;
                using (var trans = context.Database.BeginTransaction())
                {
                    var entity = _service.ConvertToUser(model);
                    result = await _service
                             .CreateUserWithRolesTransactionAsync(entity, model.Password, new[] { model.Role });

                    if (result.Succeeded)
                    {
                        trans.Commit();
                        _logger.CustomProperties(entity).Info("Register new user");
                        return(Created($"/{Business.Constants.ApiEndpoint.USER_API}?id={entity.Id}",
                                       AppResult.Success(entity.Id)));
                    }
                }
                foreach (var err in result.Errors)
                {
                    ModelState.AddModelError(err.Code, err.Description);
                }
            }
            var appResult = ResultHelper.MakeInvalidAccountRegistrationResults(ModelState);

            return(BadRequest(appResult));
        }
Ejemplo n.º 2
0
        public AppUser CreateAppUser(CreateAppUserModel model)
        {
            var entity = model.ToDest();

            PrepareCreate(entity);
            return(context.Users.Add(entity).Entity);
        }
Ejemplo n.º 3
0
        public AppUser ConvertToUser(CreateAppUserModel model)
        {
            var entity = new AppUser {
                UserName = model.UserName, FullName = model.FullName
            };

            return(entity);
        }
Ejemplo n.º 4
0
        public ValidationData ValidateCreateAppUser(ClaimsPrincipal principal,
                                                    CreateAppUserModel model)
        {
            var validationData = new ValidationData();

            if (string.IsNullOrWhiteSpace(model.Role) ||
                !Data.Constants.RoleName.ALL.Contains(model.Role))
            {
                validationData = validationData.Fail("Invalid role", code: Constants.AppResultCode.FailValidation);
            }
            if (string.IsNullOrWhiteSpace(model.UserName) || string.IsNullOrWhiteSpace(model.Password) ||
                model.UserName.Length < 3 || model.Password.Length < 6)
            {
                validationData = validationData.Fail("Invalid username or password", code: Constants.AppResultCode.FailValidation);
            }
            return(validationData);
        }