示例#1
0
        // 修改用户密码。这是指用户修改自己帐户的密码,需提供旧密码
        // return:
        //      -1  error
        //      0   succeed
        public int ChangeUserPassword(
            string strLibraryCodeList,
            string strUserName,
            string strOldPassword,
            string strNewPassword,
            out string strError)
        {
            strError = "";
            int nRet = 0;

            if (String.IsNullOrEmpty(strUserName) == true)
            {
                strError = "strUserName参数值不能为空";
                return(-1);
            }

            this.m_lock.AcquireWriterLock(m_nLockTimeout);
            try
            {
                // 查重
                XmlNode node = this.LibraryCfgDom.DocumentElement.SelectSingleNode("//accounts/account[@name='" + strUserName + "']");
                if (node == null)
                {
                    strError = "用户 '" + strUserName + "' 不存在";
                    return(-1);
                }

                string strExistLibraryCodeList = DomUtil.GetAttr(node, "libraryCode");

                // 2012/9/9
                // 分馆用户只允许修改馆代码属于管辖分馆的帐户
                if (SessionInfo.IsGlobalUser(strLibraryCodeList) == false)
                {
                    if (string.IsNullOrEmpty(strExistLibraryCodeList) == true ||
                        IsListInList(strExistLibraryCodeList, strLibraryCodeList) == false)
                    {
                        strError = "当前用户只能修改图书馆代码完全完全属于 '" + strLibraryCodeList + "' 范围的用户的密码";
                        return(-1);
                    }
                }

                // 验证旧密码
#if NO
                // 以前的做法
                string strExistPassword = DomUtil.GetAttr(node, "password");
                if (String.IsNullOrEmpty(strExistPassword) == false)
                {
                    try
                    {
                        strExistPassword = Cryptography.Decrypt(strExistPassword,
                                                                EncryptKey);
                    }
                    catch
                    {
                        strError = "已经存在的(加密后)密码格式不正确";
                        return(-1);
                    }
                }

                if (strExistPassword != strOldPassword)
                {
                    strError = "所提供的旧密码经验证不匹配";
                    return(-1);
                }
#endif
                string strExistPassword = DomUtil.GetAttr(node, "password");
                nRet = LibraryServerUtil.MatchUserPassword(strOldPassword, strExistPassword, out strError);
                if (nRet == -1)
                {
                    return(-1);
                }
                if (nRet == 0)
                {
                    strError = "所提供的旧密码经验证不匹配";
                    return(-1);
                }

                // 设置新密码
#if NO
                // 以前的做法
                strNewPassword = Cryptography.Encrypt(strNewPassword,
                                                      EncryptKey);
                DomUtil.SetAttr(node, "password", strNewPassword);
#endif
                string strHashed = "";
                nRet = LibraryServerUtil.SetUserPassword(strNewPassword, out strHashed, out strError);
                if (nRet == -1)
                {
                    return(-1);
                }
                DomUtil.SetAttr(node, "password", strHashed);

                this.Changed = true;

                return(0);
            }
            finally
            {
                this.m_lock.ReleaseWriterLock();
            }

            // return 0;
        }
示例#2
0
        // 要求操作者用 supervisor 账号登录一次。以便后续进行各种重要操作。
        // 只需要 library.xml 即可,不需要 dp2library 在运行中。
        // return:
        //      -2  实例没有找到
        //      -1  出错
        //      0   放弃验证
        //      1   成功
        public static int LibrarySupervisorLogin(IWin32Window owner,
                                                 string strInstanceName,
                                                 string strComment,
                                                 out string strError)
        {
            strError = "";

            LibraryInstanceInfo info = null;

            // return:
            //      -1  出错
            //      0   实例没有找到
            //      1   成功
            int nRet = GetLibraryInstanceInfo(
                strInstanceName,
                out info,
                out strError);

            if (nRet == -1)
            {
                return(-1);
            }
            if (nRet == 0)
            {
                strError = "实例 '" + strInstanceName + "' 没有找到";
                return(-2);
            }

            if (string.IsNullOrEmpty(info.SupervisorUserName) == true)
            {
                // TODO: 此时是否可以不用验证了呢?
                strError = "实例 '" + strInstanceName + "' 的账户中,没有找到具有 managedatabase 权限的管理员账户,因此无法验证操作者身份";
                return(-1);
            }

            ConfirmSupervisorDialog dlg = new ConfirmSupervisorDialog();

            GuiUtil.AutoSetDefaultFont(dlg);

            dlg.Comment       = strComment;
            dlg.ServerUrl     = "实例 '" + strInstanceName + "'";
            dlg.UserName      = info.SupervisorUserName;
            dlg.StartPosition = FormStartPosition.CenterScreen;
REDO_LOGIN:
            dlg.ShowDialog(owner);

            if (dlg.DialogResult == DialogResult.Cancel)
            {
                return(0);
            }

            if (info.Version <= 2.0)
            {
                // 以前的做法
                if (dlg.Password != info.SupervisorPassword)
                {
                    MessageBox.Show(owner, "密码不正确。请重新输入密码");
                    goto REDO_LOGIN;
                }
            }
            else
            {
                // 新的做法
                nRet = LibraryServerUtil.MatchUserPassword(dlg.Password,
                                                           info.SupervisorPassword,
                                                           out strError);
                if (nRet == -1)
                {
                    strError = "MatchUserPassword() error: " + strError;
                    return(-1);
                }
                Debug.Assert(nRet == 0 || nRet == 1, "");
                if (nRet == 1)
                {
                    MessageBox.Show(owner, "密码不正确。请重新输入密码");
                    goto REDO_LOGIN;
                }
            }

            return(1);
        }