Add an ACL Entry in Cpp
The following code sample illustrates how to add a TEACLEntry object to a TEACLInfo object.
Code Sample
See the C++ environment settings for details on setting the project environment.
<cpp_admin_acl_addentry.cpp>
/*****************************************************************************************
*
* cpp_admin_acl_addentry.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();
}
// Create an ACL entry to add
TEACLEntry ent2add;
ent2add.setACLId( acl.getACLId() );
ent2add.setACLRight(7);
ent2add.setEndTimestamp(0);
ent2add.setMaxUsage(0);
ent2add.setPrincipalId(1);
ent2add.setStartTimestamp(0);
ent2add.setSystemID(lSystemID);
ent2add.setUserRight(1);
TEACLInfo acl2;
acl2.setACLId( acl.getACLId() );
acl2.addEntry( ent2add );
// Update the server, the ACL entry will be added.
admin.updateACL(ACLENTRY_ADD, acl2);
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;
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;
}
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 T-tag"); // 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.getHiddenLink(1, strTtag); // T-tag 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: AAAAAYlokTUmcEXBsuNiFIU9qCOje2QR7CwPV3QZFnA7DfWTcRvdpQ==
Disconnected from localhost
Connected to localhost as ls
ACLInfo:
ACLID: 9003
Expiration Flag: 0
Number of ACL entries: 1
ACLID: 9003
ACLRight: 7
CreationTimestamp: 1120229652
EndTimestamp: 0
MaxUsage: 4294967295
PrincipalId: 3001
StartTimestamp: 0
SystemID: 1
UserRight: 0
ACLInfo was updated.
ACLInfo:
ACLID: 9003
Expiration Flag: 0
Number of ACL entries: 2
ACLID: 9003
ACLRight: 7
CreationTimestamp: 1120229652
EndTimestamp: 0
MaxUsage: 0
PrincipalId: 1
StartTimestamp: 0
SystemID: 1
UserRight: 1
ACLID: 9003
ACLRight: 7
CreationTimestamp: 1120229652
EndTimestamp: 0
MaxUsage: 4294967295
PrincipalId: 3001
StartTimestamp: 0
SystemID: 1
UserRight: 0
Disconnected from localhost