Get a Principal in Cpp
The following code sample illustrates how to get a principal.
Code Sample
See the C++ environment settings for details on setting the project environment.
<cpp_admin_principal_get.cpp>
/*****************************************************************************************
*
* cpp_admin_principal_get.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.getPrincipal(user, user);
cout << "User ID = " << user.getPrincipalId() << endl;
cout << "User Name = " << user.getUserName() << endl;
// Get a LDAP user
TELDAPPrincipalInfo ldapuser;
ldapuser.setPrincipalId(10008);
admin.getPrincipal(ldapuser, ldapuser);
cout << "LDAP User ID = " << ldapuser.getPrincipalId() << endl;
cout << "LDAP User Name = " << ldapuser.getName() << endl;
// Get a Windows user
TEWindowsPrincipalInfo winuser;
winuser.setPrincipalId(10009);
admin.getPrincipal(winuser, winuser);
cout << "Windows user ID = " << winuser.getPrincipalId() << endl;
cout << "Windows user Name = " << winuser.getUserName() << endl;
// Get a group
TEGroupInfo group;
group.setPrincipalId(10010);
admin.getPrincipal(group, 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