#include "StdAfx.h" #include "AuthorityMgr.h" #include "MsgBox.h" #include "FileMgr.h" #include "LogMgr.h" #include "PropertieMgr.h" #include "Propertie.h" #define LAIPU_NAME "laipu" #define LAIPU_PASSWORD "laipu" #define FILE_PATH _T("\\Parameter\\Authority\\Authority.par") bool CompareUser(CAccount Account1,CAccount Account2) { return Account1.m_Authority>Account2.m_Authority; } CAuthorityMgr *gAuthorityMgr = new CAuthorityMgr; CAuthorityMgr::CAuthorityMgr(void) { m_CurAuthority = _USER;//当前权限 m_AutoFactory = false;//打开软件自动升级到管理员权限 } CAuthorityMgr::~CAuthorityMgr(void) { } CMFCPropertyGridProperty *CAuthorityMgr::CreatGridProperty() { CString PropertyName;//属性名称 CString Description;//描述 CString Path = _T("AuthorityMgr");//存储路径 CString Name; //-------------------------------------------------------------------------------// PropertyName = _T("程序权限"); CMFCPropertyGridProperty* pGroup = new CMFCPropertyGridProperty(PropertyName); //-------------------------------------------------------------------------------// if(gAuthorityMgr->CheckAuthority(_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("打开软件时自动获得厂家权限,调试用"); CMFCPropertyGridProperty* p = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_AutoFactory, Description); pGroup->AddSubItem(p); gDevicePropertieMgr.Insert(p, pPropertie); } //-------------------------------------------------------------------------------// return pGroup; } void CAuthorityMgr::Ini() { SaveOrLoad(false);//读取文件 if(m_AutoFactory) m_CurAuthority = _FACTORY; } //检查当前权限 bool CAuthorityMgr::CheckAuthority(AUTHORITY_TYPE authority,bool bShowMsgbox) { if((int)m_CurAuthority >= (int)authority) { return true; } else { if(bShowMsgbox) { CMsgBox MsgBox; CString str = "需要["+GetAccountName(authority) + "]权限!"; MsgBox.Show(str, true); } return false; } } //是否登录 bool CAuthorityMgr::IsLogin() { return m_CurAuthority != _NO_LOGIN; } //锁定用户,权限变成未登录 void CAuthorityMgr::Lock() { SetAuthority(_NO_LOGIN); m_CurUserName = "空"; } //登录 bool CAuthorityMgr::Login(CString name,CString PassWord) { bool ret = false; AUTHORITY_TYPE authority = GetCurAuthority(); if(name == LAIPU_NAME && PassWord == LAIPU_PASSWORD) { ret = true; 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; CMsgBox MsgBox; CString str("登录成功---->"); str += ("[Type:") + GetCurAccountName() + "]"; str += ("[Name:") + m_CurUserName + "]"; MsgBox.Show(str); } return ret; } CString CAuthorityMgr::GetAccountName(AUTHORITY_TYPE authority) { if(authority==_FACTORY) return ACCOUNT_FACTORY; if(authority==_ADMIN) return ACCOUNT_ADMIN; if(authority==_USER) return ACCOUNT_USER; return ACCOUNT_NO_LOGIN; } //当前账户类型 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("权限文件读取失败", _LOG_ERROR); } } } void CAuthorityMgr::SaveOrLoadExt(CArchive &ar) { int size = 0; if(ar.IsStoring()) { size = m_UserVec.size();//area 的数量 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); CMsgBox MsgBox; MsgBox.Show("创建成功!"); } //删除指定的用户 void CAuthorityMgr::DelUser(int idx) { if(gAuthorityMgr->CheckAuthority(_ADMIN,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) { 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); } return; } } } void CAuthorityMgr::InsertToList(CListCtrl &List) { int size = m_UserVec.size(); for(int i=0;i