Beispiel #1
0
        public async Task <IActionResult> CreateUser(CreateUserModel model)
        {
            bool checkEmailDomain = _service.ValidateEmailDomain(model.email);

            if (!checkEmailDomain)
            {
                return(Unauthorized(AppResult.InvalidEmailDomain()));
            }
            var entity = await _service.GetUserByEmailAsync(model.email);

            if (entity != null)
            {
                return(BadRequest(AppResult.EmailExisted()));
            }
            var emailInfo = model.email.GetEmailInfo();

            entity = _service.ConvertToUser(model, emailInfo.Item3);
            using (var transaction = context.Database.BeginTransaction())
            {
                var result = await _service
                             .CreateUserWithoutPassAsync(entity, model.roles);

                if (!result.Succeeded)
                {
                    foreach (var err in result.Errors)
                    {
                        ModelState.AddModelError(err.Code, err.Description);
                    }
                    var builder = ResultHelper.MakeInvalidAccountRegistrationResults(ModelState);
                    return(BadRequest(builder));
                }
                _logger.CustomProperties(entity).Info("Register new user");
                var memberEntity = _memberService.ConvertToMember(entity, emailInfo.Item3);
                memberEntity = _memberService.CreateMember(memberEntity);
                //log event
                var ev = _sysService.GetEventForNewUser(
                    $"Admin has created a user with email {model.email}",
                    memberEntity.UserId);
                _sysService.CreateAppEvent(ev);
                //end log event
                context.SaveChanges();
                transaction.Commit();
            }
            return(NoContent());
        }