Add Group to a Principal
The following code sample illustrates how to add a group to a principal.
Code Sample
See the C++ environment settings for details on setting the project environment.
<cpp_admin_principal_group_add.cpp>
/*****************************************************************************************
*
* cpp_admin_principal_group_add.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;
TEAdmin admin;
admin.attachConnection(conn.operator->());
// Get 2 groupIDs to be added.
TEGroupInfo group;
group.setGroupName("testgroup");
admin.getPrincipal(group, group);
int id2Add = group.getPrincipalId();
TEGroupInfo group2;
group2.setGroupName("testgroup2");
admin.getPrincipal(group2, group2);
int id2Add2 = group2.getPrincipalId();
TEUserAndPasswordInfo user;
user.setUserName("testuser");
admin.getPrincipal(user, user);
cout << "User ID = " << user.getPrincipalId() << endl;
cout << "User Name = " << user.getUserName() << endl << endl;
// List group IDs before adding group
TEObjectContainer cont;
user.getGroupIds(cont);
cout << "Before adding group, number of groups = " << cont.size() << endl;
int i;
for (i=0; i < cont.size(); ++i)
{
int groupID = ((TEUInt32*)cont[i])->getValue();
cout << "\tGroup Id: " << groupID << endl;
}
// Add groups
user.addGroup(id2Add);
user.addGroup(id2Add2);
admin.updatePrincipal(user); // Update principal in the server.
cout << "Groups were added to the principal." << endl;
// List group IDs after adding group
admin.getPrincipal(user, user);
user.getGroupIds(cont);
cout << endl;
cout << "After adding groups, number of reviewers = " << cont.size() << endl;
for (i=0; i < cont.size(); ++i)
{
int groupID = ((TEUInt32*)cont[i])->getValue();
cout << "\tGroup Id: " << groupID << 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
User ID = 3004
User Name = testuser
Before adding group, number of groups = 0
Groups were added to the principal.
After adding groups, number of reviewers = 2
Group Id: 3005
Group Id: 3006
Disconnected from localhost
Press any key to continue