changekey.cpp

来自「偶作的毕业设计程序」· C++ 代码 · 共 184 行

CPP
184
字号
// ChangeKey.cpp : implementation file
//

#include "stdafx.h"
#include "SoftDocSystem.h"
#include "ChangeKey.h"
#include "Rrypt.h"
#include "LogMngr.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChangeKey dialog
extern int PublicUserType;

CChangeKey::CChangeKey(CWnd* pParent /*=NULL*/)
	: CDialog(CChangeKey::IDD, pParent)
{
	//{{AFX_DATA_INIT(CChangeKey)
	m_UserName = _T("");
	m_OldKey = _T("");
	m_NewKey = _T("");
	m_NewKeyCh = _T("");
	m_OwnerType = -1;
	//}}AFX_DATA_INIT
}


void CChangeKey::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChangeKey)
	DDX_Text(pDX, IDC_EDIT1, m_UserName);
	DDX_Text(pDX, IDC_EDIT2, m_OldKey);
	DDX_Text(pDX, IDC_EDIT3, m_NewKey);
	DDX_Text(pDX, IDC_EDIT4, m_NewKeyCh);
	DDX_Radio(pDX, IDC_RADIO1, m_OwnerType);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChangeKey, CDialog)
	//{{AFX_MSG_MAP(CChangeKey)
	ON_BN_CLICKED(IDOK, OnChange)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChangeKey message handlers

void CChangeKey::OnChange() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CString sql_;	
	if(m_UserName.IsEmpty()){AfxMessageBox("请输入用户名!");return;}
	if(m_NewKey!=m_NewKeyCh){AfxMessageBox("新密码两次不一致!");return;}
	CString oldkey,newkey;
	if(m_OldKey.IsEmpty())oldkey=_T("RX67YM3482C");
	else oldkey=CCrypt::Encrypt(m_OldKey,376);
	if(m_NewKey.IsEmpty())newkey=_T("RX67YM3482C");
	else newkey=CCrypt::Encrypt(m_NewKey,376);
	//验证用户名和旧密码
	if(m_OwnerType==1){
		
		sql_.Format ("SELECT * FROM UserInfo WHERE 用户名 = '%s' AND 密码 = '%s'",m_UserName,oldkey);
		_bstr_t sql=sql_;
		_bstr_t source=m_strSource;
		_RecordsetPtr Myset;
		Myset.CreateInstance (__uuidof(Recordset));
		try{
			Myset->Open (sql,source,adOpenDynamic,adLockOptimistic,adCmdText);}
		catch(_com_error& e){
			AfxMessageBox(e.ErrorMessage());}
		
		int Count=0;
		while(!Myset->adoEOF)
		{Count+=1;Myset->MoveNext();};
		if(Count<1){AfxMessageBox("你输入的用户名和原密码有误,请重新输入!");return;}
	try{
		m_DBCnt->BeginTrans();//开始事务
		sql_.Format ("update UserInfo set 密码 = '%s'",newkey);
		_bstr_t sql1=sql_;
		try{
			m_DBCnt->Execute(sql1,NULL,adCmdText);}
		catch(_com_error& e){
			AfxMessageBox(e.ErrorMessage());return;}
		m_DBCnt->CommitTrans();
	}
	catch(_com_error& e){
		m_DBCnt->RollbackTrans();return;}
			if(m_Log.Setup (m_DBCnt))
			m_Log.Addlog (m_UserName,"更改密码","未知");//操作日志
	AfxMessageBox("你的用户密码已成功修改,请牢记新密码!",MB_ICONINFORMATION);
}
else{//管理员密码更改
	CString m_Filename=".\\ManageKey.ini";
	CFile cf;
	char str1[20],str4[20];
	CString str2,str3,KeyWord;
	CString GuanliName,GuanliKey;
	GuanliName=_T("lilingyun");
	GuanliKey=CCrypt::Encrypt("17910",376);
	bool IsFileExist=false;
	if(!cf.Open(m_Filename,CFile::modeRead))
		//建立这个文件
	{IsFileExist=true;}
	else
		cf.Close();
	if(IsFileExist){
		try
		{ WritePrivateProfileString("ManagerKey","Manager",GuanliName,m_Filename);
		WritePrivateProfileString("ManagerKey","Key",GuanliKey,m_Filename);}
		catch(CFileException *e)
		{AfxMessageBox(e->m_cause);return;}
		KeyWord=GuanliKey;
		str3=GuanliName;
	}
	else
	{
		GetPrivateProfileString("ManagerKey","Mangnager",GuanliName,str1,20,m_Filename);
		str3=str1;
		GetPrivateProfileString("ManagerKey","Key",GuanliKey,str4,20,m_Filename);
		KeyWord=str4;}
	if(m_UserName==str3 && oldkey==KeyWord)
	{//写入新密码
		WritePrivateProfileString("ManagerKey","Manager",GuanliName,m_Filename);
		WritePrivateProfileString("ManagerKey","Key",newkey,m_Filename);
				if(m_Log.Setup (m_DBCnt))
				m_Log.Addlog ("管理员","修改密码","未知");//操作日志
		AfxMessageBox("你的管理员密码已成功修改,请牢记新密码!",MB_ICONINFORMATION);
	}
else
{AfxMessageBox("你输入的信息不正确,请重新输入");return;}
}

}

BOOL CChangeKey::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	 CSoftDocSystemApp* myApp=(CSoftDocSystemApp*)AfxGetApp();
	 CString source_;
	 source_.Format ("driver={SQL Server};server=%s;database=SoftDocument",myApp->ConnectServer);
	_bstr_t source=source_;
	_bstr_t pwd=myApp->ConnectPwd;
	_bstr_t user=myApp->ConnectUser;
	HRESULT hr;
	try{
		hr = m_DBCnt.CreateInstance(_uuidof(Connection));
		if(SUCCEEDED(hr))
			hr = m_DBCnt->Open(source, user, pwd, 16);
		if(SUCCEEDED(hr))
		{m_fConnected = TRUE;
		
		}
		else 
			m_fConnected = FALSE;
	}
	catch (_com_error &e){
		MessageBox(e.ErrorMessage()); 
		m_fConnected = FALSE;
	}
	if(m_DBCnt->GetState()!=adStateOpen) 
	{MessageBox("ADO数据源初始化失败!");
	return false;}
		else
		m_strSource = (char * )source;
		//如果是一般用户,则禁止修改管理员密码
	((CButton*)GetDlgItem(IDC_RADIO2))->SetCheck(true);
	if(PublicUserType!=1)
	{
		((CButton*)GetDlgItem(IDC_RADIO1))->EnableWindow(false);
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?