Wednesday, August 2, 2023

Code to get user's assigning roles and removing roles in d365 X++

 My requirement is to get the user assigned roles and removed roles information.

In details :

I'm a Admin user . I've added few roles to a user named "ARNIE".

Added Roles :


Removed Roles :

Removed  the above added one role "Time registration user role".



Code to get the Added Roles :

[ExtensionOf(formcontrolstr(syssecuseraddroles,commandButtonOK))]

internal final class DaxSyssecAddRoles_Extension

{

   public void clicked()

   {

       SecurityRole                 securityRole;

       DaxRoleAssignmentInfoLog     daxRolesAssignmentinfoLog;//custom table.

       UserInfo                     userInfo;

       MultiSelectionHelper         helper;

       SecurityUserRole             securityUserRole;

       FormControl                  callerButton    = any2Object(this) as FormControl;

       FormRun                      formRun         = callerButton.formRun();

       FormDataSource               formDataSource  = formRun.datasource(1);

       if (element.args().dataset() == tableNum(UserInfo))

       {

           userInfo  = element.args().record();

       }

       next Clicked();

       securityRole = formDataSource.getFirst(true);

       while(securityRole)

       {

           ttsbegin;

           daxRolesAssignmentinfoLog.ChangedBy     = curUserId();

           daxRolesAssignmentinfoLog.UserId        = userInfo.id;

           daxRolesAssignmentinfoLog.AssignedRole  = securityRole.Name;

           daxRolesAssignmentinfoLog.SecRecId      = securityRole.recid;

           daxRolesAssignmentinfoLog.Organisation  = curExt();

           daxRolesAssignmentinfoLog.insert();

           ttscommit;

           securityRole = formdataSource.getNext();

       }

   }

}

.......................................................

Code to get current removing role record :

[ExtensionOf(classstr(SysUserInfoManagementForm))]

internal final class Dax_SysUserInfoManagementForm_Extension

{

  public static SysSecRole selectedRoleloc;

   public void TreeRolesSelectionChanged(FormTreeItem _newItem)

   {

       

       next TreeRolesSelectionChanged(_newItem);

       selectedRoleloc = tree.getData(_newItem);

   }

   public static  SysSecRole parmSysSecRole()

   {

       return selectedRoleloc;

   }

}

.................................................................

Code to update the custom table when role is removed :

[ExtensionOf(formControlStr(SysUserManagement,btnUserRoleRemove))]

public final class DaxSysSecAddUser_Extension

{

   public void clicked()

   {

       SysSecRole                      secRole;

       DaxRoleAssignmentInfoLog        daxRoleAssignmentInfoLog;

       secRole = SysUserInfoManagementForm::parmSysSecRole();

       next  clicked();

       select forupdate daxRoleAssignmentInfoLog

           where daxRoleAssignmentInfoLog.SecRecId == secRole.getRecId();

       if(daxRoleAssignmentInfoLog)

       {

           ttsbegin;

           daxRoleAssignmentInfoLog.RemovedRole = daxRoleAssignmentInfoLog.AssignedRole;

           daxRoleAssignmentInfoLog.AssignedRole = "";

           daxRoleAssignmentInfoLog.update();

           ttscommit;

       }

   }

}

...............................................


Output :










No comments:

Post a Comment