📄 acllog.cpp
字号:
//===========================================================================
//
// Module: ACLLOG.CPP
//
// Description:
// Sample C++ API program that creates an access control entry
// list log file.
//
// Syntax: ACLLOG <dbname> [server]
//
// Where dbname = file path name for the database to add the ACL
// information. If you do not supply this parameter you will be
// prompted for arguments at run time.
//
//===========================================================================
#ifndef ACLLOG_HPP
#include "acllog.hpp"
#endif
// Parameter Strings
IOParameter CommandBuf;
IOParameter PromptString1;
IOParameter ParamString1;
IOParameter PromptString2;
IOParameter ParamString2;
//---------------------------------------------------------------------------
//
// MAIN
//
//---------------------------------------------------------------------------
int main(int argc, char *argv[])
{
int ProgramStatus = 0;
LNNotesSession Session;
LNSTATUS Stat;
LNSetThrowAllErrors(TRUE);
LNDatabase ACLDb;
ACLLog Log;
// Begin TRY block.
// Throw all errors encountered during command execution.
try
{
LNString ACLDbTitle;
LNDocument Doc;
LNString DatabasePath;
LNString ServerName;
// Initialize the Notes session.
Session.Init(argc, argv);
// Parse the argument list.
ProcessArguments( argc, argv, &DatabasePath, &ServerName);
LNString AclLogFile = "acllog.nsf";
LNString AclLogServer = "";
// Get the specified database.
Session.GetDatabase(DatabasePath, &ACLDb, ServerName);
// Open the log database.
Stat = Log.OpenAclDB( &Session, AclLogFile, AclLogServer );
if (Stat)
throw Stat;
LNACL Acl;
LNACLEntryArray Entries;
LNACLEntry Entry;
LNACLRoleArray EntryRoles;
LNINT EntryCount,EntryRoleCount;
LNINT Index1, Index2;
ACLDb.Open();
ACLDbTitle = ACLDb.GetTitle();
// Get the database's ACL.
Stat = ACLDb.GetACL(&Acl);
Stat = Acl.GetEntries(&Entries);
EntryCount = Entries.GetCount();
for (Index1 = 0; Index1 < EntryCount; Index1 ++)
{
// Create a New log document to log all of the ACL entry list
// data to. Log the db data to the top of the new log.
Stat = Log.CreateLogEntry( ServerName, DatabasePath, ACLDbTitle );
if (Stat)
throw Stat;
// Then output all the ACL entry info in the ACL.
Entry = Entries[Index1];
Stat = Log.AddACLEntry( Entry );
if (Stat)
throw Stat;
cout << "Entry Name: " << Entry.GetName() << endl;
// Output all the roles for each ACL entry.
Entry.GetRoles(&EntryRoles);
EntryRoleCount = EntryRoles.GetCount();
for (Index2 = 0; Index2 < EntryRoleCount; Index2 ++)
{
cout << " Entry Role Name: " << EntryRoles[Index2] << endl;
}
// All done; close the ACL log document.
Stat = Log.CloseLogEntry();
if (Stat)
throw Stat;
}
}// try
// Error handler. If an error occurred, get the text of
// the error message and display it.
catch (LNSTATUS lnerror)
{
char ErrorBuf[LNERROR_MESSAGE_LENGTH];
LNGetErrorMessage(lnerror, ErrorBuf);
cout << "\nError: " << ErrorBuf << endl;
ProgramStatus = 2;
}
catch (const char *pErrorMessage)
{
cout << "\nError: " << pErrorMessage << endl << endl;
ProgramStatus = 1;
}
// Close the log database.
if (Log.IsDbOpen())
Log.CloseAclDB();
// Close the source database.
if (ACLDb.IsOpen())
ACLDb.Close();
// Terminate the API.
Session.Term();
cout << endl;
cout << "Hit Return To Exit: ";
cin >> CommandBuf;
return (ProgramStatus);
} // END MAIN
//===========================================================================
//
// ACLLog Class Implementation
//
// Description:
// Implementation of an Abstract Logging ACL object which can be
// used to store and keep track of ACL entry list information
// from a user-specified .NSF file.
//===========================================================================
//---------------------------------------------------------------------------
//
// Name:
// Default constructor
//
//---------------------------------------------------------------------------
ACLLog::ACLLog()
{
LogSession = 0;
IsDBOpen = FALSE;
IsDocOpen = FALSE;
}
//---------------------------------------------------------------------------
//
// Name:
// Destructor
//
//---------------------------------------------------------------------------
ACLLog::~ACLLog( )
{
}
//---------------------------------------------------------------------------
//
// Name:
// ACLLog::OpenAclDB
//
// Description:
// Opens the ACL log database for ACL info. This version takes
// a pointer to an initialized LNNotesSession object.
//
// Inputs:
// session Pointer to an initialized Notes session object
// path: Database pathname
// server: Server name (null string for local database)
//
// Outputs:
// If successful, the database is opened and ready for log entries.
//
// Returns:
// 0 if successful, error code otherwise.
//
//---------------------------------------------------------------------------
LNSTATUS ACLLog::OpenAclDB( LNNotesSession *Session, const LNString &Path,
const LNString &Server)
{
LNSTATUS Error;
if (! LNIsNotesInitialized() )
return LNERR_SESSION_NOT_INITIALIZED;
if (!Session)
return LNERR_NULL_POINTER_PARAMETER;
LogSession = Session;
// Get the specified Log database.
Error = LogSession->GetDatabase(Path, &ACLLogDb, Server);
if (Error)
return Error;
// Open it.
Error = ACLLogDb.Open();
if (Error)
return Error;
IsDBOpen = TRUE;
return LNNOERROR;
} // END ACLLog::OpenAclDB
//---------------------------------------------------------------------------
//
// Name:
// ACLLog::CloseAclDB
//
// Description:
// Closes the ACL log database for ACL Log.
//
//---------------------------------------------------------------------------
LNSTATUS ACLLog::CloseAclDB()
{
LNSTATUS Error;
if (! IsDBOpen )
return LNWARN_NOT_OPEN;
// Close the database.
Error = ACLLogDb.Close();
IsDBOpen = FALSE;
return Error;
} // END ACLLog::CloseAclDB
//---------------------------------------------------------------------------
//
// Name:
// ACLLog::CreateLogEntry
//
// Description:
// A new document is created in the log database and the main
// heading is recorded in it. All of the db info about the
// specified .NSF file being logged is added to the header area.
//
// Inputs:
// The file name of the .NSF file to iterate the ACL entries.
// The title of the database for which the ACL entries are being
// iterated.
//
// Outputs:
// A new ACL log document is created in the log database.
//
// Returns:
// Success or error code
//
//---------------------------------------------------------------------------
LNSTATUS ACLLog::CreateLogEntry(const LNString &ServerName, const LNString &NSFName,
const LNString &DBTitle)
{
LNSTATUS Error;
LNDatetime Dt;
LNDatetimes TimeFld;
LNText ServerNameFld;
LNText NSFNameFld;
LNText DBTitleFld;
if (! IsDBOpen )
return LNERR_DATABASE_CLOSED;
// Create a new document log Entry.
Error = ACLLogDb.CreateDocument(&AclEntryDoc);
if (Error)
return Error;
// Attach the document to the "U&G".
Error = AclEntryDoc.SetForm("U&G");
if (Error)
return Error;
// Set up some flags for this log doc that are needed later.
IsDocOpen = TRUE;
// Get a snapshot of the current date/time.
Dt = LogSession->GetCurrentDatetime();
// Set the current datetime into the TimeField LNItem.
Error = TimeFld.SetValue(Dt);
if (Error)
return Error;
// Create the new time field item in the new document.
Error = AclEntryDoc.CreateItem("ModTime", TimeFld, LNITEMFLAGS_SUMMARY);
if (Error)
return Error;
// Set the new server name field to the supplied server name.
if (ServerName.IsNull())
{
Error = ServerNameFld.SetValue("Local");
if(Error)
return Error;
}
else
{
Error = ServerNameFld.SetValue(ServerName);
if (Error)
return Error;
}
// Create the new server name field item in the new document.
Error = AclEntryDoc.CreateItem("Server", ServerNameFld, LNITEMFLAGS_SUMMARY);
if (Error)
return Error;
// Set the new NSF name field to the supplied NSF file name.
Error = NSFNameFld.SetValue(NSFName);
if (Error)
return Error;
// Create the new NSF name field item in the new document.
Error = AclEntryDoc.CreateItem("DBFile", NSFNameFld, LNITEMFLAGS_SUMMARY);
if (Error)
return Error;
// Set the database title field.
Error = DBTitleFld.SetValue(DBTitle);
if (Error)
return Error;
// Create the database title field item in the new document.
Error = AclEntryDoc.CreateItem( "DBTitle", DBTitleFld, LNITEMFLAGS_SUMMARY );
if (Error)
return Error;
// Save the newly created document to disk but leave it open
// for appends to the log Entry field.
Error = AclEntryDoc.Save();
if (Error)
return Error;
return LNNOERROR;
} // END ACLLog::CreateLogEntry
//---------------------------------------------------------------------------
//
// Name:
// ACLLog::AddACLEntry()
//
// Description:
// A new document is created in the log database and the main
// heading is recorded in it. All of the ACL entry info about
// the specified .NSF file is being logged to the document.
//
// Inputs:
// LNACLEntry for the ACL entry information.
//
// Outputs:
// None
//
// Returns:
// Success or error code
//
//---------------------------------------------------------------------------
LNSTATUS ACLLog::AddACLEntry(LNACLEntry &Entry)
{
LNSTATUS Error;
if (! IsDBOpen )
return LNERR_DATABASE_CLOSED;
if (! IsDocOpen )
return LNNOERROR;
LNText EntryNameFld;
Error = EntryNameFld.SetValue(Entry.GetName());
if (Error)
return Error;
Error = AclEntryDoc.CreateItem("Name", EntryNameFld, LNITEMFLAGS_SUMMARY);
if (Error)
return Error;
LNString Roles;
LNACLRoleArray ARoles;
LNText RoleNameFld;
Entry.GetRoles(&ARoles);
LNINT Count = ARoles.GetCount();
for (LNINT Index = 0; Index < Count; Index++)
{
Roles << ARoles[Index];
if (Index < Count - 1)
Roles << ", "; // a separator
}
// Set the new entry name field to the supplied entry name.
if (Count == 0)
{
Error = RoleNameFld.SetValue("None");
if (Error)
return Error;
}
else
{
Error = RoleNameFld.SetValue(Roles);
if (Error)
return Error;
}
// Create the new NSF name field item in the new document.
Error = AclEntryDoc.CreateItem("Roles", RoleNameFld, LNITEMFLAGS_SUMMARY);
if (Error)
return Error;
LNText TypeFld;
switch(Entry.GetUserType())
{
case LNACLUSERTYPE_PERSON:
{
Error = TypeFld.SetValue("Person");
if (Error)
return Error;
break;
}
case LNACLUSERTYPE_SERVER:
{
Error = TypeFld.SetValue("Server");
if (Error)
return Error;
break;
}
case LNACLUSERTYPE_GROUP:
{
Error = TypeFld.SetValue("Group");
if (Error)
return Error;
break;
}
case LNACLUSERTYPE_PERSONGROUP:
{
Error = TypeFld.SetValue("Person Group");
if (Error)
return Error;
break;
}
case LNACLUSERTYPE_SERVERGROUP:
{
Error = TypeFld.SetValue("Server Group");
if (Error)
return Error;
break;
}
case LNACLUSERTYPE_UNKNOWN:
{
Error = TypeFld.SetValue("Unknown");
if (Error)
return Error;
break;
}
}
Error = AclEntryDoc.CreateItem("Type", TypeFld, LNITEMFLAGS_SUMMARY);
if (Error)
return Error;
LNText LevelFld;
switch(Entry.GetAccessLevel())
{
case LNACLLEVEL_NO_ACCESS:
{
Error = LevelFld.SetValue("No Access");
if (Error)
return Error;
break;
}
case LNACLLEVEL_DEPOSITOR:
{
Error = LevelFld.SetValue("Depositor");
if (Error)
return Error;
break;
}
case LNACLLEVEL_READER:
{
Error = LevelFld.SetValue("Reader");
if (Error)
return Error;
break;
}
case LNACLLEVEL_AUTHOR:
{
Error = LevelFld.SetValue("Author");
if (Error)
return Error;
break;
}
case LNACLLEVEL_EDITOR:
{
Error = LevelFld.SetValue("Editor");
if (Error)
return Error;
break;
}
case LNACLLEVEL_DESIGNER:
{
Error = LevelFld.SetValue("Designer");
if (Error)
return Error;
break;
}
case LNACLLEVEL_MANAGER:
{
Error = LevelFld.SetValue("Manager");
if (Error)
return Error;
break;
}
}
Error = AclEntryDoc.CreateItem("Level", LevelFld, LNITEMFLAGS_SUMMARY);
if (Error)
return Error;
LNText CreDocFld;
if (Entry.GetCanCreateDocuments())
{
Error = CreDocFld.SetValue("Yes");
if (Error)
return Error;
}
else
{
Error = CreDocFld.SetValue("No");
if (Error)
return Error;
}
Error = AclEntryDoc.CreateItem("CreDoc", CreDocFld, LNITEMFLAGS_SUMMARY);
if (Error)
return Error;
LNText DelDocFld;
if (Entry.GetCanDeleteDocuments())
{
Error = DelDocFld.SetValue("Yes");
if (Error)
return Error;
}
else
{
Error = DelDocFld.SetValue("No");
if (Error)
return Error;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -