Skip to main content

cpp_admin_principal_add.cpp

/*****************************************************************************************
*
* cpp_admin_principal_add.cpp
*
* Note: See Tricryption Engine SDK for details on how to set the project environment.
*
******************************************************************************************/

#include<string>
#include<iostream>
#include<stdexcept>
#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;


//---------------------------------------------------------------------------------------
// Resolve a role id by NAME.
//
// Role ids are assigned in setup order and are NOT a fixed part of the API -- on a stock
// Key Server "Encryptor" is id 3, not 2 (id 2 is "Desktop Manager"). Always look the role
// up by name rather than hardcoding a number.
//---------------------------------------------------------------------------------------
static te_oid findRoleIdByName(TEAdmin& admin, const string& strRoleName)
{
TEObjectContainer roles;
admin.getRoleList(roles);

for (int i = 0; i < roles.size(); i++)
{
const TEUserRole* role = static_cast<const TEUserRole*>(roles[i]);
if (role->getRoleName() == strRoleName)
return role->getRoleId();
}

throw runtime_error("role not found: " + strRoleName);
}


//---------------------------------------------------------------------------------------
// Assign roles to an ALREADY-CREATED principal.
//
// addPrincipal does not persist roles staged on the info object with addRole(); those are
// silently dropped. Roles must be assigned in a separate assignRoles call after the
// principal exists.
//---------------------------------------------------------------------------------------
static void assignRole(TEAdmin& admin, TEPrincipalInfo& principal, te_oid roleId)
{
TEOID id(roleId); // TEOID, not TEUInt32: role ids are 64-bit
TEObjectContainer roleIds;
roleIds.add(&id);

admin.assignRoles(principal, roleIds);
}


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->());


// Resolve the role ids this sample assigns, by name.
te_oid roleAdministrator = findRoleIdByName(admin, "Administrator");
te_oid roleEncryptor = findRoleIdByName(admin, "Encryptor");


// Create a native user to be added to the TE system.
TEUserAndPasswordInfo user;
user.setUserName("TestUser1"); // set user name
user.setPassword("password1A", strAdminPswd); // set password, 2nd parameter is the current user's password.

// Add native user, then assign its role (roles cannot be set before the add).
admin.addPrincipal(user, user); // 1st parameter is input, 2nd parameter is output.
assignRole(admin, user, roleAdministrator);
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");

// Add LDAP user, then assign its role.
admin.addPrincipal(ldapuser, ldapuser); // 1st parameter is input, 2nd parameter is output.
assignRole(admin, ldapuser, roleEncryptor);
cout << "LDAP user was added: " << endl;
cout << " User Name = " << ldapuser.getName() << endl;
cout << " User ID = " << ldapuser.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 '\'

// Add Windows user, then assign its role.
admin.addPrincipal(winuser, winuser);
assignRole(admin, winuser, roleEncryptor);
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 (exception &e)
{
cout << e.what() << endl;
}
catch (...)
{
cout << "Unexpected error" << endl;
}

return 0;
}