Name

sn_entitlement.UserRoleDao

Description

No description available

Script

/**
* Data access functions that return a single or many roleIds (sys_user_role.sys_id)
*/
var UserRoleDao = Class.create();
UserRoleDao.prototype = {
  initialize: function() {
      this._licenseFamilyDao = new sn_entitlement.LicenseFamilyDao();
  },

  /**
   * Retrieves the roleIds (sys_ids) associated to the subscriptionId (subscription_entitlement.sys_id) provided
   * filtered down to just the license_role_type.sys_id values provided
   *
   * @param {guid} subscriptionId The subscription_entitlement.sys_id to filter the sys_user_role records by
   * @param {array} licenseRoleTypeNames The license_role_type.name values to limit the results by
   * @param {bool} oobRolesOnly A boolean flag that filters the roles to out-of-box roles when true.
   * @return {array} An array of sys_user_role.sys_id values
   */
  getUserRoleIdsBySubscriptionIdAndLicenseRoleTypeNames: function(subscriptionId, licenseRoleTypeNames, oobRolesOnly) {
      const licenseFamilyIds = this._licenseFamilyDao.getLicenseFamilyIdsBySubscriptionId(subscriptionId);
      const licensableRoleNames = this._getUserRoleNamesByLicenseFamilyIdsAndLicenseRoleTypeNames(licenseFamilyIds, licenseRoleTypeNames, oobRolesOnly);

      const gr = new GlideRecord('sys_user_role');
      gr.addQuery('name', 'IN', licensableRoleNames); //Note: Had to query by name since that is what is referenced in the DB view
      gr.query();

      let ids = [];
      while (gr.next())
          ids.push(gr.getUniqueValue());
      return ids;
  },

  /**
   * Retrieves the sys_user_role.name values that belong to the identified license family ids with matching
   * license role type name.
   *
   * @param {array} licenseFamilyIds The license_family.sys_id values to filter by
   * @param {array} licenseRoleTypeNames The role type names to filter by
   * @param {bool} oobRolesOnly A boolean flag that filters the roles to out-of-box roles when true.
   * @returns {array} An array of role names (the reference to sys_user_role is by name instead of by id)
   */
  _getUserRoleNamesByLicenseFamilyIdsAndLicenseRoleTypeNames: function(licenseFamilyIds, licenseRoleTypeNames, oobRolesOnly) {
      const gr = new GlideRecord('license_role');
      gr.addQuery('application.sys_id', 'IN', licenseFamilyIds);
      gr.addQuery('license_role_type', 'IN', licenseRoleTypeNames);

      if (oobRolesOnly)
          gr.addQuery('category', 0); // 0 is the value for "ServiceNow OOB"; 1 is the value for "Custom"

      gr.query();

      const names = new Set();
      while (gr.next())
          names.add(String(gr.sys_user_role));
      return Array.from(names);
  },

  /**
   * Retrieves all role IDs associated with the subscription identified even if the role is not
   * a metered role for the subscription
   *
   * @param {guid} subscriptionId The subscription ID to get roles for
   * @param {bool} oobRolesOnly A flag that indicates if only out-of-box roles should be retrieved (when true) or all roles (when false)
   * @returns {array} An array of role IDs associated with the subscription
   */
  getRoleIdsBySubscriptionId: function(subscriptionId, oobRolesOnly) {
      const licenseFamilyIds = this._licenseFamilyDao.getLicenseFamilyIdsBySubscriptionId(subscriptionId);
      const roleNames = this._getUserRoleNamesByLicenseFamilyIds(licenseFamilyIds, oobRolesOnly);

      const gr = new GlideRecord('sys_user_role');
      gr.addQuery('name', 'IN', roleNames);
      gr.query();

      let ids = [];
      while (gr.next())
          ids.push(gr.getUniqueValue());
      return ids;
  },

  /**
   * Retrieves the sys_user_role.name values that belong to the identified license family ids
   *
   * @param {array} licenseFamilyIds The license_family.sys_id values to filter by
   * @param {bool} oobRolesOnly A boolean flag that filters the roles to out-of-box roles when true.
   * @returns {array} An array of role names (the reference to sys_user_role is by name instead of by id)
   */
  _getUserRoleNamesByLicenseFamilyIds: function(licenseFamilyIds, oobRolesOnly) {
      const gr = new GlideRecord('license_role');
      gr.addQuery('application.sys_id', 'IN', licenseFamilyIds);

      if (oobRolesOnly)
          gr.addQuery('category', 0); // 0 is the value for "ServiceNow OOB"; 1 is the value for "Custom"

      gr.query();

      const names = new Set();
      while (gr.next())
          names.add(String(gr.sys_user_role));
      return Array.from(names);
  },

  /**
   * Retrieves the roleIds (sys_user_role.sys_id) values assigned to a user
   *
   * @param {guid} userId The sys_user.sys_id for the user to retrieve role information for
   * @returns {array} An array of sys_user_role.sys_id values
   */
  getUserRoleIdsByUserId: function(userId) {
      // Note: It seems adding users to groups projects the roles into sys_user_has_role, so group membership
      //    does not need to be checked in this query.
      const gr = new GlideRecord('sys_user_has_role');
      gr.addQuery('user.sys_id', userId);
      gr.query();

      const ids = new Set();
      while (gr.next())
          ids.add(String(gr.role.sys_id));
      return Array.from(ids);
  },

  /**
   * Retrieves the roleIds (sys_user_role.sys_id) values assigned to a user
   *
   * @param {guid} userId The sys_user.sys_id for the user to retrieve role information for
   * @param {array} meteredRoleIds An array of sys_user_role.sys_id values to limit the results by
   * @returns {array} An array of sys_user_role.sys_id values
   */
  getUserRoleIdsByUserIdAndIsMetered: function(userId, meteredRoleIds) {
      // Note: It seems adding users to groups projects the roles into sys_user_has_role, so group membership
      //    does not need to be checked in this query.
      const gr = new GlideRecord('sys_user_has_role');
      gr.addQuery('user.sys_id', userId);
      gr.addQuery('role.sys_id', 'IN', meteredRoleIds);
      gr.query();

      const ids = new Set();
      while (gr.next())
          ids.add(String(gr.role.sys_id));
      return Array.from(ids);
  },

  /**
   * Retrieves the roleIds (sys_user_role.sys_id) values assigned to a group
   *
   * @param {guid} groupId the sys_user_group.sys_id for the group to retrieve role information for
   * @returns {array} An array of sys_user_role.sys_id values
   */
  getUserRoleIdsByGroupId: function(groupId) {
      const gr = new GlideRecord('sys_group_has_role');
      gr.addQuery('group.sys_id', groupId);
      gr.addQuery('group.active', true);
      gr.query();
      let ids = [];
      while (gr.next())
          ids.push(String(gr.role.sys_id));
      return ids;
  },

  /**
   * Retrieves the roleIds (sys_user_role.sys_id) values assigned to a group
   *
   * @param {guid} groupId the sys_user_group.sys_id for the group to retrieve role information for
   * @param {array} meteredRoleIds An array of sys_user_role.sys_id values to limit the results by
   * @returns {array} An array of sys_user_role.sys_id values
   */
  getUserRoleIdsByGroupIdAndIsMetered: function(groupId, meteredRoleIds) {
      const gr = new GlideRecord('sys_group_has_role');
      gr.addQuery('group.sys_id', groupId);
      gr.addQuery('group.active', true);
      gr.addQuery('role', 'IN', meteredRoleIds);
      gr.query();
      let ids = [];
      while (gr.next())
          ids.push(String(gr.role.sys_id));
      return ids;
  },

  type: 'UserRoleDao'
};

Sys ID

adc585e8ff102110468365d7d3b8fe5e

Offical Documentation

Official Docs: