?? usermanager.cpp
字號:
/****************************************************************/
/* */
/* UserManager.cpp */
/* */
/* Implementation of the CUserManager class. */
/* */
/* Programmed by Pablo van der Meer */
/* Based partially on and inspired by FileZilla Server. */
/* */
/* Copyright Pablo Software Solutions 2002 */
/* http://www.pablovandermeer.nl */
/* */
/* Last updated: 21 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("Li 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 : ConvertPathToLocal */
/* Description : Convert relative path to local path */
/* */
/********************************************************************/
BOOL CUserManager::ConvertPathToLocal(LPCTSTR lpszUser, CString &strDirectoryIn, CString &strDirectoryOut)
{
CUser user;
if (!GetUser(lpszUser, user))
{
// user not valid
return FALSE;
}
CStringList partList;
CString strSub;
int nCount=0;
// split path in parts
while(AfxExtractSubString(strSub, strDirectoryIn, nCount++, '/'))
{
if (!strSub.IsEmpty())
partList.AddTail(strSub);
}
// search for home directory
for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
{
if (user.m_DirectoryArray[i].m_bIsHomeDir)
{
CString strHomeDir = user.m_DirectoryArray[i].m_strDir;
while(!partList.IsEmpty())
{
CString strPart = partList.GetHead();
partList.RemoveHead();
CString strCheckDir;
if (strPart == "..")
{
// go back one level
int nPos = strHomeDir.ReverseFind('\\');
if (nPos != -1)
{
strCheckDir = strHomeDir.Left(nPos);
}
}
else
{
strCheckDir = strHomeDir + "\\" + strPart;
}
// does directory exist ?
if (FileExists(strCheckDir, TRUE))
{
strHomeDir = strCheckDir;
}
else
// does file exist ?
if (FileExists(strCheckDir, FALSE))
{
strHomeDir = strCheckDir;
}
else
{
BOOL bFound = FALSE;
// virtual directories exist only in the root
if (strHomeDir == user.m_DirectoryArray[i].m_strDir)
{
// maybe it's a virtual directory
for (int j=0; j<user.m_DirectoryArray.GetSize(); j++)
{
if (i != j && (user.m_DirectoryArray[j].m_strAlias.CompareNoCase(strPart) == 0))
{
bFound = TRUE;
strHomeDir = user.m_DirectoryArray[j].m_strDir;
break;
}
}
}
if (!bFound)
{
// directory not found
return FALSE;
}
}
}
// successfully converted directory
strDirectoryOut = strHomeDir;
return TRUE;
}
}
// no home directory found
return FALSE;
}
/********************************************************************/
/* */
/* Function name : CheckAccessRights */
/* Description : Check if user has access to specified directory. */
/* */
/********************************************************************/
BOOL CUserManager::CheckAccessRights(LPCTSTR lpszUser, LPCTSTR lpszDirectory, int nOption)
{
CUser user;
if (!GetUser(lpszUser, user))
{
// user not valid
return FALSE;
}
// start with full path
CString strCheckDir = lpszDirectory;
while(!strCheckDir.IsEmpty())
{
// search for a matching part
for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
{
CString strPath1 = strCheckDir;
strPath1.TrimRight("\\");
CString strPath2 = user.m_DirectoryArray[i].m_strDir;
strPath2.TrimRight("\\");
// found a match ?
if (strPath1.CompareNoCase(strPath2) == 0)
{
// check file access rights
if (((!user.m_DirectoryArray[i].m_bAllowDownload) && (nOption == FTP_DOWNLOAD)) ||
((!user.m_DirectoryArray[i].m_bAllowUpload) && (nOption == FTP_UPLOAD)) ||
((!user.m_DirectoryArray[i].m_bAllowRename) && (nOption == FTP_RENAME)) ||
((!user.m_DirectoryArray[i].m_bAllowDelete) && (nOption == FTP_DELETE)) ||
((!user.m_DirectoryArray[i].m_bAllowCreateDirectory) && (nOption == FTP_CREATE_DIR)))
{
return FALSE;
}
return TRUE;
}
}
int nPos = strCheckDir.ReverseFind('\\');
if (nPos != -1)
{
// strip subdir
strCheckDir = strCheckDir.Left(nPos);
}
else
{
// we're done
strCheckDir.Empty();
}
}
// users has no rights to this directory
return FALSE;
}
/********************************************************************/
/* */
/* 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("/");
strChangeTo = strCurrentdir + "/" + strChangeTo;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -