Skip to content

Instantly share code, notes, and snippets.

@umbraesoulsbane
Created June 6, 2012 18:26
Show Gist options
  • Save umbraesoulsbane/2883734 to your computer and use it in GitHub Desktop.
Save umbraesoulsbane/2883734 to your computer and use it in GitHub Desktop.
<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Import Namespace="System" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="Microsoft.SharePoint.Administration" %>
<%@ Import Namespace="System.Collections.Specialized" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" Inherits="Microsoft.SharePoint.ApplicationPages.RequestAccess" MasterPageFile="~/_layouts/simple.master" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string strUser = String.Format("{0}\\{1}", Environment.UserDomainName, Environment.UserName);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite siteCollection = new SPSite(SPContext.Current.Site.ID))
{
// Site Collection AD Group Workaround PREP
string siteURL = Request.Url.GetLeftPart(UriPartial.Authority);
SPSite thisroot = new SPSite(siteURL);
SPList admins = thisroot.RootWeb.Lists.TryGetList("Super Admin List");
SPQuery adminQry = new SPQuery();
SPUser reqUser = thisroot.RootWeb.SiteUsers[strUser];
//accessOUTPUT.Text = reqUser.Name;
// END Site Collection AD Group Workaround PREP
// SPUser reqUser = siteCollection.RootWeb.EnsureUser(this.Context.User.Identity.Name);
int userCnt = 0;
string qryUser = Request.QueryString["so"];
string qrySent = Request.QueryString["sos"];
string mailtoSubject = "Access Request for " + siteCollection.RootWeb.Title + " team site";
string mailtoBody = "<p style='font-family: Arial;'>The user <b>" + "</b> (<b>" + reqUser.LoginName + "</b>) has requested access to the <b><a href='" + siteCollection.RootWeb.Url + "'>" + siteCollection.RootWeb.Title + "</a></b> team site. Please review this request and provide access if appropriate.<BR><BR>For information on managing your site's security, please see the following sites:</p><ul><li style='font-family: Arial;'><a href='https://teamsites.csx.com/SitePages/Team%20Site%20Support%20Home.aspx'>Team Site Tutorial</a></li><li style='font-family: Arial;'><a href='https://teamsites.csx.com/SitePages/Manage%20Site%20Security.aspx'>Manage Site Security</a></li></ul><p style='font-family: Arial;'>You have been contacted because you were identified as having owner privileges to this site. If you are not the correct person to grant access, please forward to the responsible site owner, or contact <b>" + reqUser.Name + "</b> with further information.<BR><BR>Thank you.</p>";
if (qrySent == null)
{
accessOUTPUT.Text = "To request access to the <b>" + siteCollection.RootWeb.Title + "</b> team site, please select one of the following Site Owners:";
} else {
accessOUTPUT.Text = "You have requested access to the <b>" + siteCollection.RootWeb.Title + "</b> team site. Please wait for the site owner to process your request. If you do not hear back from the site owner, please follow up at a later date or return here to select another site owner.";
}
ownersOUTPUT.Text = "<ul>";
SPUserCollection userCollection = siteCollection.RootWeb.SiteUsers;
foreach (SPUser user in userCollection)
{
string userSTR = user.Name;
string userPERM = siteCollection.RootWeb.GetUserEffectivePermissions(user.LoginName).ToString();
if (userPERM.Equals("FullMask") && !userSTR.Contains("System") && !userSTR.Contains("(admin)")) // && !user.IsSiteAdmin
{
if (qryUser != null)
{
if (user.ID.ToString() == qryUser)
{
if (qrySent == null)
{
StringDictionary mailtoHeaders = new StringDictionary();
mailtoHeaders.Add("to", user.Email);
mailtoHeaders.Add("cc", reqUser.Email);
mailtoHeaders.Add("from", reqUser.Email);
mailtoHeaders.Add("subject", mailtoSubject);
SPUtility.SendEmail(siteCollection.RootWeb, mailtoHeaders, mailtoBody);
Response.Redirect(siteCollection.RootWeb.Url + Request.Path + "?" + Request.QueryString + "&sos=y");
} else {
userCnt++;
ownersOUTPUT.Text += "<li style='margin-bottom: 5px; margin-top: 25px; color: Green;'>Request for access sent to <b>" + userSTR + "</b>.</li>";
break;
}
}
} else {
userCnt++;
bool showThisGuy = true;
if (admins != null)
{
adminQry = new SPQuery();
adminQry.Query = string.Format("<Where><Eq><FieldRef Name='Person' /><Value Type='User'>{0}</Value></Eq></Where>", user.Name);
adminQry.RowLimit = 1;
SPListItemCollection curAdmins = admins.GetItems(adminQry);
// DEBUG
userSTR += "<!-- <b>Site:</b> " + siteCollection.RootWeb.Title + " || <b>User:</b> " + user.Name + " || <b>SiteAdmin:</b> " + user.IsSiteAdmin + " || <b>SuperList:</b> " + curAdmins.Count + " ";
if (curAdmins.Count.Equals(0) && user.IsSiteAdmin)
{
showThisGuy = false;
}
else if (curAdmins.Count.Equals(1) && user.IsSiteAdmin)
{
SPListItem thiscurAdmin = curAdmins[0];
// DEBUG
userSTR += "|| <b>Site:</b> " + siteCollection.RootWeb.Title + " / <b>List:</b> " + thiscurAdmin["AdminOf"];
if (!siteCollection.RootWeb.Title.Equals(thiscurAdmin["AdminOf"]))
{
showThisGuy = false;
}
}
// DEBUG
userSTR += " -->";
}
else
{ // If "Super Admin List" does not exist on root webapplication hide all site admins.
// - AdminOf = Text
// - Person = People Picker
if (user.IsSiteAdmin)
{
showThisGuy = false;
}
}
if (showThisGuy)
{
ownersOUTPUT.Text += "<li style='margin-bottom: 5px; margin-top: 10px;'><a href='?type=web&so=" + user.ID + "' style='text-decoration: underline;'>" + userSTR + "</a></li>";
}
}
}
}
if (userCnt.Equals(0))
{
ownersOUTPUT.Text += "<li>Unable to determine Site Owners.</li>";
}
ownersOUTPUT.Text += "</ul>";
}
}); // RunWithElevatedPrivileges
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">
<SharePoint:EncodedLiteral ID="EncodedLiteral1" runat="server" text="<%$Resources:wss,reqacc_pagetitle%>" EncodeMethod='HtmlEncode'/>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
<SharePoint:EncodedLiteral ID="EncodedLiteral2" runat="server" text="<%$Resources:wss,reqacc_pagetitle%>" EncodeMethod='HtmlEncode'/>
</asp:Content>
<asp:Content ID="Content3" contentplaceholderid="PlaceHolderAdditionalPageHead" runat="server">
<meta name="Robots" content="NOINDEX " />
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<table border="0" cellpadding="0" cellspacing="0" width="99%">
<tr>
<td id="RoleName" class="ms-descriptiontext">
<SharePoint:EncodedLiteral ID="EncodedLiteral4" runat="server" text="<%$Resources:wss,reqacc_logonname%>" EncodeMethod='HtmlEncode'/>
&#160;<b><asp:Label id="LabelUserName" runat="server"/></b>
</td>
</tr>
<tr><td rowspan="1" height="10"><img src="/_layouts/images/blank.gif" width='1' height='1' alt="" /></td></tr>
<tr>
<td class="ms-descriptiontext"><asp:Label id="accessOUTPUT" runat="server"/></td>
</tr>
<tr>
<td><asp:Label id="ownersOUTPUT" Text="" runat="server" /></td>
</tr>
<tr><td rowspan="1" height="10"><img src="/_layouts/images/blank.gif" width='1' height='1' alt="" /></td></tr>
<tr><td class="ms-sectionline" height="1"><img src="/_layouts/images/blank.gif" width='1' height='1' alt="" /></td></tr>
<tr><td rowspan="1" height="10"><img src="/_layouts/images/blank.gif" width='1' height='1' alt="" /></td></tr>
</table>
</asp:Content>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment