Inheritance: Prizm.Main.Forms.MainChildForm.PrizmForm
示例#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.LoggedUser;
                return LoginResult.LoggedIn;
            }
            else
            {
                System.Environment.Exit(0);
            }

            return LoginResult.Failed;
        }
示例#2
0
        private void repositoryItemButtonUserPass_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var view = gridViewUsers;
            if (view.IsValidRowHandle(view.FocusedRowHandle))
            {
                User user = view.GetRow(view.FocusedRowHandle) as User;
                if (user != null)
                {
                    PasswordChangeDialog dlg = new PasswordChangeDialog();
                    if (dlg.ShowPasswordDialog(user.PasswordHash) == System.Windows.Forms.DialogResult.OK)
                    {
                        user.PasswordHash = dlg.NewPasswordHash;
                        IsModified = true;
                    }
                }

            }
        }