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:
namespace Aria.Osp
{
public interface IUserRepository
{
User GetUser(string userId);
}
}
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:
namespace Aria.Osp
{
public class User
{
public string FullName { get; set; }
}
}
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:
using System;
using System.Linq;
using System.Xml.Linq;
using VMS.OSP.Services;
namespace Aria.Osp
{
// The XML string returned by the OSP service should follow the format:
//
// <?xml version="1.0" encoding="UTF-8"?>
// <users>
// <user>
// <userid>id</userid>
// <username>First Last</username>
// </user>
// </users>
//
// Note: Only the relevant elements are shown for the sample user,
// so there may be other elements, such as "languageid".
public class UserRepository : IUserRepository
{
public User GetUser(string userId)
{
return new User {FullName = GetUserFullName(userId)};
}
private string GetUserFullName(string userId)
{
return GetUserName(FindUserXml(userId, GetUsersXml(LoadUsersXml())));
}
private string GetUserName(XElement user)
{
return user.Element("username")?.Value
?? throw new Exception("The user has no username.");
}
private XElement FindUserXml(string userId, XElement users)
{
return users.Elements().FirstOrDefault(user => HasUserId(user, userId))
?? throw new Exception($"There is no user with ID {userId}.");
}
private bool HasUserId(XElement user, string userId)
{
return user.Element("userid")?.Value == userId;
}
private XElement GetUsersXml(string usersXml)
{
// After parsing, the element returned is "users"
return XElement.Parse(usersXml);
}
private string LoadUsersXml()
{
var ospClientFactory = new OSPClientLibraryFactory();
var ospClientServices = ospClientFactory.CreateOspClientServices();
return ospClientServices.Security.GetUsers();
}
}
}
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).
Comments