Get a List of Principals
The following code sample illustrates how to get a list of principals.
Code Samples
See the C++ environment settings for details on setting the project environment.
<cpp_admin_principal_getlist.cpp>
/*****************************************************************************************
*
* cpp_admin_principal_getlist.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"
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->());
TEObjectContainer container;
int nType = 1; // 1 for normal user, 2 for Windows user, 4 for group, 16 for LDAP user, 0x0000FFFF for all.
admin.getPrincipalList(static_cast<TEAdmin::PRINCIPAL_LIST_TYPE>(nType), container);
if( container.size() > 0 )
{
cout << "\nPrincipals obtained: " << endl;
for (long i=0; i<container.size(); i++)
{
const TEPrincipalInfo* info = static_cast<const TEPrincipalInfo*>(container[i]);
cout << endl;
cout << "Class Name: " << info->getClassName() << endl;
cout << "Number of Roles: " << info->RoleCount() << endl;
if (nType == TEAdmin::PRINCIPAL_USERANDPASSWORD )
{
const TEUserAndPasswordInfo *pUser =
static_cast<const TEUserAndPasswordInfo*>(container[i]);
cout << "User Name: " << pUser->getUserName() << endl;
cout << "User ID : " << pUser->getPrincipalId() << endl;
}
else if (nType == TEAdmin::PRINCIPAL_LDAP )
{
const TELDAPPrincipalInfo *pLDAPUser =
static_cast<const TELDAPPrincipalInfo*>(container[i]);
cout << "LDAP user Name: " << pLDAPUser->getName() << endl;
cout << "LDAP user ID : " << pLDAPUser->getPrincipalId() << endl;
}
else if (nType == TEAdmin::PRINCIPAL_WINDOWS )
{
const TEWindowsPrincipalInfo *pUser =
static_cast<const TEWindowsPrincipalInfo*>(container[i]);
cout << "Windows user Name: " << pUser->getUserName() << endl;
cout << "Windows user ID : " << pUser->getPrincipalId() << endl;
}
else if (nType == TEAdmin::PRINCIPAL_GROUP )
{
const TEGroupInfo *pGroup =
static_cast<const TEGroupInfo*>(container[i]);
cout << "Group Name: " << pGroup->getGroupName() << endl;
cout << "Group ID : " << pGroup->getPrincipalId() << endl;
}
else
cout << "Unknown principal type.";
cout << endl;
}
}
else
cout << "\nNo principal was 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;
}
Output
Connected to localhost as admin
Principals obtained:
Class Name: TEUserAndPasswordInfo
Number of Roles: 1
User Name: admin
User ID : 1
Class Name: TEUserAndPasswordInfo
Number of Roles: 3
User Name: alice
User ID : 1001
Class Name: TEUserAndPasswordInfo
Number of Roles: 3
User Name: bob
User ID : 1002
Class Name: TEUserAndPasswordInfo
Number of Roles: 4
User Name: ls
User ID : 3001
disconnected from localhost