Adding and removing users in a group
You can add users to a group and remove users from a group by creating an AdminSession and then logging in as an admin.
The following code examples illustrates adding and removing users from groups.
Perl
use CQPerlExt;
$DEBUG = 1;
sub ShowUsersInGroups() {
local($Tag) = shift;
my($GCol) = $adminSession->GetGroups();
my($GCount) = $GCol->Count();
if ($GCount == 0) {
print "\n$Tag, there are no groups.\n\n";
}
else {
print "\n$Tag, there " .
(($GCount == 1) ? "is $GCount group.\n":"are $GCount groups.\n");
print "Group Users in group\n" .
"========== ==========================================\n";
for ($i = 0; $i < $GCount; $i++) {
my($GObj) = $GCol->Item($i);
printf("%-10.10s ", $GObj->GetName());
# Display the list of users in the group...
my($UCol) = $GObj->GetUsers();
if ($UCol->Count() == 0) {
print "(none)\n";
}
else {
for (my($u) = 0; $u < $UCol->Count(); $u++) {
my($UObj) = $UCol->Item($u);
print ($UObj->GetName() . " ");
}
print ("\n");
}
}
print "\n";
}
}
# Initiate an admin session and log on as "admin"...
$adminSession= CQAdminSession::Build()
or die "Error creating AdminSession object.\n";
print "Admin session create OK\n" if ($DEBUG);
$adminSession->Logon("admin", "", "LOCALTEST");
print "Back from logging in to admin session\n" if ($DEBUG);
# Display the users who are members of groups before doing anything...
&ShowUsersInGroups("At the beginning of this program's execution");
# Get handles for the 'QE' user and the 'MyGroup' group...
$GObj = $adminSession->GetGroup("MyGroup");
$UObj = $adminSession->GetUser("QE");
# Add user "QE" to the "MyGroup" group...
$GObj->AddUser($UObj);
# Display the users who are members of groups...
&ShowUsersInGroups("After adding user 'QE' to group 'MyGroup'");
# Remove the user from the group...
$GObj->RemoveUser($UObj);
# Display the users in groups now...
&ShowUsersInGroups("After removing user 'QE' from group 'MyGroup'");
CQAdminSession::Unbuild($adminSession);