gstackoverflow
gstackoverflow

Reputation: 37106

How to parse /getusereffectivepermissions sharepoint response in java?

I have sharepoint request like this:

/_api/web/getFolderByServerRelativeUrl('<folder-rel-url>')/ListItemAllFields/getusereffectivepermissions(@u)?@u='<account>'

The response is following:

<d:GetUserEffectivePermissions m:type="SP.BasePermissions">
    <d:High m:type="Edm.Int64">176</d:High>
    <d:Low m:type="Edm.Int64">138612833</d:Low>
</d:GetUserEffectivePermissions>

I don't understand what does it mean.

I've found several code examples how to parse it but this code written in javascript and uses special Object(SP.BasePermissions)

function parseBasePermissions(value)
{      
    let permissions = new SP.BasePermissions();
    permissions.initPropertiesFromJson(value);
    let result = {};
    for(var levelName in SP.PermissionKind.prototype) {
        if (SP.PermissionKind.hasOwnProperty(levelName)) {
            var permLevel = SP.PermissionKind.parse(levelName);
            if(permissions.has(permLevel))
                result[levelName] = true;
            else
                result[levelName] = false;
        }     
    }
    return result; 
}

How to parse it correctly? I want to know if user can read that folder

Upvotes: 4

Views: 3897

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59358

Regarding:

I don't understand what does it mean.

SP.ListItem.getUserEffectivePermissions method returns

A bitwise combination of enumeration values that specifies a set of permissions.

The example demonstrates how to parse SP.BasePermissions value and determine whether user has been granted permissions to read item:

BasePermissions permissions = new BasePermissions(138612833L,176L);
String[] roles = permissions.parse();
if(Arrays.asList(roles).contains("ViewListItems"))
    System.out.println("User has permissions to read items");

where PermissionKind and BasePermissions classes are equivalents to some extent to SharePoint Client SDK BasePermissions and PermissionKind classes:

import java.util.ArrayList;
import java.util.List;

public class BasePermissions {

    private final long low;
    private final long high;

    public BasePermissions(long low, long high) {
        this.low = low;
        this.high = high;
    }

    public String[] parse() {
        List<String> result = new ArrayList<>();
        for (PermissionKind value : PermissionKind.values()) {
            if(has(value)){
                result.add(value.toString());
            }
        }
        return result.toArray(new String[0]);
    }

    public Boolean has(PermissionKind perm)
    {
       switch (perm)
        {
            case EmptyMask:
                return true;
            case FullMask:
                if ((this.high & 32767) == 32767)
                    return this.low == 65535;
                return false;
            default:
                long n = perm.getValue() - 1;
                long val = 1;
                if (n >= 0 && n < 32)
                    return 0 != (this.low & (val << n));
                if (n >= 32 && n < 64)
                    return 0 != (this.high & (val << n - 32));
                return false;
        }
    }
}

and

public enum PermissionKind {
    EmptyMask(0),
    ViewListItems(1),
    AddListItems(2),
    EditListItems(3),
    DeleteListItems(4),
    ApproveItems(5),
    OpenItems(6),
    ViewVersions(7),
    DeleteVersions(8),
    CancelCheckout(9),
    ManagePersonalViews(10),
    ManageLists(12),
    ViewFormPages(13),
    AnonymousSearchAccessList(14),
    Open(17),
    ViewPages(18),
    AddAndCustomizePages(19),
    ApplyThemeAndBorder(20),
    ApplyStyleSheets(21),
    ViewUsageData(22),
    CreateSSCSite(23),
    ManageSubwebs(24),
    CreateGroups(25),
    ManagePermissions(26),
    BrowseDirectories(27),
    BrowseUserInfo(28),
    AddDelPrivateWebParts(29),
    UpdatePersonalWebParts(30),
    ManageWeb(31),
    AnonymousSearchAccessWebLists(32),
    UseClientIntegration(37),
    UseRemoteAPIs(38),
    ManageAlerts(39),
    CreateAlerts(40),
    EditMyUserInfo(41),
    EnumeratePermissions(63),
    FullMask(65);


    PermissionKind(long value) {
        this.value = value;
    }

    public long getValue() { return value; }

    private final long value;
}

Upvotes: 7

Related Questions