Skip to main content

Get a Principal by ID 2

The following code sample illustrates how to get a principal using an ID.


Code Samples

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


<cpp_admin_principal_getbyid_2.cpp>
/*****************************************************************************************
*
* cpp_admin_principal_getbyid_2.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->());

te_oid nID = 10007;
_TESTD auto_ptr<TEPrincipalInfo> pPrcplInfo = admin.getPrincipalByID(nID, admin.getSystemID());

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

if ( strClassName == "TEUserAndPasswordInfo" )
{
TEUserAndPasswordInfo * pUser =
static_cast<TEUserAndPasswordInfo *>( pPrcplInfo.operator->() );

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

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

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

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

Class name = TEUserAndPasswordInfo
Native user ID = 10007
Native user Name = TestUser1

Disconnected from localhost

SelfReference