📄 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 <CDirectory> (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<m_UserArray.GetSize(); i++)
{
// check username case-insensitive
if (!m_UserArray[i].m_strName.CompareNoCase(lpszUser))
{
if ((!m_UserArray[i].m_strPassword.Compare(lpszPassword) || m_UserArray[i].m_strPassword.IsEmpty()) && !m_UserArray[i].m_bAccountDisabled)
{
user = m_UserArray[i];
m_CriticalSection.Unlock();
return TRUE;
}
else
{
m_CriticalSection.Unlock();
return FALSE;
}
}
}
m_CriticalSection.Unlock();
return FALSE;
}
/********************************************************************/
/* */
/* Function name : ConvertPathToLocal */
/* Description : Convert relative path to local path */
/* */
/********************************************************************/
int CUserManager::ConvertPathToLocal(LPCTSTR lpszDirectory, LPCTSTR lpszUser, CDirectory &directory, BOOL &bIsRootDir)
{
CUser user;
if (!GetUser(lpszUser, user))
{
// user not valid
return 1;
}
CStringList partList;
CString strSub;
int nCount=0;
// split path in parts
while(AfxExtractSubString(strSub, lpszDirectory, nCount++, '/'))
{
if (!strSub.IsEmpty())
partList.AddTail(strSub);
}
// get home directory
for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
{
if (user.m_DirectoryArray[i].m_bIsHomeDir)
break;
}
if (i == user.m_DirectoryArray.GetSize())
{
// no home directory found
return 1;
}
CString strLocalPath;
CString strHomeDir = user.m_DirectoryArray[i].m_strDir;
while(!partList.IsEmpty())
{
CString strPart = partList.GetHead();
partList.RemoveHead();
// does directory exist ?
if (strPart != ".." && FileExists(strHomeDir + "\\" + strPart, TRUE))
{
strHomeDir += "\\" + strPart;
}
else
// does file exist ?
if (FileExists(strHomeDir + "\\" + strPart, FALSE))
{
strHomeDir += "\\" + strPart;
}
else
{
BOOL bFound = FALSE;
// maybe it's a virtual directory
for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
{
if ((!user.m_DirectoryArray[i].m_bIsHomeDir) && (user.m_DirectoryArray[i].m_strAlias.CompareNoCase(strPart) == 0))
{
bFound = TRUE;
strHomeDir = user.m_DirectoryArray[i].m_strDir;
break;
}
}
if (!bFound)
{
// directory not found
return 2;
}
}
}
strLocalPath = strHomeDir;
// Check user rights for this directory
bIsRootDir = FALSE;
while(strHomeDir != "")
{
for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
{
CString strPath1 = strHomeDir;
strPath1.TrimRight("\\");
strPath1.MakeLower();
CString strPath2 = user.m_DirectoryArray[i].m_strDir;
strPath2.TrimRight("\\");
strPath2.MakeLower();
if (strPath1 == strPath2)
{
if (strHomeDir == strLocalPath)
{
bIsRootDir = TRUE;
}
break;
}
}
if (i == user.m_DirectoryArray.GetSize())
{
int nPos = strHomeDir.ReverseFind('\\');
if (nPos != -1)
{
strHomeDir = strHomeDir.Left(nPos);
}
else
{
// not a valid home directory
return 1;
}
continue;
}
directory = user.m_DirectoryArray[i];
strLocalPath.TrimRight('\\');
directory.m_strDir = strLocalPath;
if (!bIsRootDir)
directory.m_bIsHomeDir = FALSE;
// successfully converted directory
return 0;
}
// error converting directory
return 2;
}
/********************************************************************/
/* */
/* Function name : ChangeDirectory */
/* Description : Change to specified directory */
/* */
/********************************************************************/
int CUserManager::ChangeDirectory(LPCTSTR lpszUser, CString &strCurrentdir, CString &strChangeTo)
{
// make unix style
strChangeTo.Replace("\\","/");
while(strChangeTo.Replace("//","/"));
strChangeTo.TrimRight("/");
// now looks something like this:
// "" = root
// "/mydir/apps" = absolute path
// "mydir/apps" = relative path
if (strChangeTo == "")
{
// goto root
strChangeTo = "/";
}
else
{
// first character '/' ?
if (strChangeTo.Left(1) != "/")
{
// client specified a path relative to their current path
strCurrentdir.TrimRight("/");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -