Ejemplo n.º 1
0
        /// <summary>
        /// Try to log in
        /// </summary>
        /// <param name="failMessage">message for user, when login has been failed</param>
        /// <returns>status of logging in attempt</returns>
        private static LoginResult Login(ref string failMessage)
        {
            failMessage = Program.LanguageManager.GetString(StringResources.Message_AuthentificationFailed);
            LoginForm dlg = new LoginForm();

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string login    = dlg.Login;
                string password = dlg.Password;

#if DEBUG
                if (string.IsNullOrWhiteSpace(dlg.Login) && string.IsNullOrWhiteSpace(dlg.Password))
                {
                    login    = "******";
                    password = "******";
                }
#endif

                User user = new User()
                {
                    IsActive = false, Login = "******"
                };


                IUserRepository userRepo;


                using (userRepo = Kernel.Get <IUserRepository>())
                {
                    user = userRepo.FindByLogin(login);

                    if (user == null)
                    {
                        return(LoginResult.Failed);
                    }
                    if (!user.IsActive)
                    {
                        failMessage = string.Format(
                            Program.LanguageManager.GetString(StringResources.Message_AuthentificationFailedUserInactive), login);
                        return(LoginResult.FailedUserInactive);
                    }
                }

                userRepo = (UserRepository)Program.Kernel.GetService(typeof(UserRepository));

                string hash = PasswordEncryptor.EncryptPassword(password);

                if (user.PasswordHash != hash)
                {
                    return(LoginResult.Failed);
                }

                if (user.PasswordExpires != null && user.PasswordExpires < DateTime.Now)
                {
                    PasswordChangeDialog dlgPassChange = new PasswordChangeDialog();

                    if (dlgPassChange.ShowPasswordDialog(user.PasswordHash) ==
                        System.Windows.Forms.DialogResult.OK)
                    {
                        try
                        {
                            user.PasswordHash    = dlgPassChange.NewPasswordHash;
                            user.PasswordExpires = DateTime.Now.AddMonths(monthsCountPasswordProlongation);

                            userRepo.BeginTransaction();
                            userRepo.SaveOrUpdate(user);
                            userRepo.Commit();
                            userRepo.Evict(user);
                        }
                        catch (RepositoryException ex)
                        {
                            log.Error(ex.Message);
                        }
                    }
                    else
                    {
                        return(LoginResult.Failed);
                    }
                }

                ISecurityContext ctx = Kernel.Get <ISecurityContext>();
                ctx.LoggedUser = user;

                HibernateUtil.CurrentUser = ctx.GetLoggedPerson();
                return(LoginResult.LoggedIn);
            }
            else
            {
                System.Environment.Exit(0);
            }

            return(LoginResult.Failed);
        }