Skip to main content

Set a Principals ACL Template in Cpp

The following code sample illustrates how to set a principal's ACL template.


Code Sample

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


<cpp_admin_principal_acltemplat_eset.cpp>
/*****************************************************************************************
*
* cpp_admin_principal_acltemplat_eset.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.h"
#include "tewinprincipal.h"
#include "teldapprincipal.h"
#include "teadmin_acl.h"
#include "teacl_template.h"

using namespace ERUCES;
using namespace std;

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

try
{
// Connect to server
auto_ptr<TEConnection> conn = TEConnection::createInstance(TEConnection::SecureConnection);
conn->open(strServer, nPort, strAdminUser, strAdminPswd);
cout << "Connected to " << strServer << " as " << strAdminUser << endl << endl;

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

int aclTemplateId = 0;

// Pick up the ACLTemplate to be added to the principal
TEObjectContainer cont;
adminACL.getACLTemplate(cont);
int i;
for (i=0; i < cont.size(); ++i)
{
TEACLTemplate *aclTemplate = (TEACLTemplate*)cont[i];
if (!aclTemplate->getName().compare("TestTemplate2")) {
aclTemplateId = aclTemplate->getACLTemplateId();
}
}
adminACL.detachConnection();

if (aclTemplateId == 0 ) {
cout << "Required TEACLTemplate was not found." << endl;
conn->close();
return -1;
}


TEAdmin admin;
admin.attachConnection(conn.operator->());

// Set ACLTemplate for a native user
TEUserAndPasswordInfo user;
user.setUserName("testuser");
admin.getPrincipal(user, user);

cout << "Before adding ACLTemplate, ACLTemplateId = " << user.getAclTemplateId() << endl;


user.setAclTemplateId(aclTemplateId); // Set ACLTemplate
//user.setAclTemplateId(0); // this will unset ACLTemplate
admin.updatePrincipal(user); // Update principal in the server.
cout << "ACLTemplate was added to the principal." << endl;

// List the principal info after added the ACLTemplate
admin.getPrincipal(user, user);
cout << "After adding ACLTemplate, ACLTemplateId = " << user.getAclTemplateId() << endl;



admin.detachConnection();

// Disconnect from server
conn->close();
cout << endl << "Disconnected from " << strServer << endl;
}
catch (TEException &e)
{
cout << e.getAllMessages().c_str() << endl;
}
catch (...)
{
cout << "Unexpected error" << endl;
}

return 0;
}
Output
Connected to localhost as admin

Before adding ACLTemplate, ACLTemplateId = 0
ACLTemplate was added to the principal.
After adding ACLTemplate, ACLTemplateId = 5

Disconnected from localhost
Press any key to continue