Quick sample: EPiServer Change access level of Page

Introduction

Bitwise operators give you the ability to store multiple settings in a single primitive data type (e.g. an integer). This is useful when a single item has potentially more than one setting of the same type.

EPiServer AccessLevel is enum, and if you want to set or remove a AccessLevel, we need to use bitwise operators:

Defining the Flags

To start of with, declare an enum to list all the possible flags. Two things are important when declaring the enum. The first thing you will probably notice is the [Flags] attribute. This is necessary in order to indicate that the enumeration should be treated as a set of flags. The second important thing is assigning a value to each of the items in the enum. The first value should be 1, then just double the value for each consecutive item. The integer type in .NET can store up to 32 flags.

EPiServer.Security
{
 // Summary:
 //     The access levels (bitmapped) that are used to control the various actions
 //     that a user is allowed to perform.
 //
 // Remarks:
 //     Primarily used to control access to page manipulation functions in EPiServer,
 //     but can be used for general-purpose access restriction functions.
 [Serializable]
 [Flags]
 public enum AccessLevel

Setting flags ON:

To set multiple flags, concatenate the desired flags using the bitwise OR symbol “|”:

pageACL.Access |= AccessLevel.Delete;

Setting flags OFF:

pageACL.Access &= ~AccessLevel.Delete;

Testing to see if a certain flag is set:

if ((pageACL.Access&AccessLevel.Delete)==AccessLevel.Delete)


Some sample code

  • Get a PageData by PageLink
  • get ACL list
  • Loop though ACL
  • Remove Delete access right, so no one can delete this page
PageData data = DataFactory.Instance.GetPage(e.PageLink);

 // this page also cannot delete, ignore processing
 if (data.QueryDistinctAccess(AccessLevel.Delete) == false)
 {
     return;
 }

 // modify this page accessright, do not allow to delete
 AccessControlList list = data.ACL.CreateWritableClone();
 foreach (KeyValuePair<string, AccessControlEntry> pair in data.ACL)
 {
 if ((pair.Value.Access & AccessLevel.FullAccess) == AccessLevel.FullAccess)
 {
 list.Remove(pair.Key);
 list.Add(new AccessControlEntry(pair.Value.Name, pair.Value.Access & ~AccessLevel.FullAccess, pair.Value.EntityType));
 }

 if ((pair.Value.Access & AccessLevel.Delete) == AccessLevel.Delete)
 {
 list.Remove(pair.Key);
 list.Add(new AccessControlEntry(pair.Value.Name, pair.Value.Access & ~AccessLevel.Delete, pair.Value.EntityType));
 }
 }
 list.Save(SecuritySaveType.Modify);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s