Add a Principal
The following code sample illustrates how to add a principal to the TE system.
Code Sample
See the C++ environment settings for details on setting the project environment.
<cpp_admin_principal_add.cpp>
/*****************************************************************************************
*
* cpp_admin_principal_add.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"
#include "teunixprincipal.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->());
// Create a native user to be added to the TE system.
TEUserAndPasswordInfo user;
user.setUserName("TestUser1"); // set user name
user.setPassword("password", strAdminPswd); // set password, 2nd parameter is the current user's password.
user.addRole(1); // add ADMINISTRATOR role which is represented by 1
// Add native user.
admin.addPrincipal(user, user); // 1st parameter is input, 2nd parameter is output.
cout << "Native user was added: " << endl;
cout << " User Name = " << user.getUserName() << endl;
cout << " User ID = " << user.getPrincipalId() << endl;
// Create a LDAP user to be added to the TE system.
TELDAPPrincipalInfo ldapuser;
ldapuser.setName("cn=joe,o=emedna");
ldapuser.addRole(2); // add ENCRYPTOR role which is represented by 2
// Add LDAP user.
admin.addPrincipal(ldapuser, ldapuser); // 1st parameter is input, 2nd parameter is output.
cout << "LDAP user was added: " << endl;
cout << " User Name = " << ldapuser.getName() << endl;
cout << " User ID = " << ldapuser.getPrincipalId() << endl;
// Create a NIS user to be added to the TE system.
TEUnixPrincipalInfo nisuser(TEUnixPrincipalInfo::NIS_PRINCIPAL);
nisuser.setName("testuser");
nisuser.addRole(2); // add ENCRYPTOR role which is represented by 2
// Add NIS user.
admin.addPrincipal(nisuser, nisuser); // 1st parameter is input, 2nd parameter is output.
cout << "NIS user was added: " << endl;
cout << " User Name = " << nisuser.getName() << endl;
cout << " User ID = " << nisuser.getPrincipalId() << endl;
// Create a Windows user to be added to the TE system.
TEWindowsPrincipalInfo winuser;
winuser.setUserName("SampleDomain/SampleUser"); // set name, seperate wiht '/' but not '\'
winuser.addRole(2); // add ENCRYPTOR role which is represented by 2
// Add Windows user.
admin.addPrincipal(winuser, winuser);
cout << "Windows user was added: " << endl;
cout << " User Name = " << winuser.getUserName() << endl;
cout << " User ID = " << winuser.getPrincipalId() << endl;
// Create a group to be added to the TE system.
TEGroupInfo group;
group.setGroupName("TestGroup1"); // set group name
// Add group.
admin.addPrincipal(group, group);
cout << "Group was added: " << endl;
cout << " Group Name = " << group.getGroupName() << endl;
cout << " Group ID = " << group.getPrincipalId() << 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
Native user was added:
User Name = TestUser1
User ID = 10007
LDAP user was added:
User Name = cn=joe,o=emedna
User ID = 10008
NIS user was added:
User Name = testuser
User ID = 10009
Windows user was added:
User Name = SampleDomain/SampleUser
User ID = 100010
Group was added:
Group Name = TestGroup1
Group ID = 10011
Disconnected from localhost