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.
VBScript
' Initiate an admin session and log on as "admin"...
Dim adminSession ' an AdminSession object
Dim GCol ' a Groups collection object
Dim numGs ' a Long
Dim M ' a String
Dim g ' a Long
Dim GObj ' a Group Object
Dim UCol ' a Users collection object
Dim u ' a Long
Dim UObj ' a User object
set adminSession = CreateObject("ClearQuest.AdminSession")
adminSession.Logon "admin", "", "LOCALTEST"
Function ShowUsersInGroups(Tag)
set GCol = adminSession.Groups
numGs = GCol.Count
M = Tag & VbCrLf
If numGs = 0 Then
M = Tag & ", there are no groups" & VbCrLf
Else
M = Tag & ", there are " & numGs & " group(s)." & VbCrLf
For g = 0 to (numGs - 1)
set GObj = GCol.Item(g)
M = M & "Group=" & GObj.Name & " Users="
set UCol = GObj.Users
For u = 0 to (UCol.Count - 1)
set UObj = UCol.Item(u)
M = M & UObj.Name & " "
Next
M = M & VbCrLf
Next
End If
MsgBox M
End Function
' Display the starting state...
call ShowUsersInGroups("At the beginning of this program's execution")
' Get the Group and User object handles...
set GObj = adminSession.GetGroup("MyGroup")
set UObj = adminSession.GetUser("QE")
' Add user to group:
GObj.AddUser UObj
call ShowUsersInGroups("After adding user 'QE' to group 'MyGroup'")
' Remove user from group:
GObj.RemoveUser UObj
call ShowUsersInGroups("After removing user 'QE' from group 'MyGroup'")
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);