Name

global.PPMTablePermissionMapper

Description

Given a list of tables returns whether the given user has CRUD Access on it or not

Script

/*
* Planning Console Access Helper Base helps derving the CRUD access for the table(s)
* Methods:
* access (table, sysId) : checks the read access for the record
* accessConfig(tableObj) : generate crud access for the table/sys_ids provided
*/
var PPMTablePermissionMapper = Class.create();

PPMTablePermissionMapper.prototype = {
  initialize: function() {
  },

  glideRecord: function(table, sysId) {
      if(JSUtil.notNil(table)) {
          var gr = new GlideRecord(table);
          if(JSUtil.notNil(sysId)) {
              gr.get(sysId);
          } else {
              gr.initialize();
          }
          return gr;
      }
  },

  getPermissions: function(tables, entityId) {
      var perms = {};
      for ( var i in tables ) {
          var tableAccess = {};
          if(gs.tableExists(tables[i])) {
  			if (tables[i] == 'planned_task_rel_planned_task') {
  				var isProjectManager = gs.hasRole("project_manager");
  				tableAccess['delete'] = isProjectManager;
  				tableAccess.create = isProjectManager;
  				tableAccess.read = isProjectManager;
  				tableAccess.update = isProjectManager;
  				perms[tables[i]] = tableAccess;
  				continue;
  				}
              tableAccess['delete'] = this.canDelete(this.glideRecord(tables[i]));
              tableAccess.create = this.canCreate(this.glideRecord(tables[i]));
              tableAccess.read = this.canRead(this.glideRecord(tables[i], entityId));
              tableAccess.update = true; //this.canWrite(this.glideRecord(tables[i]));
          } else {
              tableAccess['delete'] = false;
              tableAccess.create = false;
              tableAccess.read = false;
              tableAccess.update = false;
          }
          perms[tables[i]] = tableAccess;
      }
      return perms;
  },
  
  // tableObj Defn: {table1: sys_id1, table2: sys_id2, table3: sys_id3}
  getPermissionsWithSysId: function(tableObj) {
      var access  = {};
      if( JSUtil.notNil(tableObj)) {
          for(var table in tableObj) {
              var tableAccess = { 'create': this.canCreate(this.glideRecord(table, tableObj[table])),
                                  'read': this.canRead(this.glideRecord(table, tableObj[table])),
                                  'update': this.canWrite(this.glideRecord(table, tableObj[table])),
                                  'delete': this.canDelete(this.glideRecord(table, tableObj[table]))
                              };
              access[table] = tableAccess;
          }
      }
      return access;
  },

  canCreate: function(gr) {
      return gr.canCreate();
  },

  canRead: function(gr) {
      return gr.canRead();
  },

  canWrite: function(gr) {
      return gr.canWrite();
  },

  canDelete: function(gr) {
      return gr.canDelete();
  },

  access: function(table, sysId) {
      return this.canRead(this.glideRecord(table, sysId));
  },

  type: 'PPMTablePermissionMapper'
};

Sys ID

0e8a3b519f321200598a5bb0657fcfa1

Offical Documentation

Official Docs: