Esempio n. 1
0
        /// <summary>
        ///     登录成功
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        private bool LoginSuccess(string userName, string password)
        {
            //定义记录登录成功与否的值
            var isLogin = false;
            var n       = ents.Length;

            for (var i = 0; i < n; i++)
            {
                //验证帐号密码是否正确
                if (ents[i].LoginName == userName && ents[i].PassWord == password)
                {
                    CurrentUser.CurLoginUserInfo = ents[i];
                    //记录最后一次登录用户
                    var sw = new StreamWriter(Application.StartupPath + "\\DefaultUser.txt", false);
                    sw.WriteLine(userName);
                    sw.Close();

                    //记住密码,登录成功,修改用户“尚未登录”为False;根据是否记住密码设定相应的值
                    var userLogin = UserLogin.FindOneByLoginName(userName);
                    userLogin.IsSavePassWord = 0;
                    userLogin.Save();
                    isLogin = true;
                    break;
                }
            }
            return(isLogin);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsBtnDel_Click(object sender, EventArgs e)
        {
            if (Alert.confirm(LibCommon.Const.DELETE_USER_INFO, LibCommon.Const.NOTES, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                //获取登录用户记录条数
                //int n = LoginFormBLL.GetRecordCountFromTable();
                int n = this.fpUserLoginInformation.ActiveSheet.Rows.Count - _fpTitleRowCount;
                //从数据末尾删除数据
                for (int i = n - 1; i >= 0; i--)
                {
                    if (this.fpUserLoginInformation.ActiveSheet.Cells[2 + i, 0].CellType is FarPoint.Win.Spread.CellType.CheckBoxCellType)
                    {
                        if (this.fpUserLoginInformation.ActiveSheet.Cells[2 + i, 0].Value != null)
                        {
                            if ((bool)this.fpUserLoginInformation.ActiveSheet.Cells[2 + i, 0].Value)
                            {
                                this.fpUserLoginInformation.ActiveSheet.Rows.Remove(2 + i, 1);
                                //删除一行数据后,在数据末尾加一行
                                //this.fpUserLoginInformation.ActiveSheet.Rows.Add(n - 1 + 2, 1);
                                //this.fpUserLoginInformation.ActiveSheet.Rows[n - 1 + 2].BackColor = Color.Red;//显示颜色直观显示追加行位置
                            }
                        }
                    }
                }
                //遍历 记录用户信息的数组,删除数据库中的数据
                foreach (string str in _userSel)
                {
                    var userLogin = UserLogin.FindOneByLoginName(str);
                    userLogin.Delete();
                }
                fpUserLoginInformation.Refresh();
                this.fpUserLoginInformation.ActiveSheet.SetActiveCell(UserLogin.Count() + 1, 0);

                //清除所有选择数据
                _userSel.Clear();

                //设置删除按钮是否启用
                this.tsBtnDel.Enabled = false;
                //设置修改按钮是否启用
                this.tsBtnModify.Enabled = false;

                //记录当前farpoint的记录条数
                _fpRowsCount = this.fpUserLoginInformation.ActiveSheet.Rows.Count;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsBtnModify_Click(object sender, EventArgs e)
        {
            //管理界面没有选择时,返回
            if (UserLoginInformationManagement._userSel.Count == 0)
            {
                return;
            }

            //定义  用户登录信息实体,接受旧值,添加到窗体中。旧值来源于管理界面的选择值。可以直接取值,也可从数据库取值,暂不考虑效率。
            UserLogin ent = UserLogin.FindOneByLoginName(UserLoginInformationManagement._userSel[0]);

            //修改
            UserLoginInformationInput ulii = new UserLoginInformationInput(ent);

            ulii.ShowDialog();
            //刷新显示的用户信息
            GetUserLoginInfo();
        }
        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOK_Click(object sender, EventArgs e)
        {
            //登录名
            string loginname = _txtLoginName.Text;
            //密码
            string password = _txtPassWord.Text;
            //所属用户组
            string groupname = _cboGroup.Text;
            //权限
            string permission = _cboPermission.Text;
            //备注
            string remarks = _rtxtRemark.Text;

            //用户登录名不能包含特殊字符且不能为空
            //查空、特殊字符
            if (LibCommon.Validator.checkSpecialCharacters(_txtLoginName.Text.ToString()) || LibCommon.Validator.IsEmpty(_txtLoginName.Text.ToString()))
            {
                Alert.alert(LibCommon.Const.LOGIN_NAME_IS_WRONG, LibCommon.Const.NOTES, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                _txtLoginName.Focus();
                return;
            }

            //添加时查重
            if (_strIsAddOrModify == "add")
            {
                UserLogin entAdd = UserLogin.FindOneByLoginName(_txtLoginName.Text.ToString().Trim());
                if (entAdd != null)
                {
                    if (entAdd.LoginName == _txtLoginName.Text.ToString())
                    {
                        Alert.alert(LibCommon.Const.LOGIN_NAME_EXIST, LibCommon.Const.NOTES, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        _txtLoginName.Focus();
                        return;
                    }
                }
            }
            //修改
            else if (_strIsAddOrModify == "modify")
            {
                UserLogin entModify = UserLogin.Find(_needModifyEnt.Id);
                if (entModify != null)
                {
                    Alert.alert(LibCommon.Const.LOGIN_NAME_EXIST, LibCommon.Const.NOTES, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    _txtLoginName.Focus();
                    return;
                }
            }

            //密码不能包含特殊字符且不能为空
            if (LibCommon.Validator.checkSpecialCharacters(password) || LibCommon.Validator.IsEmptyOrBlank(password))
            {
                Alert.alert(LibCommon.Const.PASS_WORD_IS_WRONG, LibCommon.Const.NOTES, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                _txtPassWord.Focus();
                return;
            }

            //确认密码不能为空且必须与密码值相同
            if (_txtConfirmPassword.Text != _txtPassWord.Text)
            {
                Alert.alert(LibCommon.Const.CONFIRM_PASSWORD_IS_WRONG, LibCommon.Const.NOTES, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                _txtConfirmPassword.Focus();
                return;
            }

            //权限选择不能为空
            if (_cboPermission.SelectedItem == null)
            {
                Alert.alert(LibCommon.Const.PERMISSION_IS_WRONG, LibCommon.Const.NOTES, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                _cboPermission.Focus();
                return;
            }

            //定义用户实体
            UserLogin ent = new UserLogin();

            //登录名
            ent.LoginName = loginname;
            //密码
            ent.PassWord = password;
            //权限
            ent.Permission = permission;
            //所属用户组
            ent.GroupName = groupname;
            //备注
            ent.Remarks = remarks;
            //尚未登录系统,在插入新值时,默认为True
            ent.IsLogined = 0;
            //记住密码,在插入新值时,默认为False
            ent.IsSavePassWord = 0;

            //添加
            if (_strIsAddOrModify == "add")
            {
                //数据库插值
                ent.Save();
                UserGroupInformationManagementBLL.UpdateUserCountFromUserGroup(ent.GroupName);
            }
            //修改
            if (_strIsAddOrModify == "modify")
            {
                //数据库更新,ent代表修改的实体,参数2代表LoginName
                UserLogin.FindOneByLoginName(UserLoginInformationManagement._userSel[0]);
                ent.LoginName      = loginname;
                ent.PassWord       = password;
                ent.Permission     = permission;
                ent.GroupName      = groupname;
                ent.Remarks        = remarks;
                ent.IsLogined      = 0;
                ent.IsSavePassWord = 0;
                ent.Save();
            }

            //初次登录系统时,需要添加新用户。记录初次登录的用户名与密码,并直接输入到LoginForm中
            if (LibCommon.Const.FIRST_TIME_LOGIN)
            {
                LibCommon.Const.FIRST_LOGIN_NAME       = ent.LoginName;
                LibCommon.Const.FIRST_LOGIN_PASSWORD   = ent.PassWord;
                LibCommon.Const.FIRST_LOGIN_PERMISSION = ent.Permission;
                this.Close();
                return;
            }
            this.Close();
        }