Exemplo n.º 1
0
        private void RegisterNewUser()
        {
            try
            {
                var person = GetPersonFromOrder();
                person.LoginCredential.NewPassword = _loginService.GeneratePassword();

                using (_securityContextService.ActAsSystem())
                {
                    _personService.Create(person);
                }

                var orderCarrier = _requestModelAccessor.RequestModel.Cart.OrderCarrier;
                orderCarrier.CustomerInfo.PersonID       = person.SystemId;
                orderCarrier.CustomerInfo.CustomerNumber = person.Id;

                _mailService.SendEmail(_welcomeEmailDefinitionResolver.Get(person.MapTo <WelcomeEmailModel>(), person.Email), false);

                _loginService.Login(person.LoginCredential.Username, person.LoginCredential.NewPassword, out SecurityToken token);
            }
            catch (Exception ex)
            {
                this.Log().Error("New user registration failed.", ex);
                throw;
            }
        }
        public bool Create(BusinessPersonViewModel viewModel, ModelStateDictionary modelState)
        {
            var selectedRole = GetRoles().First(x => x.Id == viewModel.Role);

            try
            {
                var person = viewModel.MapTo <Person>();
                person.LoginCredential.PasswordExpirationDate = DateTimeOffset.UtcNow;
                person.OrganizationLinks = new List <PersonToOrganizationLink>()
                {
                    new PersonToOrganizationLink(_personStorage.CurrentSelectedOrganization.SystemId)
                    {
                        RoleSystemIds = new HashSet <Guid>()
                        {
                            selectedRole.SystemId
                        }
                    }
                };

                var template = _templateService.Get <PersonFieldTemplate>(typeof(CustomerArea), DefaultWebsiteFieldValueConstants.CustomerTemplateId);
                person.FieldTemplateSystemId = template.SystemId;

                person.LoginCredential.NewPassword = _loginService.GeneratePassword();

                using (_securityContextService.ActAsSystem())
                {
                    _personService.Create(person);
                }

                _mailService.SendEmail(_welcomeEmailDefinitionResolver.Get(person.MapTo <WelcomeEmailModel>(), person.Email), false);

                return(true);
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, "{Message}", ex.Message);
                modelState.AddModelError("general", "mypage.person.unabletocreate".AsWebsiteText());
                return(false);
            }
        }