The user session service

The user session service exposes information about the current user and their session. Although this service is categorized by Microsoft as a system service, technically it isn't. Unlike other system services, the business logic is contained in a service class in the AOT and exposed using a basic port. Consequently, it is also possible to expose this service using an enhanced port, allowing you to further customize the service.

The user session has the following operations:

  • GetUserSessionInfo: This returns information about the current session in the form of an instance of the UserSessionInfo class containing the following information: language, currency, company, company time zone, user-preferred time zone, preferred calendar, user ID, whether the user is a system admin, and the locale name.
  • GetAccessRights: This returns a collection of the type AccessRight that contains the permissions which the user has on the items that were provided as parameters, such as tables, fields, and menu items.
  • ApplyTimeZone: This executes the DateTimeUtil::applyTimeZoneOffset() method and thereby offsets the utcdatetime value by the amount specified in the timezone parameter.
  • RemoveTimeZone: This executes the DateTimeUtil::removeTimeZoneOffset() method and thereby removes the offset specified by the timezone parameter from the utcdatetime value.

We will use the GetUserSessionInfo and GetAccessRights operations in the following scenario to demonstrate how to use this service.

Retrieving user information

The functionalities that we will add to the form are as follows:

  • A button is added to retrieve the user session information
  • A ListBox control is added to display the user session information
  • General user information such as the company and language is retrieved
  • Permissions are retrieved for the query data source

To enable this functionality, override the cmdUserSessionInfo_Click() method using the following code:

private void cmdUserSessionInfo_Click(object sender, EventArgs e)
{
    // Create an instance of the usersession client
    UserSessionServiceClient client = new UserSessionServiceClient();

    // Get session information
    UserSessionInfo sessionInfo = client.GetUserSessionInfo(null);
    
    // Put all of the information in the listbox
    lboUserSessionInfo.Items.Clear();
    lboUserSessionInfo.Items.Add("User : " + sessionInfo.UserId);
    lboUserSessionInfo.Items.Add("Company : " + sessionInfo.Company);
    lboUserSessionInfo.Items.Add("Language : " + sessionInfo.AXLanguage);
    lboUserSessionInfo.Items.Add("Currency : " + sessionInfo.CurrencyInfo.CurrencyCode);
    lboUserSessionInfo.Items.Add("Administrator : " + sessionInfo.IsSysAdmin);

    // Create an access control item for the main table of the 
    // selected query
    AccessControlledItemKey key = new AccessControlledItemKey()
    {
        ItemType = AccessControlledType.Table,
        ItemName = cboAxQueryName.Text
    };

    // Create a list with the item in it
    List<AccessControlledItemKey> keys = new List<AccessControlledItemKey>();
    keys.Add(key);

    // Now request the effective access right for this user session on 
    // the item
    List<AccessRight> accessRights = client.GetAccessRights(null, keys);

    // Get the access rights
    AccessRight accessRight = accessRights.First();

    lboUserSessionInfo.Items.Add("Query access right : " + accessRight.ToString());
}

As you can see, we can divide the code into the following two large parts:

  • First, we use the GetUserSessionInfo operation to retrieve the session information and use it to add items to the listbox.
  • Next, we create a new list object of the type AccessControlledItemKey and add an item specifying the table name. Then, we use the GetAccessRights() method to retrieve the permissions that the user has on this table and then add them to the list.

To test the code, simply click on the Session Information button and the listbox should be filled with the session information, as shown in the following screenshot:

Retrieving user information
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.77.153