public static string CurrentUserRoleType()
{
string Curntroletype = string.Empty;
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
site.AllowUnsafeUpdates = true;
using (SPWeb spweb = site.OpenWeb())
{
spweb.AllowUnsafeUpdates = true;
SPGroupCollection groupCollection = spweb.Groups;
foreach (SPGroup grp in spweb.CurrentUser.Groups)
{
foreach (SPGroup group in groupCollection)
{
if (grp.Name.Equals(group.Name))
{
SPContext.Current.Site.CatchAccessDeniedException = false;
// Retrieve all user roles assigned for the current user for the current web.
SPRoleDefinitionBindingCollection userRoles =
SPContext.Current.Web.AllRolesForCurrentUser;
string xml = userRoles.Xml;
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
foreach (XmlNode item in doc.DocumentElement.ChildNodes)
{
//Check If Role Type == Reader - Do something
if (item.Attributes["Type"].Value.Equals(SPRoleType.Reader.ToString()))
{
Curntroletype= SPRoleType.Reader.ToString();
}
//Administrators access - Full Control - Do something
else if (item.Attributes["Type"].Value.Equals(SPRoleType.Administrator.ToString()))
{
Curntroletype= SPRoleType.Administrator.ToString();
}
//Contributor access - Contribute - Do something
else if (item.Attributes["Type"].Value.Equals(SPRoleType.Contributor.ToString()))
{
Curntroletype= SPRoleType.Contributor.ToString();
}
//Web Designer access - Design rights- Do something
else if (item.Attributes["Type"].Value.Equals(SPRoleType.WebDesigner.ToString()))
{
Curntroletype= SPRoleType.WebDesigner.ToString();
}
//Limited access - Do something
else if (item.Attributes["Type"].Value.Equals(SPRoleType.Guest.ToString()))
{
Curntroletype= SPRoleType.Guest.ToString();
}
//No access on Current Web- Do something
else if (item.Attributes["Type"].Value.Equals(SPRoleType.None.ToString()))
{
Curntroletype = SPRoleType.Guest.ToString();
}
else
{
Curntroletype = SPRoleType.None.ToString();
}
break;
}
}
//Get Role Assignments for Current User - If he has been directly assigned permissions
try { }
catch (Exception) {/*Best attempt to catch Exceptions*/}
finally
{
SPContext.Current.Site.CatchAccessDeniedException = true;
}
}
}
spweb.AllowUnsafeUpdates = false;
} site.AllowUnsafeUpdates = false;
} return Curntroletype;
}
No comments:
Post a Comment