#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;
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);
}
static void assignRole(TEAdmin& admin, TEPrincipalInfo& principal, te_oid roleId)
{
TEOID id(roleId);
TEObjectContainer roleIds;
roleIds.add(&id);
admin.assignRoles(principal, roleIds);
}
int main()
{
string strServer = "localhost";
long nPort = 8888;
string strAdminUser = "admin";
string strAdminPswd = "password1A";
try
{
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 roleAdministrator = findRoleIdByName(admin, "Administrator");
te_oid roleEncryptor = findRoleIdByName(admin, "Encryptor");
TEUserAndPasswordInfo user;
user.setUserName("TestUser1");
user.setPassword("password1A", strAdminPswd);
admin.addPrincipal(user, user);
assignRole(admin, user, roleAdministrator);
cout << "Native user was added: " << endl;
cout << " User Name = " << user.getUserName() << endl;
cout << " User ID = " << user.getPrincipalId() << endl;
TELDAPPrincipalInfo ldapuser;
ldapuser.setName("cn=joe,o=emedna");
admin.addPrincipal(ldapuser, ldapuser);
assignRole(admin, ldapuser, roleEncryptor);
cout << "LDAP user was added: " << endl;
cout << " User Name = " << ldapuser.getName() << endl;
cout << " User ID = " << ldapuser.getPrincipalId() << endl;
TEWindowsPrincipalInfo winuser;
winuser.setUserName("SampleDomain/SampleUser");
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;
TEGroupInfo group;
group.setGroupName("TestGroup1");
admin.addPrincipal(group, group);
cout << "Group was added: " << endl;
cout << " Group Name = " << group.getGroupName() << endl;
cout << " Group ID = " << group.getPrincipalId() << endl;
admin.detachConnection();
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;
}