How to get the current user role in mobile?

I am trying to get the current user role in mobile but it is not working !
 

Terrasoft.sdk.Model.addBusinessRule("Activity", {
  name: "GetRoleFromtheUser",
  ruleType: Terrasoft.RuleTypes.Custom,
  triggeredByColumns: ["UsrVisitType","UsrTypeDeVisiteManager"],
  events: [Terrasoft.BusinessRuleEvents.ValueChanged, Terrasoft.BusinessRuleEvents.Save],
  executeFn: function(record, rule, column, customData, callbackConfig) {

    var myrecord = record; 
    var currentUser =Terrasoft.CurrentUserInfo.userId;
    var store = Ext.create('Terrasoft.store.BaseStore', {
      model: 'SMVwUserRoles'
    });
    var queryConfig = Ext.create('Terrasoft.QueryConfig', {
      columns: ['SMId', 'SMUserName', 'SMRoleName'],
      modelName: 'SMVwUserRoles'
    });
    store.loadPage(1, {
      queryConfig: queryConfig,

 

      filters: Ext.create('Terrasoft.Filter', {
        property: 'SMId',
        value: currentUser
      }),
      callback: function(records, operation, success) {
        var loadedRecord = records[0];
        if (loadedRecord) {
          var role = loadedRecord.get('SMRoleName');                                                                                                    
          if (role=='Sales Management') {                                                                                                                   
            record.changeProperty("UsrVisitType", {
                 hidden: true,
              readOnly: true
               });
          }else{
            record.changeProperty("UsrTypeDeVisiteManager", {
                 hidden: true
               });
          }
          }
        },
      });

Ext.callback(callbackConfig.success, callbackConfig.scope, [hidden,readOnly]); 
  }
});

Any idea please ?

Like 1

Like

1 comments

Hello!

You’re thinking in a right way and I would recommend you to change the logic of getting the role a bit. I tested this code on basic table SysAdminUnitInRole where we have a column SysAdminUnitRoleId

var currentUser =Terrasoft.CurrentUserInfo.userId;
var store = Ext.create('Terrasoft.store.BaseStore', {
  model: 'SysAdminUnitInRole'
});
var queryConfig = Ext.create('Terrasoft.QueryConfig', {
  columns: ['SysAdminUnitRoleId'],
  modelName: 'SysAdminUnitInRole'
});

Variable records structure looks like this:

 

And each class contains SysAdminUnitRoleId:

 

I used ID from loadedRecord.get('SysAdminUnitRoleId') and it works for me to see the role. You can do it in two ways:

  1. Create a loop for each “records” element and check if SysAdminUnitRoleId is equal to the user's current role id
  2. If you want to use “role=='Sales Management'” you can create a new object to display extensions of ID and role names.

P.S. Check if you added models to MobileApplicationManifestDefaultWorkplace:

"Models": { 
			"Activity": {
			"RequiredModels": ["SysAdminUnitInRole"],
			"ModelExtensions": ["UsrActivityModel"],
			"PagesExtensions": []
		}
	},

 

Show all comments