Add a Role in Cpp
The following code sample illustrates how to add a role to the TE system.
Code Sample
See the C++ environment settings for details on setting the project environment.
<cpp_admin_role_add.cpp>
/*****************************************************************************************
*
* cpp_admin_role_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"
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 role to be added to the
TEUserRole role;
role.setRoleName("TestRole1"); // set role name
role.addOperation(16777217); // add "Encrypt Data" operation to role
role.addOperation(16777218); // add "Decrypt Data" operation to role
// Add role to the TE system.
admin.addRole(role, role);
cout << "Role was added:" << endl;
cout << " Role Name = " << role.getRoleName() << endl;
cout << " Role ID = " << role.getRoleId() << endl;
cout << " Number of operations = " << role.OperationIdCount() << endl;
admin.detachConnection();
// Disconnect from server
conn->close();
cout << "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
Role was added:
Role Name = TestRole1
Role ID = 1001
Number of operations = 2
Disconnected from localhost