Remove an ACL Entry in Cpp
The following code sample illustrates how to remove a TEACLEntry object from a TEACLInfo object.
Code Sample
See the C++ environment settings for details on setting the project environment.
<cpp_admin_acl_removeentry.cpp>
/*****************************************************************************************
*
* cpp_admin_acl_removeentry.cpp
*
*
******************************************************************************************/
#include<string>
#include<iostream>
#include "teagent.h"
#include "teconnection.h"
#include "teexception.h"
#include "teauthuserctx.h"
#include "teobject.h"
#include "teadmin.h"
using namespace ERUCES;
using namespace std;
string generateTtag(string strServer, long nAdminPort, string strUser, string strPswd);
int main()
{
string strServer = "localhost";
long nAdminPort = 8888;
string strAdminUser = "ls";
string strAdminPswd = "password";
try
{
// Set the T-tag for which the ACLInfo will be got.
string strTT = generateTtag(strServer, nAdminPort, strAdminUser, strAdminPswd);
// Connect to server
auto_ptr<TEConnection> conn = TEConnection::createInstance(TEConnection::SecureConnection);
auto_ptr<TEUserAuthenticationContext> ctx = TEUserAuthenticationContext::getInstance();
ctx->setUser(strAdminUser);
ctx->setPasswd(strAdminPswd);
conn->open(strServer, nAdminPort, *ctx.operator->());
cout << "Connected to " << strServer << " as " << strAdminUser << endl;
TEAdmin admin;
admin.attachConnection(conn.operator->());
TEACLInfo acl;
admin.getACL(strTT, acl, true);
cout << "ACLInfo: " << endl;
cout << "\tACLID: " << acl.getACLId() << endl;
cout << "\tExpiration Flag: " << acl.getKeyExpirationFlag() << endl;
cout << "\tNumber of ACL entries: " << acl.EntryCount() << endl;
long lSystemID; // for assignment late
int i=0;
TEObjectContainer cont;
acl.getEntries(cont);
for (i=0; i < cont.size(); ++i)
{
TEACLEntry *ent = (TEACLEntry*)cont[i];
cout << endl;
cout << "\tACLID: " << ent->getACLId() << endl;
cout << "\tACLRight: " << ent->getACLRight() << endl;
cout << "\tCreationTimestamp: " << ent->getCreationTimestamp() << endl;
cout << "\tEndTimestamp: " << ent->getEndTimestamp() << endl;
cout << "\tMaxUsage: " << ent->getMaxUsage() << endl;
cout << "\tPrincipalId: " << ent->getPrincipalId() << endl;
cout << "\tStartTimestamp: " << ent->getStartTimestamp() << endl;
cout << "\tSystemID: " << ent->getSystemID() << endl;
cout << "\tUserRight: " << ent->getUserRight() << endl;
if (i==0)
lSystemID = ent->getSystemID();
}
long lPrincipalID = 2001;
// Create an ACL entry to add
TEACLEntry ent2add;
ent2add.setACLId( acl.getACLId() );
ent2add.setPrincipalId(lPrincipalID);
ent2add.setSystemID(lSystemID);
ent2add.setStartTimestamp(0);
ent2add.setEndTimestamp(0);
ent2add.setACLRight(1); // 1 read-only
ent2add.setMaxUsage(0);
ent2add.setUserRight(1);
acl.clearEntry();
acl.addEntry( ent2add );
// Update the server, the ACL entry will be added.
admin.updateACL(ACLENTRY_ADD, acl);
cout << endl;
cout << "A new entry was added to ACLInfo. " << endl;
// Get the updated info.
admin.getACL(strTT, acl, true);
cout << "ACLInfo: " << endl;
cout << "\tACLID: " << acl.getACLId() << endl;
cout << "\tExpiration Flag: " << acl.getKeyExpirationFlag() << endl;
cout << "\tNumber of ACL entries: " << acl.EntryCount() << endl;
TEObject * pOjb = NULL; // ACL entry to be removed.
acl.getEntries(cont);
for (i=0; i < cont.size(); ++i)
{
TEACLEntry *ent = (TEACLEntry *)cont[i];
cout << endl;
cout << "\tACLID: " << ent->getACLId() << endl;
cout << "\tACLRight: " << ent->getACLRight() << endl;
cout << "\tCreationTimestamp: " << ent->getCreationTimestamp() << endl;
cout << "\tEndTimestamp: " << ent->getEndTimestamp() << endl;
cout << "\tMaxUsage: " << ent->getMaxUsage() << endl;
cout << "\tPrincipalId: " << ent->getPrincipalId() << endl;
cout << "\tStartTimestamp: " << ent->getStartTimestamp() << endl;
cout << "\tSystemID: " << ent->getSystemID() << endl;
cout << "\tUserRight: " << ent->getUserRight() << endl;
if ( ent->getPrincipalId() == lPrincipalID )
{
pOjb = ent->clone();
}
}
// Remove ACL entry
if (pOjb)
{
TEACLEntry * pEnt2remove = static_cast<TEACLEntry *>(pOjb);
acl.clearEntry();
acl.addEntry( *pEnt2remove );
delete pOjb;
// Update the server, the ACL entry will be removed.
admin.updateACL(ACLENTRY_REMOVE, acl);
cout << endl;
cout << "ACLInfo was updated. " << endl;
// Get the updated info.
admin.getACL(strTT, acl, true);
cout << "ACLInfo: " << endl;
cout << "\tACLID: " << acl.getACLId() << endl;
cout << "\tExpiration Flag: " << acl.getKeyExpirationFlag() << endl;
cout << "\tNumber of ACL entries: " << acl.EntryCount() << endl;
TEObjectContainer cont;
acl.getEntries(cont);
for ( i=0; i < cont.size(); ++i)
{
TEACLEntry *ent = (TEACLEntry*)cont[i];
cout << endl;
cout << "\tACLID: " << ent->getACLId() << endl;
cout << "\tACLRight: " << ent->getACLRight() << endl;
cout << "\tCreationTimestamp: " << ent->getCreationTimestamp() << endl;
cout << "\tEndTimestamp: " << ent->getEndTimestamp() << endl;
cout << "\tMaxUsage: " << ent->getMaxUsage() << endl;
cout << "\tPrincipalId: " << ent->getPrincipalId() << endl;
cout << "\tStartTimestamp: " << ent->getStartTimestamp() << endl;
cout << "\tSystemID: " << ent->getSystemID() << endl;
cout << "\tUserRight: " << ent->getUserRight() << endl;
}
}
else
cout << "ACL entry was not found." << endl;
admin.detachConnection();
// Disconnect from server
conn->close();
cout << "disconnected from " << strServer << endl;
}
catch (TEException &e)
{
cout << e.getAllMessages().c_str() << endl;
}
catch (...)
{
cout << "Unexpected error" << endl;
}
return 0;
}
string generateTtag(string strServer, long nAdminPort, string strUser, string strPswd)
{
string strTtag = "";
auto_ptr<TEConnection> conn = TEConnection::createInstance(TEConnection::NormalConnection);
// Open connection to the server
conn->open(strServer, nAdminPort, strUser, strPswd);
cout << "Connected to " << strServer << " at " << nAdminPort
<< " as " << strUser << "\n" << endl;
// Create TEAgentMatrix object
TEAgentMatrix matrix;
// Set matrix for encryption
matrix.setElement(1, 1, "dummy data for creating Ttag"); // set element (1, 1)
// Encrypt matrix
matrix.attachConnection(conn.operator->()); // Attach connection
matrix.encrypt();
matrix.detachConnection(); // Detach connection
// Get T-tag
matrix.setEncodeMode(true);
matrix.getTtag(1, strTtag); // Ttag for first row
cout << "T-tag: " << strTtag << endl;
// Close conneciton
conn->close();
cout << "Disconnected from " << strServer << endl;
return strTtag;
}
Output
Connected to localhost at 8888 as ls
T-tag: AAAAAWg1xjcQTXW6DwnQi6a2cjBWJTz54d0k4qW4i4nt4+j1Pdu/bQ==
Disconnected from localhost
Connected to localhost as ls
ACLInfo:
ACLID: 1012
Expiration Flag: 0
Number of ACL entries: 1
ACLID: 1012
ACLRight: 7
CreationTimestamp: 1124121726
EndTimestamp: 0
MaxUsage: 4294967295
PrincipalId: 1001
StartTimestamp: 0
SystemID: 1
UserRight: 0
A new entry was added to ACLInfo.
ACLInfo:
ACLID: 1012
Expiration Flag: 0
Number of ACL entries: 2
ACLID: 1012
ACLRight: 7
CreationTimestamp: 1124121726
EndTimestamp: 0
MaxUsage: 4294967295
PrincipalId: 1001
StartTimestamp: 0
SystemID: 1
UserRight: 0
ACLID: 1012
ACLRight: 1
CreationTimestamp: 1124121726
EndTimestamp: 0
MaxUsage: 0
PrincipalId: 2001
StartTimestamp: 0
SystemID: 1
UserRight: 1
ACLInfo was updated.
ACLInfo:
ACLID: 1012
Expiration Flag: 0
Number of ACL entries: 1
ACLID: 1012
ACLRight: 7
CreationTimestamp: 1124121726
EndTimestamp: 0
MaxUsage: 4294967295
PrincipalId: 1001
StartTimestamp: 0
SystemID: 1
UserRight: 0
disconnected from localhost