示例#1
0
        /// <summary>
        /// Создает зарегестрированного пользователя
        /// </summary>
        /// <param name="model">Модель для регистрации пользователя</param>
        /// <returns>ServiceResponce</returns>
        public ServiceResponce CreateSystemUser(RegisterUserModel model)
        {
            if (!model.GeneratePassword && !PasswordService.IsPasswordAcceptable(model.Password))
            {
                return(ServiceResponce
                       .FromFailed()
                       .Add("error", "Password not acceptable"));
            }

            model.Phone = PhoneService.PhoneConvert(model.Phone);
            if (_userRep.CountByCredentails(model.Email, model.Phone) != 0)
            {
                return(ServiceResponce
                       .FromFailed()
                       .Add("error", "User with this Email or Phone already exist"));
            }

            // Генерируем и хэшируем пароль
            string UnHashedPassword = model.Password;

            if (model.GeneratePassword)
            {
                UnHashedPassword = PasswordService.GeneratePasswordString();
            }
            model.Password = PasswordService.GeneratePasswordHash(UnHashedPassword);

            User user = RegisterUserModelHelper.CreateUser(model);

            _userRep.Save(user);
            ServiceResponce response = ServiceResponce
                                       .FromSuccess()
                                       .Result("User registered")
                                       .Add("UserId", user.Id);

            if (model.GeneratePassword)
            {
                response.Add("GeneratedPassword", UnHashedPassword);
            }

            if (model.NotSendWelcome == false)
            {
                // Создаем задачу отправки сообщения в фоне и запускаем ее
                new Thread(send =>
                {
                    RegisteredEmailModel RegisteredEmailModel
                        = RegisteredEmailModelHelper.GetRegisteredEmailModel(model, UnHashedPassword);
                    string RegisteredText = _templateServ
                                            .Run("Emails/Registered", typeof(RegisteredEmailModel), RegisteredEmailModel);
                    EmailService.SendMail(RegisteredEmailModel, RegisteredText);
                }).Start();
            }

            return(response);
        }
示例#2
0
        /// <summary>
        /// Проверяет аутентификацию пользователя по
        /// <paramref name="UserName" />, <paramref name="Phone" />, <paramref name="Password" />,
        /// в случае успеха возвращает сущность пользователя;
        /// При проверке <paramref name="Phone" /> преобразуется в формат <see cref="PhoneService.PhoneConvert(string)" />,
        /// <paramref name="UserName" /> приводится в нижний регистр
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="Phone"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public User Authenticate(string UserName, string Phone, string Password)
        {
            Password = PasswordService.GeneratePasswordHash(Password);
            Phone    = PhoneService.IsPhoneValid(Phone) ? PhoneService.PhoneConvert(Phone) : null;
            UserName = !string.IsNullOrEmpty(UserName) ? UserName.ToLower() : null;
            UserStatusType AvailableStatus = UserStatusType.System;

            if (UserName == null && Phone == null)
            {
                return(null);
            }

            IList <User> users = null;

            if (UserName != null && Phone != null)
            {
                users = AuthRep.FindAllByNamePhone(UserName, Phone, Password, AvailableStatus);
            }
            else if (UserName != null)
            {
                users = AuthRep.FindAllByName(UserName, Password, AvailableStatus);
            }
            else
            {
                users = AuthRep.FindAllByPhone(Phone, Password, AvailableStatus);
            }

            if (users == null || users.Count == 0)
            {
                return(null);
            }
            else if (users.Count == 1)
            {
                //TODO это зачем?
                User toReturn = null;
                foreach (User u in users)
                {
                    toReturn = u;
                }
                return(toReturn);
            }
            else
            {
                throw new Exception("CredentailsService: found more than 1 user with specified credentails");
            }
        }