Remove a Role from a Principal in Cpp
The following code sample illustrates how to remove a role from a principal.
Code Sample
See the C++ environment settings for details on setting the project environment.
<cpp_admin_role_removefrom.cpp>
/*****************************************************************************************
*
* cpp_admin_role_removefrom.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"
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(3001);
admin.getPrincipal(user, user);
cout << "Principal info: " << endl;
cout << "User ID = " << user.getPrincipalId() << endl;
cout << "User Name = " << user.getUserName() << endl;
cout << "Number of role = " << user.RoleCount() << endl;
// Prepare the role ID's to be removed
TEObjectContainer roleIDs;
TEUInt32 id(1);
roleIDs.add(&id);
id = TEUInt32(2);
roleIDs.add(&id);
id = TEUInt32(3);
roleIDs.add(&id);
// Remove roles from the principal
admin.removeRoles(user, roleIDs);
cout << endl;
cout << "Principal was updated" << endl;
// Get the updated info
admin.getPrincipal(user, user);
cout << "User ID = " << user.getPrincipalId() << endl;
cout << "User Name = " << user.getUserName() << endl;
cout << "Number of role = " << user.RoleCount() << 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
Principal info:
User ID = 3001
User Name = ls
Number of role = 5
Principal was updated
User ID = 3001
User Name = ls
Number of role = 2
disconnected from localhost