Using the OSP to Get the Current User’s Name

There may be cases when you want to obtain the full name of the user currently running your script. For example, you may want to generate a report and include the user’s name on it.

Unfortunately, the Eclipse Scripting API (ESAPI) only provides the user’s ID. Here, I’m going to show you how to use another API to get the user’s full name. More specifically, we’re going to use Varian’s Oncology System Platform (OSP).

The OSP provides various services, including user management, licensing, and security. You have access to these services if you have the OSP API installed on your computer. I’ll show you in a bit how to tell whether you have the right files installed.

Assuming you already have an existing solution, add a new project. Since this project will contain OSP-related classes, you can name it something like “Aria.Osp.”

Add references to the following assemblies: VMS.OSP.ICommon.dll, VMS.OSP.Common.dll, VMS.OSP.IServices.dll, and VMS.OSP.Services.dll. On my computer, these are located in C:\Windows\Microsoft.NET\assembly\GAC_MSIL, under the directory with the same name as the assembly. If you can’t find these assemblies anywhere, you probably don’t have the OSP installed.

Add an interface named “IUserRepository.cs” with the following contents:

This interface simply defines the method GetUser, which takes the user ID and returns an object of type User. We’ll create the User class next.

Add a class named “User.cs” with the following contents:

This class simply contains the name of the user. We’ll obtain the user’s name in our implementation of the IUserRepository interface.

Add a class named “UserRepository.cs” with the following contents:

In this class, we’re using the OSP security service (see LoadUsersXml method). This service has a method to obtain all the users in the system as an XML string. It includes the user’s ID and name, so we’re also parsing the XML to pull out the user’s name.

That’s it! Now you can instantiate the UserRepository class, and call the GetUser method to obtain the User (and therefore the full name) of the user you’re interested in.

You may be asking yourself why didn’t we just return the user’s name directly instead of wrapping it in a User class. We certainly could have, but I wanted the flexibility of being able to add more properties to the User class in the future, such as the user ID or the group the user belongs to.

You may also be wondering whether we needed the IUserRepository interface. We didn’t really, but it makes it easier to create fake and mock objects in unit testing (see Mock object).

Leave a Reply

Your email address will not be published. Required fields are marked *