Exemplo n.º 1
0
        public IUser CreateUser(CreateUserParams createUserParams)
        {
            Logger.Information("CreateUser {0} {1}", createUserParams.Username, createUserParams.Email);

            var user = new UserRecord();

            user.UserName           = createUserParams.Username;
            user.Email              = createUserParams.Email;
            user.NormalizedUserName = createUserParams.Username.ToLowerInvariant();
            user.HashAlgorithm      = PBKDF2;
            SetPassword(user, createUserParams.Password);



            user.RegistrationStatus = UserStatus.Approved;
            user.EmailStatus        = UserStatus.Approved;


            if (createUserParams.IsApproved)
            {
                user.RegistrationStatus = UserStatus.Approved;
                user.EmailStatus        = UserStatus.Approved;
            }

            var userContext = new UserContext {
                User = user, Cancel = false, UserParameters = createUserParams
            };

            _userEventHandlers.Creating(userContext);

            if (userContext.Cancel)
            {
                return(null);
            }

            _userRepository.Create(user);

            _userEventHandlers.Created(userContext);
            if (user.RegistrationStatus == UserStatus.Approved)
            {
                _userEventHandlers.Approved(user);
            }

            return(user);
        }
Exemplo n.º 2
0
        public IUser CreateUser(CreateUserParams createUserParams)
        {
            Logger.Information("CreateUser {0} {1}", createUserParams.Username, createUserParams.Email);

            var registrationSettings = _orchardServices.WorkContext.CurrentSite.As <RegistrationSettingsPart>();

            var user = _orchardServices.ContentManager.New <UserPart>("User");

            user.UserName           = createUserParams.Username;
            user.Email              = createUserParams.Email;
            user.NormalizedUserName = createUserParams.Username.ToLowerInvariant();
            user.HashAlgorithm      = PBKDF2;
            SetPassword(user, createUserParams.Password);

            if (registrationSettings != null)
            {
                user.RegistrationStatus = registrationSettings.UsersAreModerated ? UserStatus.Pending : UserStatus.Approved;
                user.EmailStatus        = registrationSettings.UsersMustValidateEmail ? UserStatus.Pending : UserStatus.Approved;
            }

            if (createUserParams.IsApproved)
            {
                user.RegistrationStatus = UserStatus.Approved;
                user.EmailStatus        = UserStatus.Approved;
            }

            var userContext = new UserContext {
                User = user, Cancel = false, UserParameters = createUserParams
            };

            _userEventHandlers.Creating(userContext);

            if (userContext.Cancel)
            {
                return(null);
            }

            _orchardServices.ContentManager.Create(user);

            _userEventHandlers.Created(userContext);
            if (user.RegistrationStatus == UserStatus.Approved)
            {
                _userEventHandlers.Approved(user);
            }

            if (registrationSettings != null &&
                registrationSettings.UsersAreModerated &&
                registrationSettings.NotifyModeration &&
                !createUserParams.IsApproved)
            {
                var usernames = String.IsNullOrWhiteSpace(registrationSettings.NotificationsRecipients)
                                    ? new string[0]
                                    : registrationSettings.NotificationsRecipients.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var userName in usernames)
                {
                    if (String.IsNullOrWhiteSpace(userName))
                    {
                        continue;
                    }
                    var recipient = GetUser(userName);
                    if (recipient != null)
                    {
                        var template = _shapeFactory.Create("Template_User_Moderated", Arguments.From(createUserParams));
                        template.Metadata.Wrappers.Add("Template_User_Wrapper");

                        var parameters = new Dictionary <string, object> {
                            { "Subject", T("New account").Text },
                            { "Body", _shapeDisplay.Display(template) },
                            { "Recipients", new [] { recipient.Email } }
                        };

                        _messageService.Send("Email", parameters);
                    }
                }
            }

            return(user);
        }