Monday, 10 April 2017

How to add user permission for Add-on specific objects

If you have created a new Add-on objects and now you need to execute these objects in the customer environment(Ex. Page 12103540, Page 12103560).
So, these object id's which are not a part of Dynamics NAV needs a permission to execute or modify or insert in some other workstations.

So, here is the sample code, permissions in the below code are assigned to TableData(you can change it to page or any other object based on your requirement).

Global variables.
Name                            DataType      Subtype            Length
UserRole                          Record Permission Set
Permission                       Record Permission
AllObj                             Record AllObj
User                               Record User
WindowsLogin                  Record User
WindowsAccessControl Record     Access Control

1. Assign the new user Role ID and User Role Name
2. If not user role inserted then sort the record based on object and object id's.
3. Fetch the Permission Role ID(insert,modify,delete,execute) from permission set table and assign it to permission table for the object id's which you need to assign permission.

Example:

UserRole."Role ID" := 'Addon1';
UserRole.Name := 'Required for all user';
IF NOT UserRole.INSERT THEN ;
AllObj.RESET;
AllObj.SETRANGE("Object Type",AllObj."Object Type"::TableData);
AllObj.SETFILTER(
  "Object ID",
  '12103560..12103580');
IF AllObj.FIND('-') THEN
  REPEAT
    Permission."Role ID" := UserRole."Role ID";
    Permission."Object Type" := AllObj."Object Type";
    Permission."Object ID" := AllObj."Object ID";

    Permission."Read Permission" := Permission."Read Permission"::Yes;
    IF AllObj."Object ID" IN [12103540, 12103560] THEN BEGIN
      Permission."Insert Permission" := Permission."Insert Permission"::Yes;
      Permission."Modify Permission" := Permission."Modify Permission"::Yes;
      Permission."Delete Permission" := Permission."Delete Permission"::Yes;
      Permission."Execute Permission" := Permission."Execute Permission"::Yes;
    END ELSE BEGIN
      Permission."Insert Permission" := Permission."Insert Permission"::" ";
      Permission."Modify Permission" := Permission."Modify Permission"::" ";
      Permission."Delete Permission" := Permission."Delete Permission"::" ";
      Permission."Execute Permission" := Permission."Execute Permission"::" ";
    END;
    IF NOT Permission.INSERT THEN ;
  UNTIL AllObj.NEXT = 0;