How To Tell if Two PermissionSets Are The Same

Determining if two PermissionSet objects are logically the same is a relatively common thing for an application that deals with security to attempt to do, however the v1.0 and v1.1 PermissionSet classes did not override the Equals method to allow this functionality.  Even though an Equals override is not provided, it's pretty easy to write your own using PermissionSet's set operations.

From set theory we know that if set a is a subset of set b and if set b is also a subset of set a, then sets a and b contain equivalent elements.  Add in a few checks for null, and this boils down into a quick and dirty ComparePermissionSets method:

/// <summary>
/// Compare two permission sets to see if they are the same
/// </summary>
/// <param name="lhs">PermissionSet 1</param>
/// <param name="rhs">PermissionSet 2</param>
/// <returns>true if lhs is the same as rhs, false if they are different</returns>
public static bool ComparePermissionSets(PermissionSet lhs, PermissionSet rhs)
    if(lhs == null && rhs == null)
        return true;
    else if(lhs == null && rhs != null)
        return false;
    else if(lhs != null && rhs == null)
        return false;
        return lhs.IsSubsetOf(rhs) && rhs.IsSubsetOf(lhs);

Comments (0)

Skip to main content