You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

581 lines
18 KiB
C++

#include "StdAfx.h"
#include "AuthorityMgr.h"
#include "MsgBox.h"
#include "FileMgr.h"
#include "LogMgr.h"
#include "PropertieMgr.h"
#include "Propertie.h"
#include "CStringFuc.h"
#include "WorkTime.h"
#include "ProgramLaserTuiHuo.h"
#define LAIPU_NAME "laipu"
#define LAIPU_PASSWORD "laipu123"
#define FILE_PATH _T("\\Parameter\\Authority\\Authority.par")
#define AUTHORITY_PARA_FILE _T("\\CommPar\\Authority.bin")
#define NEW_LOGIN_FILE_PATH _T("\\LoginRecord\\")
bool CompareUser(CAccount Account1,CAccount Account2)
{
return Account1.m_Authority>Account2.m_Authority;
}
CAuthorityMgr *gAuthorityMgr = new CAuthorityMgr;
CAuthorityMgr::CAuthorityMgr(void)
{
m_AutoFactory = false;//打开软件自动升级到管理员权限
m_CurAuthority = _Authority_NoLogin;//当前权限
m_CurUserName = ACCOUNT_NoLogin;
m_bLoginDlgOpen = false;//是否正在打开登录对话框
}
CAuthorityMgr::~CAuthorityMgr(void)
{
}
CMFCPropertyGridProperty *CAuthorityMgr::CreatGridProperty()
{
CString PropertyName;//属性名称
CString Description;//描述
CString Path = _T("AuthorityMgr");//存储路径
CString Name;
CString GroupName;
CString ModuleName;
//-------------------------------------------------------------------------------//
PropertyName = _T("程序权限");
GroupName = PropertyName;
CMFCPropertyGridProperty* pGroup = new CMFCPropertyGridProperty(PropertyName);
//-------------------------------------------------------------------------------//
if(gAuthorityMgr->CheckAuthority(_Authority_Factory))
{
//添加属性变量映射
Name = _T("m_AutoFactory");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_AutoFactory);
pPropertie->SetType(_PROP_TYPE_BOOL);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("厂家权限");
Description = _T("打开软件时自动获得厂家权限,调试用");
pPropertie->SetGroupName(GroupName);
pPropertie->SetShowName(PropertyName);
CMFCPropertyGridProperty* p = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_AutoFactory, Description);
pGroup->AddSubItem(p);
gDevicePropertieMgr.Insert(p, pPropertie);
}
//-------------------------------------------------------------------------------//
return pGroup;
}
void CAuthorityMgr::OnAppInitialize()
{
SaveOrLoad(false);//读取文件
if(m_AutoFactory)
m_CurAuthority = _Authority_Factory;
//读取权限参数文件
ReadAuthorityParaFile();
}
//检查当前权限
bool CAuthorityMgr::CheckAuthority(eAuthorityType authority,bool bShowMsgbox)
{
if((int)m_CurAuthority >= (int)authority)
{
return true;
}
else
{
if(bShowMsgbox)
{
CMsgBox MsgBox;
CString str = "需要["+GetAccountName(authority) + "]权限!";
MsgBox.Show(str);
}
return false;
}
}
//是否登录
bool CAuthorityMgr::IsLogin()
{
return m_CurAuthority != _Authority_NoLogin;
}
//锁定用户,权限变成未登录
void CAuthorityMgr::Lock()
{
//记录
{
CUserCtrlPar UserCtrlPar;
UserCtrlPar.m_UserName = m_CurUserName;
UserCtrlPar.m_UserCtrlType = _UserCtrlType_logout;
UserCtrlPar.m_AuthorityType = m_CurAuthority;
SaveUserCtrlHistory(UserCtrlPar);
}
SetAuthority(_Authority_NoLogin);
m_CurUserName = ACCOUNT_NoLogin;
}
//登录
bool CAuthorityMgr::Login(CString name,CString PassWord)
{
//读取账号信息
SaveOrLoad(false);
//读取权限参数文件
ReadAuthorityParaFile();
bool ret = false;
eAuthorityType authority = GetCurAuthority();
if(name == LAIPU_NAME && PassWord == LAIPU_PASSWORD)
{
ret = true;
authority = _Authority_Factory;
}
else
{
vector<CAccount>::iterator iter = m_UserVec.begin();
vector<CAccount>::iterator iter_end = m_UserVec.end();
for(;iter!=iter_end;iter++)
{
if((*iter).m_Name == name)
{
if((*iter).m_PassWord == PassWord)
{
ret = true;
authority = (*iter).m_Authority;
break;
}
else
{
CMsgBox MsgBox;
MsgBox.Show("密码和账号不匹配");
break;
}
}
}
if(iter==iter_end)
{
CMsgBox MsgBox;
MsgBox.Show("账号或密码错误!");
}
}
if(ret)
{
SetAuthority(authority);
m_CurUserName = name;
CString log = "CAuthorityMgr::Login::";
log += GetAccountName(authority);
log += "->"+name;
gLogMgr->WriteDebugLog(log);
//记录
{
CUserCtrlPar UserCtrlPar;
UserCtrlPar.m_UserName = name;
UserCtrlPar.m_UserCtrlType = _UserCtrlType_login;
UserCtrlPar.m_AuthorityType = authority;
SaveUserCtrlHistory(UserCtrlPar);
}
}
return ret;
}
CString CAuthorityMgr::GetAccountName(eAuthorityType authority)
{
CString s;
switch(authority)
{
case _Authority_NoLogin:
s = ACCOUNT_NoLogin;
break;
case _Authority_Operator:
s = ACCOUNT_Operator;
break;
case _Authority_OperatorAdmin:
s = ACCOUNT_OperatorAdmin;
break;
case _Authority_Engineer:
s = ACCOUNT_Engineer;
break;
case _Authority_Technics:
s = ACCOUNT_Technics;
break;
case _Authority_Factory:
s = ACCOUNT_Factory;
break;
default:
break;
}
return s;
}
//当前账户类型
CString CAuthorityMgr::GetCurAccountName()
{
return GetAccountName(GetCurAuthority());
}
//保存或者读取账户信息
void CAuthorityMgr::SaveOrLoad(bool bSave)
{
CFileMgr FileMgr;
char filepath[1024];
FileMgr.GetFullFilePath(filepath,FILE_PATH);//获取完整路径
if(bSave)
{
CFile file(filepath,CFile::modeReadWrite|CFile::modeCreate);
CArchive ar(&file,CArchive::store);
SaveOrLoadExt(ar);
}
else
{
CFile file;
if(file.Open(filepath,CFile::modeRead))
{
CArchive ar(&file,CArchive::load);
SaveOrLoadExt(ar);
}
else
{
gLogMgr->WriteDebugLog("Authority File Read Error");
}
}
}
void CAuthorityMgr::SaveOrLoadExt(CArchive &ar)
{
int size = 0;
if(ar.IsStoring())
{
size = m_UserVec.size();//Account 的数量
ar<<size;
}
else
{
m_UserVec.clear();
ar>>size;
for(int i=0;i<size;i++)
{
CAccount Account;
m_UserVec.push_back(Account);
}
}
for(int i=0;i<size;i++)
{
m_UserVec[i].Serialize(ar);
}
if(!ar.IsStoring())
{
//重新排序
SortUserVec();
}
}
void CAuthorityMgr::AddAccount(CAccount Account)
{
//检查是否有重复的用户名
vector<CAccount>::iterator iter = m_UserVec.begin();
vector<CAccount>::iterator iter_end = m_UserVec.end();
for(;iter!=iter_end;iter++)
{
if((*iter).m_Name == Account.m_Name)
{
CMsgBox MsgBox;
CString str(_T("用户:"));
str += Account.m_Name + "已存在!";
MsgBox.Show(str);
return;
}
}
m_UserVec.push_back(Account);
//重新排序
SortUserVec();
SaveOrLoad(true);
//记录
{
CUserCtrlPar UserCtrlPar;
UserCtrlPar.m_UserName = Account.m_Name;
UserCtrlPar.m_UserCtrlType = _UserCtrlType_AddUser;
UserCtrlPar.m_AuthorityType = Account.m_Authority;
SaveUserCtrlHistory(UserCtrlPar);
}
CMsgBox MsgBox;
MsgBox.Show("创建成功!");
}
//删除指定的用户
void CAuthorityMgr::DelUser(int idx)
{
//Engineer 才能删除用户
if(gAuthorityMgr->CheckAuthority(_Authority_Engineer,true)==false)
{
return;
}
vector<CAccount>::iterator iter = m_UserVec.begin();
vector<CAccount>::iterator iter_end = m_UserVec.end();
for(int i=0;iter!=iter_end;iter++,i++)
{
if(i==idx)
{
CAccount Account = (*iter);
CMsgBox MsgBox;
if((*iter).m_Authority>m_CurAuthority)
{
MsgBox.Show(_T("不能删除更高级的用户!"));
return;
}
CString str(_T("删除"));
str += (*iter).m_Name + "?";
if(MsgBox.ConfirmOkCancel(str))
{
m_UserVec.erase(iter);
SortUserVec();
SaveOrLoad(true);
//记录
{
CUserCtrlPar UserCtrlPar;
UserCtrlPar.m_UserName = Account.m_Name;
UserCtrlPar.m_UserCtrlType = _UserCtrlType_DelUser;
UserCtrlPar.m_AuthorityType = Account.m_Authority;
SaveUserCtrlHistory(UserCtrlPar);
}
}
return;
}
}
}
void CAuthorityMgr::InsertToList(CListCtrl &List)
{
int size = m_UserVec.size();
for(int i=0;i<size;i++)
{
int idx = 0;
List.InsertItem(i," ");//插入一行
List.SetItemText(i,idx++,GetAccountName(m_UserVec[i].m_Authority));//权限类型
List.SetItemText(i,idx++,(m_UserVec[i].m_Name));//用户名
}
}
//重新排序
void CAuthorityMgr::SortUserVec()
{
sort(m_UserVec.begin(),m_UserVec.end(),CompareUser);
}
#if 1
//读取权限参数文件
void CAuthorityMgr::ReadAuthorityParaFile()
{
gLogMgr->WriteDebugLog("Func--->ReadAuthorityParaFile");
CFileMgr FileMgr;
CString FilePath;
FileMgr.GetFullFilePath(FilePath,AUTHORITY_PARA_FILE);//结果文件路径
vector<vector<CString>> StrVec;
FileMgr.ReadFileToStrVec(FilePath,StrVec);
m_AuthorityParVec.clear();
int size = StrVec.size();
for(int k=0;k<size;k++)
{
if(StrVec[k].size() == 6)
{
CAuthorityPar AuthorityPar;
AuthorityPar.m_AuthorityName = StrVec[k][0];
AuthorityPar.m_ShowName = StrVec[k][1];
AuthorityPar.m_bOperator = (StrVec[k][2]=="1")?true:false;
AuthorityPar.m_bOperatorAdmin = (StrVec[k][3]=="1")?true:false;
AuthorityPar.m_bEngineer = (StrVec[k][4]=="1")?true:false;
AuthorityPar.m_bTechnics = (StrVec[k][5]=="1")?true:false;
m_AuthorityParVec.push_back(AuthorityPar);
}
}
}
void CAuthorityMgr::SaveAuthorityParaFile()
{
gLogMgr->WriteDebugLog("Func--->SaveAuthorityParaFile");
CFileMgr FileMgr;
CString FilePath;
FileMgr.GetFullFilePath(FilePath,AUTHORITY_PARA_FILE);//结果文件路径
m_AuthorityParVec = m_TmpAuthorityParVec;
ofstream FileStream;
FileStream.open(FilePath);
int size = m_AuthorityParVec.size();
for(int k=0;k<size;k++)
{
CAuthorityPar &AuthorityPar = m_AuthorityParVec[k];
FileStream<<AuthorityPar.m_AuthorityName<<",";
FileStream<<AuthorityPar.m_ShowName<<",";
FileStream<<Bool2CString(AuthorityPar.m_bOperator)<<",";
FileStream<<Bool2CString(AuthorityPar.m_bOperatorAdmin)<<",";
FileStream<<Bool2CString(AuthorityPar.m_bEngineer)<<",";
FileStream<<Bool2CString(AuthorityPar.m_bTechnics)<<"\n";
}
}
bool CAuthorityMgr::CheckAuthorityByName(CString AuthorityName,bool bShowMsgbox)
{
bool Ret = false;
CString ShowName;
int size = m_AuthorityParVec.size();
for(int k=0;k<size;k++)
{
CAuthorityPar &AuthorityPar = m_AuthorityParVec[k];
if(AuthorityName == AuthorityPar.m_AuthorityName)
{
if(m_CurAuthority==_Authority_Operator && AuthorityPar.m_bOperator)
Ret = true;
if(m_CurAuthority==_Authority_OperatorAdmin && AuthorityPar.m_bOperatorAdmin)
Ret = true;
if(m_CurAuthority==_Authority_Engineer && AuthorityPar.m_bEngineer)
Ret = true;
if(m_CurAuthority==_Authority_Technics && AuthorityPar.m_bTechnics)
Ret = true;
if(m_CurAuthority==_Authority_Factory)
Ret = true;
ShowName = AuthorityPar.m_ShowName;
break;
}
}
if(!Ret && bShowMsgbox)
{
CMsgBox MsgBox;
CString str = ShowName + " : 权限不足!";
MsgBox.Show(str);
}
return Ret;
}
void CAuthorityMgr::InitAuthorityTypeComb(CComboBox &Combo)
{
int idx = 0;
Combo.InsertString(idx++,GetAccountName(_Authority_Operator));
Combo.InsertString(idx++,GetAccountName(_Authority_OperatorAdmin));
Combo.InsertString(idx++,GetAccountName(_Authority_Engineer));
Combo.InsertString(idx++,GetAccountName(_Authority_Technics));
Combo.SetCurSel(0);
}
void CAuthorityMgr::InitAuthorityParList(CListCtrl &List)
{
//设置风格
List.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_CHECKBOXES);
//设置列
int idx = 0;
List.InsertColumn(idx,"",LVCFMT_LEFT,25);//checkbox
idx++;
List.InsertColumn(idx,"",LVCFMT_LEFT,200,-1);
idx++;
int size = m_AuthorityParVec.size();
for(int i=0;i<size;i++)
{
CAuthorityPar &AuthorityPar = m_AuthorityParVec[i];
int idx = 0;
List.InsertItem(i," ");//插入一行
List.SetItemText(i,idx++,"");//check box
List.SetItemText(i,idx++,AuthorityPar.m_ShowName);//层号
}
//当前操作临时容器
m_TmpAuthorityParVec = m_AuthorityParVec;
UpdateAuthorityParList(List,0);
}
void CAuthorityMgr::UpdateAuthorityParList(CListCtrl &List,int AuthorityType)
{
for(int i=0;i<List.GetItemCount();i++)
{
CAuthorityPar &AuthorityPar = m_TmpAuthorityParVec[i];
if(AuthorityType==0)
List.SetCheck(i,AuthorityPar.m_bOperator);
if(AuthorityType==1)
List.SetCheck(i,AuthorityPar.m_bOperatorAdmin);
if(AuthorityType==2)
List.SetCheck(i,AuthorityPar.m_bEngineer);
if(AuthorityType==3)
List.SetCheck(i,AuthorityPar.m_bTechnics);
}
}
void CAuthorityMgr::UpdateAuthorityPar(CListCtrl &List,int AuthorityType)
{
for(int i=0;i<List.GetItemCount();i++)
{
CAuthorityPar &AuthorityPar = m_TmpAuthorityParVec[i];
if(AuthorityType==0)
AuthorityPar.m_bOperator = List.GetCheck(i);
if(AuthorityType==1)
AuthorityPar.m_bOperatorAdmin = List.GetCheck(i);
if(AuthorityType==2)
AuthorityPar.m_bEngineer = List.GetCheck(i);
if(AuthorityType==3)
AuthorityPar.m_bTechnics = List.GetCheck(i);
}
}
#endif
#if 1
//记录当前账户操作记录
void CAuthorityMgr::SaveUserCtrlHistory(CUserCtrlPar UserCtrlPar)
{
if(UserCtrlPar.m_AuthorityType==_Authority_NoLogin)
return;
if(UserCtrlPar.m_UserName==ACCOUNT_NoLogin)
return;
CWorkTime WorkTime;
CString CurDateTime = WorkTime.GetDateTime("/",":");
CCsvData CsvData;
CsvData.AddData("时间",false);
CsvData.AddData(CurDateTime,true);
CsvData.AddData("操作类型",false);
switch(UserCtrlPar.m_UserCtrlType)
{
case _UserCtrlType_AddUser:
{
CsvData.AddData("Add",true);
}
break;
case _UserCtrlType_DelUser:
{
CsvData.AddData("Delete",true);
}
break;
case _UserCtrlType_login:
{
CsvData.AddData("Login",true);
}
break;
case _UserCtrlType_logout:
{
CsvData.AddData("Logout",true);
}
break;
default:
break;
}
CsvData.AddData("用户名",false);
CsvData.AddData(UserCtrlPar.m_UserName,true);
CsvData.AddData("权限",false);
CsvData.AddData(GetAccountName(UserCtrlPar.m_AuthorityType),true);
CsvData.AddData("",false);
CsvData.AddData("",true);//空行
CFileMgr FileMgr;
CString DataPath;
CurDateTime = WorkTime.GetCurDate("_");
CString DirPath = gProgramLaserTuiHuo->GetLaipuLaserDataDir(NEW_LOGIN_FILE_PATH);
DataPath += DirPath;
DataPath += WorkTime.GetCurYear();
DataPath += "\\"+WorkTime.GetCurMonth();
//DataPath += "\\"+WorkTime.GetCurDay();
DataPath += "\\";
CString DataName;
DataName += "LaipuDrawing_";
DataName += CurDateTime;
//创建数据目录
FileMgr.CreatDir(DataPath);
FileMgr.WriteDataToExcel(DataPath,DataName,CsvData,true);
}
#endif