Skip to main content

Get a Principal by ID 1

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

// Get a native user
TEUserAndPasswordInfo user;
user.setPrincipalId(10007);
admin.getPrincipalByID(user);
cout << "User ID = " << user.getPrincipalId() << endl;
cout << "User Name = " << user.getUserName() << endl;

// Get a LDAP user
TELDAPPrincipalInfo ldapuser;
ldapuser.setPrincipalId(10008);
admin.getPrincipalByID(ldapuser);
cout << "LDAP User ID = " << ldapuser.getPrincipalId() << endl;
cout << "LDAP User Name = " << ldapuser.getName() << endl;

// Create a Windows user
TEWindowsPrincipalInfo winuser;
winuser.setPrincipalId(10009);
admin.getPrincipalByID(winuser);
cout << "Windows user ID = " << winuser.getPrincipalId() << endl;
cout << "Windows user Name = " << winuser.getUserName() << endl;

// Create a group
TEGroupInfo group;
group.setPrincipalId(10010);
admin.getPrincipalByID(group);
cout << "Group ID = " << group.getPrincipalId() << endl;
cout << "Group Name = " << group.getGroupName() << 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 = 10007
User Name = TestUser1
LDAP User ID = 10008
LDAP User Name = cn=joe,o=emedna
Windows user ID = 10009
Windows user Name = SampleDomain/SampleUser
Group ID = 10010
Group Name = TestGroup1

Disconnected from localhost