-
大小: 3KB文件類型: .rar金幣: 2下載: 0 次發(fā)布日期: 2022-10-07
- 語言: C/C++
- 標(biāo)簽: C++??登錄??權(quán)限管理??
資源簡介
1.使用C++實現(xiàn)的權(quán)限管理模塊
2.單例模式實現(xiàn),方便集成
3.實現(xiàn)了root和普通用戶區(qū)別,添加新用戶,修改密碼
4.用戶名密碼以加密文檔形式存在本地文件

代碼片段和文件信息
#include?“stdafx.h“
#include?“AuthorityManagement.h“
namespace?cis
{
//using?singleton
AuthorityManagement*?AuthorityManagement::instance?=?new?AuthorityManagement();
AuthorityManagement*?AuthorityManagement::getInstance()
{
return?instance;
}
AuthorityManagement::AuthorityManagement()
{
m_Key?=?“dasidaiousdadfkjdhIJpqwieTGYU“;
m_isInitialized?=?false;
}
AuthorityManagement::~AuthorityManagement()
{
}
int?AuthorityManagement::Initialization()
{
//打開存放用戶信息的txt
ifstream?readUserInfo;
readUserInfo.open(“UserInfo.txt“);
if?(!readUserInfo.is_open())
{
return?0;
}
//從txt中讀入用戶信息
string?userName;
string?password;
while?(getline(readUserInfo?userName))
{
decrypt(userName);
getline(readUserInfo?password);
decrypt(password);
m_UserInfo[userName]?=?password;
}
//關(guān)閉ifstream
readUserInfo.close();
m_isInitialized?=?true;
return?1;
}
string?AuthorityManagement::getCurrentUser()
{
return?m_CurrentUser;
}
int?AuthorityManagement::logout()
{
m_CurrentUser.clear();
return?1;
}
int?AuthorityManagement::signIn(string?userName?string?password)
{
//必須在權(quán)限管理模塊初始化完成后才能登錄
if?(!m_isInitialized)
{
return?false;
}
//首先檢查是否存在用戶名
if?(m_UserInfo.count(userName))
{
//如果密碼正確就能登錄系統(tǒng)
if?(m_UserInfo[userName]?==?password)
{
m_CurrentUser?=?userName;
return?true;
}
else
{
return?false;
}
}
else
{
return?false;
}
}
int?AuthorityManagement::addUser(string?userName?string?password)
{
//必須在權(quán)限管理模塊初始化完成后才能登錄
if?(!m_isInitialized)
{
return?false;
}
//非管理員沒有權(quán)限添加新用戶
if?(m_CurrentUser!=“root“)
{
return?false;
}
//禁止添加相同用戶名的用戶
if?(m_UserInfo.count(userName))
{
return?false;
}
ofstream?writeUserInfo;
//打開文件以追加方式打開
writeUserInfo.open(“UserInfo.txt“?ios_base::app);
if?(!writeUserInfo.is_open())
{
return?false;
}
string?cipherUserName?=?userName;
encrypt(cipherUserName);
string?cipherPassword?=?password;
encrypt(cipherPassword);
writeUserInfo?< writeUserInfo?< writeUserInfo.close();
m_UserInfo[userName]?=?password;
return?true;
}
int?AuthorityManagement::addRoot(string?password)
{
ofstream?writeUserInfo;
//打開文件以追加方式打開
writeUserInfo.open(“UserInfo.txt“);
if?(!writeUserInfo.is_open())
{
return?false;
}
string?cipherUserName?=?“root“;
encrypt(cipherUserName);
string?cipherPassword?=?password;
encrypt(cipherPassword);
writeUserInfo?< writeUserInfo?< writeUserInfo.close();
m_UserInfo[“root“]?=?password;
return?true;
}
int?AuthorityManagement::modifyPassword(string?password)
{
//必須在權(quán)限管理模塊初始化完成后才能登錄
if?(!m_isInitialized)
{
return?false;
}
//必須在登錄的情況下才能修改密碼
if?(!m_CurrentUser.size())
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????4764??2018-05-25?21:24??testAuthorityManagement\AuthorityManagement.cpp
?????文件???????1183??2018-05-25?20:43??testAuthorityManagement\AuthorityManagement.h
?????文件???????1169??2018-05-25?20:41??testAuthorityManagement\testAuthorityManagement.cpp
?????目錄??????????0??2018-05-25?21:29??testAuthorityManagement
-----------?---------??----------?-----??----
?????????????????7116????????????????????4
評論
共有 條評論