Skip to main content

Get Principals with Query in Cpp

The following code sample illustrates how to get principals with query.


Code Sample

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


<cpp_admin_query.cpp>
/*****************************************************************************************
*
* cpp_admin_query.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_query.h"
#include "tequery.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
{
auto_ptr<TEConnection> conn = TEConnection::createInstance(TEConnection::SecureConnection); // Use SecureConnection here

// Connect to server
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;

TEAdminQuery adminQuery;
adminQuery.attachConnection(conn.operator->());

// Query native users
string strAttribute = "user";
string strPredicate = "%";

//TEQuery query(strAttribute, TEQuery::Equal, strPredicate);
TEQuery query(strAttribute, (TEQuery::ComparisonOperator)7, strPredicate); // 7 : like

TEObjectContainer contPrincipals; // container to hold principals
int nPrincipals = adminQuery.getPrincipalList("SRPPrincipalInfo", query, contPrincipals);

cout << "\nNumber of Principals obtained = " << nPrincipals << endl;
for (long i=0; i<contPrincipals.size(); i++)
{
cout << endl;
const TEPrincipalInfo* pPrcplInfo =
static_cast<const TEPrincipalInfo*>(contPrincipals[i]);

string strClassName = pPrcplInfo->getClassName();
cout << "Class name = " << strClassName << endl;
cout << "Number of Roles = " << pPrcplInfo->RoleCount() << endl;

if ( strClassName == "TEUserAndPasswordInfo" )
{
const TEUserAndPasswordInfo * pUser =
static_cast<const TEUserAndPasswordInfo *>( pPrcplInfo );

cout << "Native user ID = " << pUser->getPrincipalId() << endl;
cout << "Native user Name = " << pUser->getUserName() << endl;
}
else if ( strClassName == "TELDAPPrincipalInfo" )
{
const TELDAPPrincipalInfo * pLDAPuser =
static_cast<const TELDAPPrincipalInfo *>( pPrcplInfo );

cout << "LDAP user ID = " << pLDAPuser->getPrincipalId() << endl;
cout << "LDAP user Name = " << pLDAPuser->getName() << endl;
}
else if ( strClassName == "TEWindowsPrincipalInfo" )
{
const TEWindowsPrincipalInfo * pWinuser =
static_cast<const TEWindowsPrincipalInfo *>( pPrcplInfo );

cout << "Windows user ID = " << pWinuser->getPrincipalId() << endl;
cout << "Windows user Name = " << pWinuser->getUserName() << endl;
}
else if ( strClassName == "TEGroupInfo" )
{
const TEGroupInfo * pGroup = static_cast<const TEGroupInfo *>( pPrcplInfo );

cout << "Group ID = " << pGroup->getPrincipalId() << endl;
cout << "Group Name = " << pGroup->getGroupName() << endl;
}
else
cout << "Unknown class type !!!" << endl;

cout << endl;
}

adminQuery.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 at port 8888

Number of Principals obtained = 5

Class name = TEUserAndPasswordInfo
Number of Roles = 1
Native user ID = 1
Native user Name = admin

Class name = TEUserAndPasswordInfo
Number of Roles = 1
Native user ID = 3003
Native user Name = adminuser

Class name = TEUserAndPasswordInfo
Number of Roles = 3
Native user ID = 3002
Native user Name = alice

Class name = TEUserAndPasswordInfo
Number of Roles = 1
Native user ID = 3004
Native user Name = testuser

Class name = TEUserAndPasswordInfo
Number of Roles = 1
Native user ID = 3007
Native user Name = testuser2

Disconnected from localhost
Press any key to continue