public class BasePage : System.Web.UI.UserControl
{
#region Fields
bool _isSuperAdmin, _IsAdmin, _isManager, _IsEndUser = false;
#endregion
#region Properties
public bool IsEndUser
{
get { return _IsEndUser; }
set { _IsEndUser = value; }
}
public bool IsManager
{
get { return _isManager; }
set { _isManager = value; }
}
public bool IsAdmin
{
get { return _IsAdmin; }
set { _IsAdmin = value; }
}
public bool IsSuperAdmin
{
get { return _isSuperAdmin; }
set { _isSuperAdmin = value; }
}
#endregion
#region Methods
protected override void OnLoad(EventArgs e)
{
CheckUser();
base.OnLoad(e);
}
public void CheckUser()
{
if (HttpContext.Current.Session.Count > 0)
{
if (!string.IsNullOrEmpty(Convert.ToString(HttpContext.Current.Session["GroupID"])))
{
int i = Convert.ToInt32(HttpContext.Current.Session["GroupID"].ToString());
switch (i)
{
case (int)RoleType.SuperAdmin:
IsSuperAdmin = true; break;
case (int)RoleType.OwnerGroup:
IsAdmin = true;
break;
case (int)RoleType.MemeberGroup:
IsManager = true;
break;
case (int)RoleType.VisitorGroup:
IsEndUser = true;
break;
default: IsAdmin = IsManager = IsEndUser = false;
break;
}
}
}
}
#endregion
}
#region Enum
public enum RoleType
{
SuperAdmin,
OwnerGroup,
MemeberGroup,
VisitorGroup,
}
#endregion
-----------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BasePage chkuser = new BasePage();
if (SiteHelper.CurrentUserLoginName() != "NA")
{
if (Session["GroupID"] == null)
{
if (SiteHelper.IsSiteAdmin())
{
HttpContext.Current.Session["GroupID"] = ((int)RoleType.SuperAdmin).ToString();
}
else
{
if (SiteHelper.CurrentUserRoleType() == SPRoleType.Administrator.ToString())
{
HttpContext.Current.Session["GroupID"] = ((int)RoleType.OwnerGroup).ToString();
}
if (SiteHelper.CurrentUserRoleType() == SPRoleType.Contributor.ToString())
{
HttpContext.Current.Session["GroupID"] = ((int)RoleType.MemeberGroup).ToString();
}
if (SiteHelper.CurrentUserRoleType() == SPRoleType.Reader.ToString())
{
HttpContext.Current.Session["GroupID"] = ((int)RoleType.VisitorGroup).ToString();
}
}
}
else
{
chkuser.CheckUser();
if (chkuser.IsSuperAdmin)
{
}
else if (chkuser.IsAdmin)
{
}
else if (chkuser.IsManager)
{
}
else if (chkuser.IsEndUser)
{
}
else
{
}
}
}
else
{
//no user
}
}
}
------------------------------
public class SiteHelper
{
public static string SiteUrl
{
get { return SPContext.Current.Web.Url; }
}
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;
}
#region GetCurrentUserGroup
public static string GetCurrentUserGroup()
{
string GroupName = "";
using (SPSite site = new SPSite(SiteUrl))
{
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))
{
GroupName = grp.Name; break;
}
//Get Role Assignments for Current User - If he has been directly assigned permissions
try { }
catch (Exception) {/*Best attempt to catch Exceptions*/}
}
}
spweb.AllowUnsafeUpdates = false;
} site.AllowUnsafeUpdates = false;
}
return GroupName;
}
#endregion
#region SiteAdmin
public static bool IsSiteAdmin()
{
SPContext currentContext;
try
{
//Getting the current context
currentContext = SPContext.Current;
}
catch (InvalidOperationException)
{
currentContext = null;
}
if (currentContext != null && currentContext.Web.CurrentUser != null)
{
if (SPContext.Current.Web.CurrentUser.IsSiteAdmin)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
#endregion
#region CurrentUserLoginName
public static string CurrentUserLoginName()
{
string userName = "NA";
SPContext currentContext;
try
{
//Getting the current context
currentContext = SPContext.Current;
}
catch (InvalidOperationException)
{
currentContext = null;
}
if (currentContext != null && currentContext.Web.CurrentUser != null)
{
SPSite spSite;
SPWeb spWeb;
using (spSite = new SPSite(SiteUrl))
{
using (spWeb = spSite.OpenWeb())
{
userName = spWeb.CurrentUser.LoginName;
}
}
}
return userName;
}
#endregion
public static DataTable GetCurrentSiteGroup()
{
DataTable dtGroups = new DataTable();
dtGroups.Columns.Add("GroupName");
dtGroups.Columns.Add("Url");
using (SPSite site = new SPSite(SiteUrl))
{
site.AllowUnsafeUpdates = true;
using (SPWeb spweb = site.OpenWeb())
{
spweb.AllowUnsafeUpdates = true;
SPGroupCollection groupCollection = spweb.Groups;
if (groupCollection.Count != 0 && groupCollection != null)
{
foreach (SPGroup group in groupCollection)
{
DataRow dr = dtGroups.NewRow();
dr["GroupName"] = group.Name;
dr["Url"] = group.ID;
dtGroups.Rows.Add(dr);
}
}
spweb.AllowUnsafeUpdates = false;
} site.AllowUnsafeUpdates = false;
}
return dtGroups;
}
}