www.pudn.com > ftpserversrc.rar > UserManager.cpp


/****************************************************************/ 
/*																*/ 
/*  UserManager.cpp												*/ 
/*																*/ 
/*  Implementation of the CUserManager class.					*/ 
/*																*/ 
/*  Programmed by Pablo van der Meer							*/ 
/*  Copyright Pablo Software Solutions 2002						*/ 
/*	http://www.pablovandermeer.nl								*/ 
/*																*/ 
/*  Last updated: 10 july 2002									*/ 
/*																*/ 
/****************************************************************/ 
 
 
#include "stdafx.h" 
#include "FTPserverApp.h" 
#include "UserManager.h" 
 
extern CFTPServerApp theApp; 
 
IMPLEMENT_SERIAL(CDirectory, CObject, 1) 
 
CDirectory::CDirectory() 
{ 
} 
 
CDirectory::~CDirectory() 
{ 
} 
 
void CDirectory::Serialize(CArchive& ar) 
{ 
	if (ar.IsStoring()) 
	{ 
		// 'store' data 
		ar << m_strDir; 
		ar << m_strAlias; 
		ar << m_bAllowDownload; 
		ar << m_bAllowUpload; 
		ar << m_bAllowRename; 
		ar << m_bAllowDelete; 
		ar << m_bAllowCreateDirectory; 
		ar << m_bIsHomeDir; 
	} 
	else 
	{ 
		// 'load' data 
		ar >> m_strDir; 
		ar >> m_strAlias; 
		ar >> m_bAllowDownload; 
		ar >> m_bAllowUpload; 
		ar >> m_bAllowRename; 
		ar >> m_bAllowDelete; 
		ar >> m_bAllowCreateDirectory; 
		ar >> m_bIsHomeDir; 
	} 
} 
 
 
template <> void AFXAPI SerializeElements  (CArchive& ar, CDirectory* pNewDirectories, int nCount) 
{ 
    for (int i = 0; i < nCount; i++, pNewDirectories++) 
    { 
        // Serialize each CDirectory object 
        pNewDirectories->Serialize(ar); 
    } 
} 
 
 
/* Copy-constructor */ 
CDirectory::CDirectory(const CDirectory &dir) 
{ 
	m_strDir = dir.m_strDir; 
	m_strAlias = dir.m_strAlias; 
	m_bAllowDownload = dir.m_bAllowDownload; 
	m_bAllowUpload = dir.m_bAllowUpload; 
	m_bAllowRename = dir.m_bAllowRename; 
	m_bAllowDelete = dir.m_bAllowDelete; 
	m_bAllowCreateDirectory = dir.m_bAllowCreateDirectory; 
	m_bIsHomeDir = dir.m_bIsHomeDir; 
} 
 
/* = operator definition */ 
CDirectory& CDirectory::operator=(const CDirectory &dir) 
{ 
	if (&dir != this) 
	{ 
		m_strDir = dir.m_strDir; 
		m_strAlias = dir.m_strAlias; 
		m_bAllowDownload = dir.m_bAllowDownload; 
		m_bAllowUpload = dir.m_bAllowUpload; 
		m_bAllowRename = dir.m_bAllowRename; 
		m_bAllowDelete = dir.m_bAllowDelete; 
		m_bAllowCreateDirectory = dir.m_bAllowCreateDirectory; 
		m_bIsHomeDir = dir.m_bIsHomeDir; 
	} 
	return *this; 
} 
 
 
IMPLEMENT_SERIAL(CUser, CObject, 1) 
 
CUser::CUser() 
{ 
	m_bAccountDisabled = FALSE; 
} 
 
CUser::~CUser() 
{ 
} 
 
void CUser::Serialize(CArchive& ar) 
{ 
	if (ar.IsStoring()) 
	{ 
		// 'store' data 
		ar << m_strName; 
		ar << m_strPassword; 
		ar << m_bAccountDisabled; 
	} 
	else 
	{ 
		// 'load' data 
		ar >> m_strName; 
		ar >> m_strPassword; 
		ar >> m_bAccountDisabled; 
	} 
	// serialize directories 
	m_DirectoryArray.Serialize(ar); 
} 
 
 
/* Copy-constructor */ 
CUser::CUser(const CUser &user) 
{ 
	m_strName = user.m_strName; 
	m_strPassword = user.m_strPassword; 
	m_bAccountDisabled = user.m_bAccountDisabled; 
	for (int i=0; i < user.m_DirectoryArray.GetSize(); i++) 
		m_DirectoryArray.Add(user.m_DirectoryArray[i]); 
} 
 
/* = operator definition */ 
CUser& CUser::operator=(const CUser &user) 
{ 
	if (&user != this) 
	{ 
		m_strName = user.m_strName; 
		m_strPassword = user.m_strPassword; 
		m_bAccountDisabled = user.m_bAccountDisabled; 
		for (int i=0; i < user.m_DirectoryArray.GetSize(); i++) 
			m_DirectoryArray.Add(user.m_DirectoryArray[i]); 
	} 
	return *this; 
} 
 
 
 
CUserManager::CUserManager() 
{ 
	GetAppDir(m_strFilename); 
	m_strFilename += "users.dat"; 
} 
 
CUserManager::~CUserManager() 
{ 
 
} 
 
 
/********************************************************************/ 
/*																	*/ 
/* Function name : Serialize										*/ 
/* Description   : Call this function to store/load the user data	*/ 
/*																	*/ 
/********************************************************************/ 
BOOL CUserManager::Serialize(BOOL bStoring) 
{ 
	static const TCHAR* lpszSignature = _T("Pablo Software Solutions - StoreObject"); 
 
	CFile file; 
 
	if (file.Open(m_strFilename, bStoring ? CFile::modeWrite|CFile::modeCreate : CFile::modeRead)) 
	{ 
		TRY 
		{ 
			CString str;  
			CArchive ar(&file, bStoring ? CArchive::store : CArchive::load); 
			 
			if (bStoring) 
			{ 
				// save signature 
				ar << CString(lpszSignature); 
 
				// Save the changed user details 
				for (int i=0; i < m_UserArray.GetSize(); i++) 
				{ 
					m_UserArray[i].Serialize(ar); 
				} 
 
				ar.Flush(); 
			} 
			else 
			{ 
				// load signature 
				ar >> str; 
				// if this the file we are looking for ? 
				if (str.Compare(lpszSignature) == 0) 
				{ 
					int nCount=0; 
 
					while(!ar.IsBufferEmpty()) 
					{ 
						CUser user; 
 
						// get user data 
						user.Serialize(ar); 
						 
						// add user to array 
						m_UserArray.Add(user); 
					} 
				} 
			} 
			ar.Close(); 
			file.Close(); 
		} 
		CATCH_ALL(e) 
		{ 
			// catch all exceptions that might happen ... 
			return FALSE; 
		} 
		END_CATCH_ALL 
	} 
	return TRUE; 
} 
 
 
/********************************************************************/ 
/*																	*/ 
/* Function name : CheckUser										*/ 
/* Description   : Lookup a specific user and return it's data		*/ 
/*																	*/ 
/********************************************************************/ 
BOOL CUserManager::CheckUser(LPCTSTR lpszUser, LPCTSTR lpszPassword, CUser &user) 
{ 
	m_CriticalSection.Lock(); 
	for (int i=0; i&array) 
{ 
	m_CriticalSection.Lock(); 
	for (int i=0; i&array) 
{ 
	m_CriticalSection.Lock(); 
	m_UserArray.RemoveAll(); 
	for (int i=0; i 356) 
	{ 
		strResult = time.Format(" %b %d %Y "); 
	} 
	else 
	{ 
		strResult.Format(" %s %02d:%02d ", time.Format("%b %d"), time.GetHour(), time.GetMinute()); 
	} 
 
	return strResult; 
}