September 10, 2009

AzMan Add/Remove Members to Role by SID or UPN with C#

We have already seen how to add a store,create an application, add an operation, add tasks, create roles, and add application groups in AzMan using C#. Now, let's go on to adding users by name or SID to those roles using c#. I have uploaded a helper class here which has some comments not included in the short snippets below as well as some other methods (check if exists etc.). This class was designed to work with XML and AD (sorry not sql server yet).

One of the cool things is that you can add a member to a role by a SID. This though has to be done via code. Below you can find how to ao a bunch of this via SIDs...the helper class shows how you can do the same thing with UPN as well:
 public static void AddRoleMember(IAzRole role, string memberSID)
{
if (memberSID == null || memberSID.Length == 0)
{
throw new ArgumentNullException("memberSID", "Member SID can not be null or empty.");
}
if (role == null)
{
throw new ArgumentNullException("role", "Role can not be null.");
}

role.AddMember(memberSID, null);
role.Submit(0, null);
}
Now let's remove that member by SID from the role
 public static void RemoveRoleMember(IAzRole role, string memberSID)
{
if (memberSID == null || memberSID.Length == 0)
{
throw new ArgumentNullException("memberSID", "Member SID can not be null or empty.");
}
if (role == null)
{
throw new ArgumentNullException("role", "Role can not be null.");
}

role.DeleteMember(memberSID, null);
role.Submit(0, null);
}
How about returning true if role member SID exists?
 public static bool IsRoleMemberExists(IAzRole role, string memberSID)
{
if (memberSID == null || memberSID.Length == 0)
{
throw new ArgumentNullException("memberSID", "Member SID can not be null or empty.");
}

if (role == null)
{
throw new ArgumentNullException("role", "Role can not be null.");
}

foreach (string sid in GetRoleMemberNames(role))
{
if (String.Compare(memberSID, sid) == 0)
{
return true;
}
}

return false;
}

No comments:

Post a Comment