⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usermanage.cpp

📁 Visual C++ 入门、进阶、与应用 如何使用链表末尾追加节点实例 可学习操作链表
💻 CPP
字号:
// UserManage.cpp: implementation of the CUserManage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Ex080201.h"
#include "UserManage.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CUser::CUser(CUserSet &rs)
{
	m_uID			= rs.m_ID ;
	m_uPopedom		= (USER_TYPE)rs.m_Popedom ;
	m_strUserName	= rs.m_UserName ;
	m_strPassWord	= rs.m_Password ;
	m_strComment	= rs.m_Comment ;
}

CUser::CUser(const CUser &other)
{
	m_uID			= other.m_uID ;
	m_strUserName	= other.m_strUserName ;
	m_strPassWord	= other.m_strPassWord ;
	m_uPopedom		= other.m_uPopedom ;
	m_strComment	= other.m_strComment ;
}

CUserManage::CUserManage()
{
	m_pLoginUser = NULL ;

	m_nMaxID = - 1 ;
	CUserSet rs ;
	rs.Open();
	while(!rs.IsEOF())
	{
		CUser * pUser = new CUser(rs); 
		m_userInfos[pUser->m_uID] = pUser ;
		if( (int)pUser->m_uID > m_nMaxID )
			m_nMaxID = pUser->m_uID ;
		rs.MoveNext();
	}
	rs.Close();
}

CUserManage::~CUserManage()
{
	POSITION pos = m_userInfos.GetStartPosition() ;
	while(NULL != pos )
	{
		UINT uID ;
		CUser * pUser = NULL ;
		m_userInfos.GetNextAssoc(pos,uID,pUser);
		delete pUser ;
	}
	m_userInfos.RemoveAll() ;
}

bool CUserManage::Login(CString strUserName, CString strPassword)
{
	POSITION pos = m_userInfos.GetStartPosition() ;
	while(NULL != pos )
	{
		UINT uID ;
		CUser * pUser = NULL ;
		m_userInfos.GetNextAssoc(pos,uID,pUser);
		if(NULL == pUser)
			continue ;
		if(strUserName == pUser->m_strUserName && strPassword == pUser->m_strPassWord)
		{
			m_pLoginUser = pUser ;
			return true ;
		}
	}

	return false ;
}

UINT CUserManage::GetNewID()
{
	return ++m_nMaxID ;
}

UINT CUserManage::AddUser(CString strName, CString strPassword, UINT uPopedom, CString strComment)
{
	//检查是否用重复的用户名
	for(POSITION pos = m_userInfos.GetStartPosition() ; NULL != pos ; )
	{
		UINT uID ;
		CUser * pUser ;
		m_userInfos.GetNextAssoc(pos,uID,pUser);
		if(pUser->m_strUserName == strName)
			return -1 ;//不同的用户的用户名不能相同
	}
	
	CUser * pUser = new CUser ;
	if(NULL == pUser)
		return -1 ;

	pUser->m_uID = GetNewID();
	pUser->m_strUserName = strName;
	pUser->m_strPassWord = strPassword ;
	pUser->m_uPopedom = (CUser::USER_TYPE)uPopedom ;
	pUser->m_strComment = strComment ;
	if(!IsHasManagePopedom(pUser))
	{
		delete pUser ;
		return -1 ;
	}

	//存进数据库
	CUserSet rs ;
	rs.AddUser(*pUser);
	m_userInfos[pUser->m_uID] = pUser ;
	
	return pUser->m_uID ;
}

bool CUserManage::EditUser(CUser newUser)
{
	for(POSITION pos = m_userInfos.GetStartPosition() ; NULL != pos ; )
	{
		UINT uID ;
		CUser * pUser ;
		m_userInfos.GetNextAssoc(pos,uID,pUser);
		if((uID != newUser.m_uID) && (pUser->m_strUserName == newUser.m_strUserName))
			return false ;//不同的用户的用户名不能相同
	}
	
	CUser * pUser = m_userInfos[newUser.m_uID] ;
	if(NULL == pUser)
		return false ;//没有找到要修改的用户
	if(!IsHasManagePopedom(pUser))
		return false ;
	if(!IsHasManagePopedom(&newUser))
		return false ;

	CUserSet rs ;
	if(!rs.EditUser(newUser))
	{
		delete pUser ;
		return false ;
	}

	*pUser = newUser ;
	return true ;
}

bool CUserManage::DeleteUser(UINT uID)
{
	CUser * pUser = m_userInfos[uID] ;
	if(!IsHasManagePopedom(pUser))
		return false ;

	CUserSet rs ;
	if(!rs.DeleteUser(uID))
		return false ;
	delete pUser ;
	return m_userInfos.RemoveKey(uID);
}

bool CUserManage::IsHasManagePopedom(CUser *pUser)
{
	if(CUser::GREATE_ADMINISTRATOR == m_pLoginUser->m_uPopedom )
	{
		if(NULL != pUser )
			if(CUser::GREATE_ADMINISTRATOR == pUser->m_uPopedom)
			return false ;
	}
	else if( CUser::ADMINISTRATOR == m_pLoginUser->m_uPopedom)
	{
		if(NULL != pUser )
			if(CUser::GREATE_ADMINISTRATOR == pUser->m_uPopedom || CUser::ADMINISTRATOR == pUser->m_uPopedom)
				return false ;
	}
	else
	{
		return false ;
	}

	return true ;
}

CUser::USER_TYPE CUserManage::GetLogicUserPopedom()
{
	return m_pLoginUser->m_uPopedom ;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -