📄 powsetdialog.cpp
字号:
}
void CPowSetDialog::OnBnClickedLeave()
{
m_pConnection->Close();
m_pConnection = NULL;
CDialog::OnCancel();
}
void CPowSetDialog::OnBnClickedButtonAddpow()
{
CButton *pButton = (CButton*) GetDlgItem(IDOK);
pButton->EnableWindow(TRUE);
pButton = (CButton*)GetDlgItem (IDC_CANCEL);
pButton->EnableWindow(TRUE);
CEdit *pEdit = (CEdit*)GetDlgItem (IDC_EDIT1);
pEdit->EnableWindow(TRUE);
pEdit->SetFocus();
pButton = (CButton*)GetDlgItem (IDC_BUTTON_MODIFP);
pButton->EnableWindow(FALSE);
pButton = (CButton*)GetDlgItem (IDC_BUTTON_DELPOW);
pButton->EnableWindow(FALSE);
ControlCanUse();
m_FlagAdd = TRUE;
}
void CPowSetDialog::OnBnClickedOk()
{
if(m_FlagAdd)
{
_RecordsetPtr m_pRecordsetM; //PowerManagerTable
_RecordsetPtr m_pRecordsetP; //PowerNameTable
_RecordsetPtr m_pRecordsetMM;//ManagerModuleTable
HRESULT hrM;
HRESULT hrP;
HRESULT hrMM;
hrM = m_pRecordsetM.CreateInstance (__uuidof(Recordset));
hrP = m_pRecordsetP.CreateInstance (__uuidof(Recordset));
hrMM = m_pRecordsetMM.CreateInstance(__uuidof(Recordset));
if(FAILED(hrM) || FAILED(hrP) || FAILED(hrMM))
{
AfxMessageBox(_T("不能创建实例!"));
return;
}
hrM = m_pRecordsetM->Open ("select * from PowerManagerTable",
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
hrP = m_pRecordsetP->Open ("select * from PowerNameTable",
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
hrMM = m_pRecordsetMM->Open ("select * from ManagerModuleTable",
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(SUCCEEDED(hrM) && SUCCEEDED(hrP) && SUCCEEDED(hrMM) )
{
if(!m_pRecordsetM->adoEOF && !m_pRecordsetP->adoEOF && !m_pRecordsetMM->adoEOF)
{
CEdit *pEdit = (CEdit*) GetDlgItem(IDC_EDIT1);
CString strValue;
pEdit->GetWindowText (strValue);
_variant_t var;
CString strNum;
CString num = "";
BOOL flag = FALSE;
var = m_pRecordsetP->GetCollect("PowerNum");
strNum = (LPCSTR)_bstr_t(var);
m_pRecordsetP->MoveNext();
if(m_pRecordsetP->adoEOF)
{
num += TCHAR(strNum[0] + 1);
flag = TRUE;
}
while(!m_pRecordsetP->adoEOF)
{
var = m_pRecordsetP->GetCollect("PowerNum");
CString tempNum = (LPCSTR)_bstr_t(var);
if(strNum[0] < tempNum[0])
strNum = tempNum;
m_pRecordsetP->MoveNext();
}
if(flag == FALSE)
num += TCHAR(strNum[0] + 1);
m_pRecordsetP->AddNew();
m_pRecordsetP->PutCollect("PowerNum", _variant_t(num));
m_pRecordsetP->PutCollect("PowerName", _variant_t(strValue));
m_pRecordsetM->AddNew();
m_pRecordsetM->PutCollect ("PowerName", _variant_t(strValue));
m_pRecordsetM->PutCollect ("ManagerPowNum",_variant_t(""));
m_pRecordsetM->MoveFirst();
CString ManagerPowNum;
while(!m_pRecordsetM->adoEOF)
{
var = m_pRecordsetM->GetCollect ("PowerName");
CString PowerName = (LPCSTR)_bstr_t(var);
if(PowerName == _T("超级用户") || PowerName == _T("系统管理员"))
{
var = m_pRecordsetM->GetCollect ("ManagerPowNum");
ManagerPowNum = "";
ManagerPowNum = (LPCSTR)_bstr_t(var);
ManagerPowNum += num[0];
m_pRecordsetM->PutCollect ("ManagerPowNum", _variant_t(ManagerPowNum));
m_pRecordsetM->Update();
}
m_pRecordsetM->MoveNext();
}
m_pRecordsetMM->AddNew();
m_pRecordsetMM->PutCollect ("PowerName", _variant_t(strValue));
m_pRecordsetMM->PutCollect ("ManagerModuleNum", _variant_t(""));
m_pRecordsetMM->Update();
m_pRecordsetP->Update();
m_PowerNameList.InsertString(-1,strValue);
pEdit->SetWindowText(_T(""));
}
m_pRecordsetM->Close();
m_pRecordsetP->Close();
m_pRecordsetMM->Close();
m_pRecordsetM = NULL;
m_pRecordsetP = NULL;
m_pRecordsetMM = NULL;
}
m_FlagAdd = FALSE;
CButton *pButton = (CButton*)GetDlgItem (IDC_BUTTON_MODIFP);
pButton->EnableWindow(TRUE);
pButton = (CButton*)GetDlgItem (IDC_BUTTON_DELPOW);
pButton->EnableWindow(TRUE);
ControlCanNotUse();
}
}
void CPowSetDialog::OnBnClickedButtonModifp()
{
m_FlagModify = TRUE;
CButton *pButton = (CButton*)GetDlgItem (IDC_BUTTON_ADDPOW);
pButton->EnableWindow(FALSE);
pButton = (CButton*)GetDlgItem (IDC_BUTTON_DELPOW);
pButton->EnableWindow(FALSE);
pButton = (CButton*)GetDlgItem (IDOK);
pButton->EnableWindow(FALSE);
pButton = (CButton*)GetDlgItem (IDC_CANCEL);
pButton->EnableWindow(TRUE);
ControlCanUse();
}
void CPowSetDialog::OnBnClickedCancel()
{
if(m_FlagAdd)
{
ControlCanNotUse();
m_FlagAdd = FALSE;
CButton *pButton = (CButton*)GetDlgItem (IDC_BUTTON_MODIFP);
pButton->EnableWindow(TRUE);
pButton = (CButton*)GetDlgItem (IDC_BUTTON_DELPOW);
pButton->EnableWindow(TRUE);
pButton = (CButton*)GetDlgItem (IDOK);
pButton->EnableWindow(FALSE);
pButton = (CButton*)GetDlgItem (IDC_CANCEL);
pButton->EnableWindow(FALSE);
}
if(m_FlagModify)
{
ControlCanNotUse();
m_FlagModify = FALSE;
CButton *pButton = (CButton*)GetDlgItem (IDC_BUTTON_ADDPOW);
pButton->EnableWindow(TRUE);
pButton = (CButton*)GetDlgItem (IDC_BUTTON_DELPOW);
pButton->EnableWindow(TRUE);
pButton = (CButton*)GetDlgItem (IDOK);
pButton->EnableWindow(FALSE);
pButton = (CButton*)GetDlgItem (IDC_CANCEL);
pButton->EnableWindow(FALSE);
}
}
void CPowSetDialog::OnBnClickedButtonDelpow()
{
int sel = m_PowerNameList.GetCurSel();
if(sel == LB_ERR)
{
AfxMessageBox(_T("请先选择要删除的权限!"));
return;
}
CString tempPowerName;
m_PowerNameList.GetText(sel, tempPowerName);
CString string;
if(tempPowerName == _T("系统管理员"))
{
MessageBox(_T("系统管理员是系统权限,不能删除!"), _T("权限设置"), MB_ICONERROR);
return;
}
string.Format ("真的要删除权限 %s 吗?", tempPowerName);
if(MessageBox (string,_T("权限设置"),MB_OK | MB_ICONERROR) == IDOK)
{
if(DeletePowName(tempPowerName))
m_PowerNameList.DeleteString(sel);
}
}
BOOL CPowSetDialog::DeletePowName(CString PowerName)
{
_RecordsetPtr m_pRecordsetPN;
_RecordsetPtr m_pRecordsetPM;
_RecordsetPtr m_pRecordsetMM;
_RecordsetPtr m_pRecordsetL;
HRESULT hrPN;
HRESULT hrPM;
HRESULT hrMM;
HRESULT hrL;
try
{
hrPN = m_pRecordsetPN.CreateInstance(__uuidof(Recordset));
hrPM = m_pRecordsetPM.CreateInstance(__uuidof(Recordset));
hrMM = m_pRecordsetMM.CreateInstance(__uuidof(Recordset));
hrL = m_pRecordsetL.CreateInstance(__uuidof(Recordset));
if(FAILED(hrPN) || FAILED(hrPM) || FAILED(hrMM) || FAILED(hrL))
{
AfxMessageBox(_T("创建实例失败!"));
return FALSE;
}
CString strSQL = "select * from PowerNameTable";
m_pRecordsetPN->Open (_bstr_t(strSQL),
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
m_pRecordsetMM->Open ("select * from PowerManagerTable",
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
strSQL.Format("select * from LoginTable where PowerName = '%s'", PowerName);
m_pRecordsetL->Open (_bstr_t(strSQL),
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(!m_pRecordsetL->adoEOF)
{
AfxMessageBox(_T("有用户使用改权限,请更改这些用户的权限再删除"));
m_pRecordsetL->Close();
m_pRecordsetL = NULL;
m_pRecordsetMM->Close();
m_pRecordsetPN->Close();
m_pRecordsetMM = NULL;
m_pRecordsetPN = NULL;
return FALSE;
}
_variant_t RecordsetsAffected;
_variant_t var;
CString strValue;
if(!m_pRecordsetPN->adoEOF)
{
CString strValue;
CString PowerNum;
while(!m_pRecordsetPN->adoEOF)
{
var = m_pRecordsetPN->GetCollect("PowerName");
strValue = (LPCSTR)_bstr_t(var);
var = m_pRecordsetPN->GetCollect("PowerNum");
PowerNum = (LPCSTR)_bstr_t(var);
if(PowerName == strValue)
break;
m_pRecordsetPN->MoveNext();
}
strSQL.Format ("delete from PowerNameTable where PowerName = '%s'", PowerName);
m_pConnection->BeginTrans();
m_pConnection->Execute (_bstr_t(strSQL), &RecordsetsAffected, adCmdText);
strSQL.Format("delete from PowerManagerTable where PowerName = '%s'", PowerName);
m_pConnection->Execute (_bstr_t(strSQL), &RecordsetsAffected, adCmdText);
strSQL.Format("delete from ManagerModuleTable where PowerName = '%s'", PowerName);
m_pConnection->Execute (_bstr_t(strSQL), &RecordsetsAffected, adCmdText);
m_pRecordsetPM->Open ("select * from PowerManagerTable",
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
CString AllManagerNum;
while(!m_pRecordsetPM->adoEOF)
{
var = m_pRecordsetPM->GetCollect ("ManagerPowNum");
AllManagerNum = (LPCSTR)_bstr_t(var);
int Count = AllManagerNum.GetLength();
CString newManagerNum = "";
for(int index = 0; index < Count; index++)
{
if(AllManagerNum[index] != PowerNum[0])
newManagerNum += AllManagerNum[index];
}
if(newManagerNum != "")
{
m_pRecordsetPM->PutCollect ("ManagerPowNum", _variant_t(newManagerNum));
m_pRecordsetPM->Update();
}
m_pRecordsetPM->MoveNext();
}
m_pRecordsetPM->MoveFirst();
AllManagerNum = "";
while(!m_pRecordsetPM->adoEOF)
{
var = m_pRecordsetPM->GetCollect ("ManagerPowNum");
AllManagerNum = (LPCSTR)_bstr_t(var);
int Count = AllManagerNum.GetLength();
CString newAllManagerNum = "";
for(int index = 0; index < Count; index++)
{
if(AllManagerNum[index] > PowerNum[0])
newAllManagerNum += TCHAR (AllManagerNum[index] -1);
else
newAllManagerNum += AllManagerNum[index];
}
m_pRecordsetPM->PutCollect("ManagerPowNum", _variant_t (newAllManagerNum));
m_pRecordsetPM->Update();
m_pRecordsetPM->MoveNext();
}//while
m_pRecordsetPN->MoveFirst();
while(!m_pRecordsetPN->adoEOF)
{
var = m_pRecordsetPN->GetCollect ("PowerNum");
CString tempPowerNum = (LPCSTR)_bstr_t(var);
if(tempPowerNum[0] > PowerNum[0])
{
CString num = "";
num += TCHAR (tempPowerNum[0] - 1);
m_pRecordsetPN->PutCollect ("PowerNum", _variant_t(num));
m_pRecordsetPN->Update();
}
m_pRecordsetPN->MoveNext();
}//while
}//if
m_pConnection->CommitTrans();
m_pRecordsetPM->Close();
m_pRecordsetPN->Close();
m_pRecordsetPM = NULL;
m_pRecordsetPN = NULL;
int Count = m_ManagerPowNotYetList.GetCount();
for(int index = 0; index < Count; index++)
m_ManagerPowNotYetList.DeleteString(0);
Count = m_ManagerPowNameList.GetCount();
for(int index = 0; index< Count; index++)
m_ManagerPowNameList.DeleteString(0);
Count = m_ListModule.GetCount();
for(int index = 0; index < Count; index++)
m_ListModule.DeleteString(0);
Count = m_ListNoModule.GetCount();
for(int index = 0; index < Count; index++)
m_ListNoModule.DeleteString(0);
return TRUE;
}
catch(_com_error &e)
{
AfxMessageBox(e.ErrorMessage());
return FALSE;
}
return FALSE;
}
void CPowSetDialog::OnBnClickedUserGive()
{
int selPN = m_PowerNameList.GetCurSel();
if(selPN == LB_ERR)
{
AfxMessageBox (_T("没有选择用户权限!"));
return;
}
if(m_ManagerPowNotYetList.GetCount() == 0)
{
AfxMessageBox (_T("已经赋予该用户所有的管理权限!"));
return;
}
int selPNY = m_ManagerPowNotYetList.GetCurSel();
if(selPNY == LB_ERR)
{
AfxMessageBox (_T("没有选择未管理用户权限!"));
return;
}
CString PowerNamePN, PowerNamePNY;
m_PowerNameList.GetText (selPN, PowerNamePN);
m_ManagerPowNotYetList.GetText (selPNY, PowerNamePNY);
CString str;
str.Format ("真是要赋予%s%s的管理权限吗?", PowerNamePN, PowerNamePNY);
if(AfxMessageBox (str, MB_OKCANCEL) == IDOK)
{
HRESULT hrPN;
_RecordsetPtr m_pRecordsetPN;
try{
m_ManagerPowNotYetList.DeleteString (selPNY);
hrPN = m_pRecordsetPN.CreateInstance (__uuidof(Recordset));
if(FAILED(hrPN))
{
AfxMessageBox (_T("创建实例失败!"));
return;
}
CString strSQL;
strSQL.Format("select * from PowerNameTable where PowerName = '%s'", PowerNamePNY);
hrPN = m_pRecordsetPN->Open (_bstr_t(strSQL),
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(SUCCEEDED(hrPN))
{
if(!m_pRecordsetPN->adoEOF)
{
CString PowerNum;
_variant_t var;
var = m_pRecordsetPN->GetCollect ("PowerNum");
PowerNum = (LPCSTR)_bstr_t(var);
_RecordsetPtr m_pRecordsetPM;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -