Dynamics 365 Business Central: How to quickly check if the user has the SUPER permissions set via AL (Best practice)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to share a mini tip when developing, how to quickly check if the user has the SUPER permissions set.

This is a very simple customization for permission management, such as some key settings pages, only users with Super permission set can access.

I prepared two test users.

Admin: Super permisison set

YZHU: Only have D365 BUS FULL ACCESS

Next, as you might know, a more general way is to use table 2000000053 “Access Control”

If the user can be found in this table with SUPER Role ID, then he has the SUPER permission set.

So, we can do the following. (If you also deal with different companies, please add another filter for the company here)

Source Code:

pageextension 50111 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage()
    var
        AccessControl: Record "Access Control";
    begin
        AccessControl.Reset();
        AccessControl.SetRange("User Security ID", UserSecurityId());
        AccessControl.SetRange("Role ID", 'SUPER');
        if not AccessControl.IsEmpty then
            Message('The user has the SUPER permissions set')
        else
            Message('The user does not have the SUPER permissions set');
    end;
}

There is nothing wrong with this, but please note that this is not the best practise. In fact, Microsoft has prepared a method for us in codeunit 152 “User Permissions”.

procedure IsSuper(UserSecurityId: Guid): Boolean: Checks whether the user has the SUPER permissions set.

We only need one line of code to check whether the user has Super permissions set. Isn’t it very easy.😁

Source Code:

pageextension 50111 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage()
    var
        UserPermissions: Codeunit "User Permissions";
    begin
        if UserPermissions.IsSuper(UserSecurityId()) then
            Message('The user has the SUPER permissions set')
        else
            Message('The user does not have the SUPER permissions set');
    end;
}

Test video:

PS: A very good example can be found in page 9192 “Company Creation Wizard”

END

Hope this will help.

Thanks for reading.

ZHU

コメント

タイトルとURLをコピーしました