/// <summary> /// called from the CreatChildControls, to get the sub sites list /// </summary> private void FindAccessibleSubSites() { //debug.Add("FindAccessibleSubSites"); try { dataTable.Rows.Clear(); FindPermissionToUse(); if (string.IsNullOrEmpty(ConnectedUser)) { FindSitesForUser(); } else { SPSecurity.CodeToRunElevated elevatedCode = new SPSecurity.CodeToRunElevated(FindSitesForConnectedUser); SPSecurity.RunWithElevatedPrivileges(elevatedCode); } grid.DataBind(); } catch (Exception e) { //debug.Add(e.ToString()); Controls.Add(new LiteralControl("Error occurred: " + e.Message)); Controls.Add(new LiteralControl("<br />" + e.StackTrace)); return; } }
private bool IsMember(string userName, string siteURL) { _user = userName; _site = siteURL; SPSecurity.CodeToRunElevated checkSiteCode = new SPSecurity.CodeToRunElevated(CheckSite); SPSecurity.RunWithElevatedPrivileges(checkSiteCode); return(_validMember); }
public static void RunWithProcessIdentity(SPSecurity.CodeToRunElevated secureCode) { using (WindowsIdentity.Impersonate(IntPtr.Zero)) { secureCode(); } }
internal static void RunWithElevatedPrivilegesAndContextSwitch(SPSecurity.CodeToRunElevated secureCode) { HttpContext backupContext = HttpContext.Current; HttpContext.Current = null; SPSecurity.RunWithElevatedPrivileges(secureCode); HttpContext.Current = backupContext; }
public void CanSpyOnRunCodeWithElevatedPrivilegesHasCodeCodeToRunElevated() { //Assign SPSecurity.CodeToRunElevated t = () => { }; //Act SPSecurity.RunWithElevatedPrivileges(t); //Assert Assert.That(SubstituteContext.HasCodeRunWithElevatedPrivilegesDelegate, Is.EqualTo(t)); }
/// <summary> /// Esegue del codice sia in modalita' Amministrazione sia con privilegi elevati /// </summary> /// <param name="codeToRunInAdminMode"></param> public static void RunInAdminMode(SPSecurity.CodeToRunElevated codeToRunInAdminMode) { var adminProp = typeof(SPSecurity).GetField("s_AdministrationAllowedInCurrentProcess", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.FlattenHierarchy); var originalValue = adminProp.GetValue(null); // imposta a true la modalita' di amministrazione adminProp.SetValue(null, true); try { SPSecurity.RunWithElevatedPrivileges(codeToRunInAdminMode); } catch { throw; } finally { // imposta il valore a come era all'inizio adminProp.SetValue(null, originalValue); } }
private void RunAsAdmin(SPSecurity.CodeToRunElevated secureCode) { bool nullUserFlag = (SPContext.Current != null && IsAnonymous); if (nullUserFlag) { HttpContext backupCtx = HttpContext.Current; try { HttpContext.Current = null; SPSecurity.RunWithElevatedPrivileges(secureCode); } finally { HttpContext.Current = backupCtx; } } else { SPSecurity.RunWithElevatedPrivileges(secureCode); } }
protected override void RunElevated(SPSecurity.CodeToRunElevated method) { method.Invoke(); }
// setDocMethod is a delegate pointing to the SetDocument() method of the XmlBasedControl // being executed public static void InvokeSetDocument(SetDocumentDelegate setDocMethod) { // check if the user is in the SharePoint group allowed to view draft items if (UserCanViewDraftItems()) { // yes - elevate security so they have 'edit' permissions on the list items // which will enable them to view drafts SPSecurity.CodeToRunElevated elevatedSetDoc = new SPSecurity.CodeToRunElevated(setDocMethod); SPSecurity.RunWithElevatedPrivileges(elevatedSetDoc); } else { // no - so execute in the user's current context setDocMethod(); } }
private static string SaveItem(SPWeb web, string guid, SPListItem item, SPFolder spFolder, int contractId, int currentUserId, List <int> viewers, List <int> editors, int adder) { SPSecurity.CodeToRunElevated secureCode = null; string str = ""; Guid siteID = web.Site.ID; try { if (secureCode == null) { secureCode = delegate { using (SPSite site = new SPSite(siteID)) { using (SPWeb Web = site.OpenWeb()) { SPList list = Web.Lists[new Guid(guid)]; if (!spFolder.Exists) { SPListItem folderItem = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder); folderItem["Title"] = contractId.ToString(); Web.AllowUnsafeUpdates = true; folderItem.Update(); int num = 0; foreach (int num2 in viewers) { if (num == 0) { Utility.SetListItemPermission(folderItem, num2, 0x40000002, true); } else { Utility.SetListItemPermission(folderItem, num2, 0x40000002, false); } num++; } foreach (int num3 in editors) { if (num == 0) { Utility.SetListItemPermission(folderItem, num3, 0x40000003, true); } else { Utility.SetListItemPermission(folderItem, num3, 0x40000003, false); } num++; } if (adder != 0) { Utility.SetListItemPermission(folderItem, adder, 0x4000006b, false); } spFolder = web.GetFolder(list.RootFolder.Url + "/" + contractId); } } } }; } SPSecurity.RunWithElevatedPrivileges(secureCode); SPWeb web2 = item.Web; web2.AllowUnsafeUpdates = true; item.Update(); if (contractId != 0) { Utility.ResetItemPermission(item); } else { int num = 0; foreach (int num2 in viewers) { if (num == 0) { Utility.SetListItemPermission(item, num2, 0x40000002, true); } else { Utility.SetListItemPermission(item, num2, 0x40000002, false); } num++; } foreach (int num3 in editors) { if (num == 0) { Utility.SetListItemPermission(item, num3, 0x40000003, true); } else { Utility.SetListItemPermission(item, num3, 0x40000003, false); } num++; } } str = item.ID.ToString(); web2.AllowUnsafeUpdates = false; } catch (Exception exception) { return(exception.Message); } return(str); }
/// <summary> /// Delegate to run without elevated privileges. Just run as the current user. /// </summary> private void RunWithCurrentUserPrivileges(SPSecurity.CodeToRunElevated codeToRun) { // Don't impersonate, just run the code. codeToRun(); }
/// <summary> /// Runs the method in an elevated SharePoint context. /// </summary> /// <param name="method">method to run</param> protected virtual void RunElevated(SPSecurity.CodeToRunElevated method) { SPSecurity.RunWithElevatedPrivileges(method); }
public override void Provision(SPWebProvisioningProperties props) { // Create a blank site to begin from props.Web.ApplyWebTemplate(SITE_TEMPLATE); // Save this so it is available in other methods Properties = props; SPSecurity.CodeToRunElevated code = new SPSecurity.CodeToRunElevated(CreateSite); SPSecurity.RunWithElevatedPrivileges(code); }
public static void RunWithElevatedPrivileges(SPSecurity.CodeToRunElevated codeToRunElevated) { RunWithoutAccessDenied(() => SPSecurity.RunWithElevatedPrivileges(codeToRunElevated)); }