对Shared Services(ssp)的用户(user)的增删改
对Shared Services(ssp)的用户(user)的增删改
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server;
using System.Reflection;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server.Infrastructure;
using Microsoft.SharePoint;
namespace CurrentUserHelp
{
public class CurrentUserHelp
{
#region GetUserSSPRights
public static SharedServiceRights GetUserSSPRights(ServerContext context, string username)
{
Type sharedServiceAccessControlListType = Type.GetType("Microsoft.Office.Server.Infrastructure.SharedServiceAccessControlList, Microsoft.Office.Server, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
MethodInfo sharedServiceAccessControlListMethod =
sharedServiceAccessControlListType.GetMethod("GetInstance",
BindingFlags.NonPublic |
BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.InvokeMethod |
BindingFlags.FlattenHierarchy |
BindingFlags.Static,
null,
new Type[] { typeof(ServerContext) },
null
);
object acl = sharedServiceAccessControlListMethod.Invoke(
null,
new object[] { context }
);
System.Reflection.PropertyInfo itemProp = acl.GetType().GetProperty("Item",
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.InvokeMethod |
BindingFlags.GetProperty |
BindingFlags.Public
);
SharedServiceAccessControlEntry aclEntry =
(SharedServiceAccessControlEntry)itemProp.GetValue(
acl,
new object[] { username }
);
// check if the user has any permissions
if (aclEntry != null)
{
return aclEntry.Rights;
}
else
{
// the user is not even in the list
return SharedServiceRights.None;
}
}
#endregion
#region Check User Have SSP Rights
public static bool CheckUserHaveSSPRights(ServerContext context, string username, SharedServiceRights rightMask)
{
return (GetUserSSPRights(context, username) & rightMask) == rightMask;
}
#endregion
#region ChangeUserRight
/// <summary>
/// true:add false:remove
/// </summary>
/// <param name="isadd"></param>
public void ChangeUserRight(bool isadd, ServerContext current, string username)
{
SharedServiceRights rights = SharedServiceRights.ManageUserProfiles;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPContext.Current.Web.AllowUnsafeUpdates = true;
Type sharedServiceAccessControlListType = Type.GetType("Microsoft.Office.Server.Infrastructure.SharedServiceAccessControlList, Microsoft.Office.Server, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
MethodInfo sharedServiceAccessControlListMethodInfo =
sharedServiceAccessControlListType.GetMethod("GetInstance",
BindingFlags.NonPublic |
BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.InvokeMethod |
BindingFlags.FlattenHierarchy |
BindingFlags.Static,
null,
new Type[] { typeof(ServerContext) }, null);
object acl = sharedServiceAccessControlListMethodInfo.Invoke(null, new object[] { current });
if (isadd)
{
//SharedServiceAccessControlEntry aclEntry = acl[username];
System.Reflection.PropertyInfo itemProp = acl.GetType().GetProperty("Item",
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.InvokeMethod |
BindingFlags.GetProperty |
BindingFlags.Public);
SharedServiceAccessControlEntry aclEntry = (SharedServiceAccessControlEntry)itemProp.GetValue(acl, new object[] { username });
if (aclEntry == null)
{
// Adding a new user
aclEntry = new SharedServiceAccessControlEntry(username, rights);
MethodInfo add = acl.GetType().GetMethod("Add",
BindingFlags.NonPublic |
BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.InvokeMethod |
BindingFlags.FlattenHierarchy,
null,
new Type[] { typeof(SharedServiceAccessControlEntry) }, null);
//acl.Add(aclEntry);
add.Invoke(acl, new object[] { aclEntry });
}
}
else
{
MethodInfo remove = acl.GetType().GetMethod("Remove",
BindingFlags.NonPublic |
BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.InvokeMethod |
BindingFlags.FlattenHierarchy,
null,
new Type[] { typeof(string) }, null);
//acl.Remove(username);
remove.Invoke(acl, new object[] { username });
}
MethodInfo update =
acl.GetType().GetMethod("Update",
BindingFlags.NonPublic |
BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.InvokeMethod |
BindingFlags.FlattenHierarchy,
null,
new Type[] { }, null);
// acl.Update();
update.Invoke(acl, null);
});
}
catch (Exception ex)
{
throw (ex);
}
}
#endregion
}
}