示例#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>
        /// Отсылает пользователю новый сгенерированный пароль по Email,
        /// если, конечно он зарегестрирован в системе
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ServiceResponce RestorePassword(RestorePasswordModel model)
        {
            IList <User> users = _userRep.FindAllByCredentails(model.Email, model.Phone);

            if (users == null)
            {
                return(ServiceResponce
                       .FromFailed()
                       .Add("error", "user doesn't exist"));
            }

            /*       if (users.Count > 1)
             *     {
             *         return ServiceResponce
             *             .FromFailed()
             *             .Add("error", "user doesn't exist");
             *     } */

            User user = new User();

            if (users.Count == 1)
            {
                foreach (User u in users)
                {
                    user = u;
                }
            }

            string Password = PasswordService.GeneratePasswordString();

            user.Password = PasswordService.GeneratePasswordHash(Password);
            _userRep.Save(user);

            new Thread(send =>
            {
                RestorePasswordEmailModel RestorePasswordEmailModel = RestorePasswordEmailModelHelper.GetRestorePasswordEmailModel(user.UserName, Password);
                string RestoreText = _templateServ
                                     .Run("Emails/RestorePassword", typeof(RestorePasswordEmailModel), RestorePasswordEmailModel);
                if (!EmailService.SendMail(RestorePasswordEmailModel, RestoreText))
                {
                    user.UserStatuses.Description = "Restore password Email was not delivered";
                    user.UserStatuses.UpdateTime  = DateTime.Now;
                }
                else
                {
                    user.UserStatuses.Description = "Restore password Email was delivered at " + DateTime.Now;
                    user.UserStatuses.UpdateTime  = DateTime.Now;
                }
            }).Start();

            return(ServiceResponce.FromSuccess());
        }
示例#3
0
        /// <summary>
        /// Обновляет данные приглашения пользователя
        /// и переводит статус приглашения в статус ожидающий
        /// "Акцепта(ТЗ стр.8)"
        /// </summary>
        /// <param name="code">Код приглашения</param>
        /// <param name="model">Модель с данными для обновления</param>
        /// <returns></returns>
        public ServiceResponce UpdateInvite(string code, UpdateInviteModel model)
        {
            InviteCode invite = InviteRep.FindOneByCode(code);

            if (invite == null)
            {
                return(ServiceResponce
                       .FromFailed()
                       .Add("error", "invite not found"));
            }

            User user = UserRep.FindOneById(invite.User.Id);

            if (!StatusService.CanChangeStatus(user, UserStatusType.AcceptInvite))
            {
                return(ServiceResponce
                       .FromFailed()
                       .Add("error", "it isn't possible to change the status to AcceptInvited"));
            }


            if (!UserService.CanUpdateUserCredentails(user.Id, model.Email, model.Phone, UserRep))
            {
                return(ServiceResponce
                       .FromFailed()
                       .Add("error", "user with such Email or Phone already exists"));
            }

            if (!model.GeneratePassword && !PasswordService.IsPasswordAcceptable(model.Password))
            {
                return(ServiceResponce
                       .FromFailed()
                       .Add("error", "Password not acceptable"));
            }

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

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

            user = UpdateInviteModelHelper.UpdateInviteUser(user, model);



            UserRep.Save(user);
            InviteRep.Delete(invite.Id);

            ServiceResponce response = ServiceResponce
                                       .FromSuccess()
                                       .Result("invite accepted");

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