如何使用webservices API找出网站中用户的角色?我必须继续使用的是用户名和域名。
我发现PermissionsService.Permissions.GetPermissionCollection(url,“Web”)将返回一组允许的用户和组及其权限掩码,但我仍然需要弄清楚用户是否在任何组中然后将权限掩码转换为角色集合。
我觉得有更好的方法可以做到这一点,我只是错过了它。
答案 0 :(得分:5)
我已经解决了类似问题 - 我的方法检查是否为用户分配了特定角色。首先是算法:
代码:
public bool IsAssignedAPermission(string premissionName, string userLoginName)
{
XmlNode nodes;
bool isAssignedAPermission;
isAssignedAPermission = false;
//Check if user is directly assigned a Full Control role
try
{
nodes = userGroupService.GetRoleCollectionFromUser(userLoginName);
using (XmlNodeReader reader = new XmlNodeReader(nodes))
{
DataSet ds = new DataSet();
ds.ReadXml(reader);
DataTable dt = ds.Tables[1];
foreach (DataRow row in dt.Rows)
{
string permission = row[1].ToString();
if (permission == premissionName)
{
isAssignedAPermission = true;
break;
}
}
}
}
catch
{
List<string> groupMemberships;
List<string> fullControlGroups;
//Check if user is a member of a Full Control group
//This is done in three steps:
//1. Get the list of groups the user is member of
groupMemberships = new List<string>();
nodes = userGroupService.GetGroupCollectionFromUser(userLoginName);
using (XmlNodeReader reader = new XmlNodeReader(nodes))
{
DataSet ds = new DataSet();
ds.ReadXml(reader);
DataTable dt = ds.Tables[1];
foreach (DataRow row in dt.Rows)
{
string groupName = row[1].ToString();
groupMemberships.Add(groupName);
}
}
//2. Get the list of groups that have Full Control permissions
fullControlGroups = new List<string>();
nodes = userGroupService.GetGroupCollectionFromRole(premissionName);
using (XmlNodeReader reader = new XmlNodeReader(nodes))
{
DataSet ds = new DataSet();
ds.ReadXml(reader);
DataTable dt = ds.Tables[1];
foreach (DataRow row in dt.Rows)
{
string groupName = row[1].ToString();
fullControlGroups.Add(groupName);
}
}
//3. Check if user belongs to any of the Full Control groups
foreach (string membership in groupMemberships)
{
if (fullControlGroups.Contains(membership))
{
isAssignedAPermission = true;
break;
}
}
}
return isAssignedAPermission;
}
方法参数userLoginName应该在表单域\用户名中,例如SHAREPOINT \鲍里斯。我希望我帮忙。干杯
答案 1 :(得分:1)
在这篇博文中,我使用UserGroup Web服务列出用户所属的SharePoint组:http://www.theblackknightsings.com/ListAllSharePointGroupsAUserBelongsTo.aspx
答案 2 :(得分:0)
尝试使用 UserGroup 网络服务中的 GetRoleCollectionFromUser 方法,它将为您提供用户在您网站中所属的角色列表。
只需将其作为参数传递给您的域\用户名。
在此处查找其定义: http://msdn.microsoft.com/en-us/library/ms772680.aspx
返回的xml将如下所示,然后您只需获得所需的信息。
<GetRoleCollectionFromUser xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/">
<Roles>
<Role ID="1073741829" Name="Full Control" Description="Has full control." Order="1" Hidden="False" Type="Administrator" BasePermissions="FullMask" />
<Role ID="1073741825" Name="Limited Access" Description="Can view specific lists, document libraries, list items, folders, or documents when given permissions." Order="8" Hidden="True" Type="Guest" BasePermissions="ViewFormPages, Open, BrowseUserInfo, UseClientIntegration, UseRemoteAPIs" />
</Roles>
</GetRoleCollectionFromUser>