#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::iterator iter = m_UserVec.begin(); vector::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; for(int i=0;i::iterator iter = m_UserVec.begin(); vector::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::iterator iter = m_UserVec.begin(); vector::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;iWriteDebugLog("Func--->ReadAuthorityParaFile"); CFileMgr FileMgr; CString FilePath; FileMgr.GetFullFilePath(FilePath,AUTHORITY_PARA_FILE);//结果文件路径 vector> StrVec; FileMgr.ReadFileToStrVec(FilePath,StrVec); m_AuthorityParVec.clear(); int size = StrVec.size(); for(int k=0;kWriteDebugLog("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;kGetLaipuLaserDataDir(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