Getting session and database information
The following code from an external application illustrates some of the Session and DatabaseDesc methods. You need a session object to connect to the database. The session object allows you to get information about the database (such as the SQL connect string) and the user that is currently logged on. There are three steps to the process:
- Create the session object.
- Log on to the database.
- Do the tasks you want to do.
For more information, see the Session Object and the DatabaseDesc Object.
The following code prints out the information stored in the Session's DatabaseDesc object, as well as all the user-related information. This subroutine makes use of another routine called StdOut, which prints its arguments to a message box.
VBScript
' Connect via OLE to Rational
ClearQuest
Set session = CreateObject("CLEARQUEST.SESSION")
' login_name, password, and dbname are Strings that have
' been set elsewhere
session.UserLogon "joe","", dbname, AD_PRIVATE_SESSION, ""
Set dbDesc = session.GetSessionDatabase
StdOut "DB name = " & dbDesc.GetDatabaseName
StdOut "DB set name = " & dbDesc.GetDatabaseSetName
' You must log in with superuser privilege or an error will be
' generated by GetDatabaseConnectString
StdOut "DB connect string = " & dbDesc.GetDatabaseConnectString
StdOut "user login name = " & session.GetUserLoginName
StdOut "user full name = " & session.GetUserFullName
StdOut "user email = " & session.GetUserEmail
StdOut "user phone = " & session.GetUserPhone
StdOut "misc user info = " & session.GetUserMiscInfo
StdOut "user groups:"
Set userGroups = session.GetUserGroups
If IsArray(userGroups) Then
for each onename in userGroups
StdOut " group " & onename
next
End If
REM Start of Global Script StdOut
sub StdOut(Msg)
msgbox Msg
end sub
REM End of Global Script StdOut
Perl
use lib "E:\\Program Files\\Rational\\common\\lib";
use CQPerlExt;
$CQsession = CQSession::Build();
$CQsession->UserLogon("admin", "", "perl2", "");
$dbDesc = $CQsession->GetSessionDatabase();
print "DB name = ", $dbDesc->GetDatabaseName(), "\n";
print "DB set name = ", $dbDesc->GetDatabaseSetName(), "\n";
print "DB connect string = ", $dbDesc->GetDatabaseConnectString(), "\n";
print "User login name = ", $CQsession->GetUserLoginName(), "\n";
print "User full name = ", $CQsession->GetUserFullName(), "\n";
print "User email = ", $CQsession->GetUserEmail(), "\n";
print "User phone = ", $CQsession->GetUserPhone(), "\n";
print "Misc user info = ", $CQsession->GetUserMiscInfo(), "\n";
print "User groups: \n";
$userGroups = $CQsession->GetUserGroups();
if (!@$userGroups) {
#Code to handle if no user groups exist
print "This user does not belong to any groups\n";
}
else {
# print out all groups
foreach $groupname (@$userGroups) {
print "Group $groupname\n";
}
}
CQSession::Unbuild($CQsession);