dave823
dave823

Reputation: 1211

What is the result of this C# Bitwise-OR?

I am trying to check permissions for SharePoint users in c# and I came across the following code that seems to work:

isGranted = spweb.DoesUserHavePermissions(userlogin, SPBasePermissions.EmptyMask | SPBasePermissions.ViewPages);

The first argument is the user to check a permission of. The second argument is the permission to check if the user has.

My question is, what is the result of the bitwise-or between emptymask and viewpages permissions? What permission is this actually checking against?

Upvotes: 2

Views: 605

Answers (3)

ken2k
ken2k

Reputation: 49013

The enumeration has the Flag attribute, which indicates you can combine values using bitwise operators.

Actually, this makes no sense to combine EmptyMask (which is 0) with another value, as 0 | X is always equal to X. Just use the other value.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727077

Since EmptyMask is defined as zero, the result is the same as passing the SPBasePermissions.ViewPages with no EmptyMask:

[Flags]
public enum SPBasePermissions
{
    EmptyMask =                 0×0000000000000000,
    ...
}

Upvotes: 6

PhonicUK
PhonicUK

Reputation: 13874

It's checking against both permissions. The permissions are bitwise flags.

I don't know the actual values, but say Empty Mask is: 01000000, and ViewPage is 00100000 - then OR'ing them would be 01100000 - so you get both of them together.

So then if you want to check that a user has the ViewPage permission, you can take the OR'ed value, AND it against the value for ViewPage, and if it's > 0 then you know you have permission.

Upvotes: 1

Related Questions