Skip to main content

Update ACL Template in Cpp

The following code sample illustrates how to update an ACL template.


Code Sample

See the C++ environment settings for details on setting the project environment.


<cpp_adminacl_acltemplate_update.cpp>
/*****************************************************************************************
*
* cpp_adminacl_acltemplate_update.cpp
*
*
******************************************************************************************/

#include<string>
#include<iostream>

#include "teagent.h"
#include "teconnection.h"
#include "teexception.h"
#include "teauthuserctx.h"
#include "teauthwinctx.h"
#include "teobject.h"
#include "teadmin_local.h"
#include "teadmin_acl.h"
#include "teacl_template.h"

using namespace ERUCES;
using namespace std;

int main()
{
string strServer = "xyang";
long nPort = 8888;
string strAdminUser = "admin";
string strAdminPswd = "password1A";

try
{
auto_ptr<TEConnection> conn = TEConnection::createInstance(TEConnection::SecureConnection); // Use SecureConnection here

// Open connection to the server for native user
auto_ptr<TEUserAuthenticationContext> ctx = TEUserAuthenticationContext::getInstance();
ctx->setUser(strAdminUser);
ctx->setPasswd(strAdminPswd);
conn->open(strServer, nPort, *ctx.operator->());
cout << "Connected to " << strServer << " as " << strAdminUser << " at port " << nPort << endl;


TEAdminACL adminACL;
adminACL.attachConnection(conn.operator->());

// Get PrincipalID for late use
TEUserAndPasswordInfo user;
user.setUserName("testuser");
adminACL.getPrincipalByName(user);
cout << "User ID = " << user.getPrincipalId() << endl;
int nPrincipalID = user.getPrincipalId();

TEACLTemplate *aclTemplate2Update = NULL;
TEACLTemplateMember *member2Update = NULL;

// List ACLTemplates
TEObjectContainer cont, cont2;
adminACL.getACLTemplate(cont);
cout << "Before updating ACLTemplate, number of ACLTemplates = " << cont.size() << endl;
int i, j;
for (i=0; i < cont.size(); ++i)
{
TEACLTemplate *aclTemplate = (TEACLTemplate*)cont[i];
cout << endl;
cout << "\tACLTemplateId: " << aclTemplate->getACLTemplateId() << endl;
cout << "\tName: " << aclTemplate->getName() << endl;
cout << "\tFlag : " << aclTemplate->getFlag () << endl;

if (!aclTemplate->getName().compare("TestTemplate")) {
aclTemplate2Update = aclTemplate;
}

aclTemplate->getMembers(cont2);
cout << "\tNumber of Members: " << cont2.size() << endl;
for (j=0; j < cont2.size(); ++j)
{
TEACLTemplateMember *member = (TEACLTemplateMember*)cont2[j];
cout << endl;
cout << "\t\tPrincipalId: " << member->getPrincipalId() << endl;
cout << "\t\tSystemId: " << member->getSystemId() << endl;
cout << "\t\tRight: " << member->getRight() << endl;
cout << "\t\tACLTemplateId: " << member->getACLTemplateId() << endl;
cout << "\t\tStartTime: " << member->getStartTime() << endl;
cout << "\t\tEndTime: " << member->getEndTime() << endl;
cout << "\t\tMaxUsage: " << member->getMaxUsage() << endl;

if (aclTemplate2Update == aclTemplate &&
member->getPrincipalId() == nPrincipalID) {
member2Update = member;
}
}

}

if (aclTemplate2Update == NULL ) {
cout << "Required TEACLTemplate was not found." << endl;
goto FailureExit;
}

if (member2Update == NULL ) {
cout << "Required TEACLTemplateMember was not found." << endl;
goto FailureExit;
}

// Update TEACLTemplate
aclTemplate2Update->setName("TestTemplate2");
member2Update->setRight(TE_ACL_TEMPLATE_READ);


adminACL.updateACLTemplateMember(*member2Update, TEAdminACL::ACL_TEMPLATE_MEMBER_UPDATE);
adminACL.updateACLTemplate(*aclTemplate2Update);
cout << "\nTEACLTemplate was updated." << endl;


// List ACLTemplates after updating
adminACL.getACLTemplate(cont);
cout << "\nAfter updating ACLTemplate, number of ACLTemplates = " << cont.size() << endl;
for (i=0; i < cont.size(); ++i)
{
TEACLTemplate *aclTemplate = (TEACLTemplate*)cont[i];
cout << endl;
cout << "\tACLTemplateId: " << aclTemplate->getACLTemplateId() << endl;
cout << "\tName: " << aclTemplate->getName() << endl;
cout << "\tFlag : " << aclTemplate->getFlag () << endl;

TEObjectContainer cont2;

aclTemplate->getMembers(cont2);
cout << "\tNumber of Members: " << cont2.size() << endl;
for (j=0; j < cont2.size(); ++j)
{
TEACLTemplateMember *member = (TEACLTemplateMember*)cont2[j];
cout << endl;
cout << "\t\tPrincipalId: " << member->getPrincipalId() << endl;
cout << "\t\tSystemId: " << member->getSystemId() << endl;
cout << "\t\tRight: " << member->getRight() << endl;
cout << "\t\tACLTemplateId: " << member->getACLTemplateId() << endl;
cout << "\t\tStartTime: " << member->getStartTime() << endl;
cout << "\t\tEndTime: " << member->getEndTime() << endl;
cout << "\t\tMaxUsage: " << member->getMaxUsage() << endl;
}
}

FailureExit:

adminACL.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;
}
Output
Connected to xyang as admin at port 8888
User ID = 3004
Before updating ACLTemplate, number of ACLTemplates = 1

ACLTemplateId: 4
Name: TestTemplate
Flag : 1
Number of Members: 2

PrincipalId: 1
SystemId: 1
Right: 768
ACLTemplateId: 4
StartTime: 0
EndTime: 0
MaxUsage: 4294967295

PrincipalId: 3004
SystemId: 1
Right: 768
ACLTemplateId: 4
StartTime: 0
EndTime: 0
MaxUsage: 4294967295

TEACLTemplate was updated.

After updating ACLTemplate, number of ACLTemplates = 1

ACLTemplateId: 4
Name: TestTemplate2
Flag : 1
Number of Members: 2

PrincipalId: 1
SystemId: 1
Right: 768
ACLTemplateId: 4
StartTime: 0
EndTime: 0
MaxUsage: 4294967295

PrincipalId: 3004
SystemId: 1
Right: 256
ACLTemplateId: 4
StartTime: 0
EndTime: 0
MaxUsage: 4294967295
Disconnected from xyang
Press any key to continue