public void TestAddRoleToSPSiteSimple() { String addPerms = ""; string result = result = RunBuild(String.Format(CultureInfo.InvariantCulture, _xmlProjectTemplate, _newSPSiteUrl, "true", _testRoleName, _roleDescription, "", addPerms)); SPSite site = new SPSite(_newSPSiteUrl); SPRole newRole = site.RootWeb.Roles[_testRoleName]; Assertion.AssertNotNull("New Role is null!", newRole); // Construct the rights the role should have for comparison. The // base permission of a role defaults to the permission of the // "Guest" role. From there we add anything other permissions // the role should have. SPRights rights = site.RootWeb.Roles["Guest"].PermissionMask; Assertion.AssertEquals("permissions were not correctly modified.", 0, rights.CompareTo(newRole.PermissionMask)); }
public void TestAddRoleToSPSiteNoAdd() { String addPerms = ""; string result = result = RunBuild(String.Format(CultureInfo.InvariantCulture, _xmlProjectTemplate, _newSPSiteUrl, "true", _testRoleName, _roleDescription, _baseRoleName, addPerms)); SPSite site = new SPSite(_newSPSiteUrl); SPRole newRole = site.RootWeb.Roles[_testRoleName]; Assertion.AssertNotNull("New Role is null!", newRole); // Construct the rights the role should have for comparison. SPRights rights = site.RootWeb.Roles[_baseRoleName].PermissionMask; Assertion.AssertEquals("permissions were not correctly modified.", 0, rights.CompareTo(newRole.PermissionMask)); }
/// <summary> /// Sets the permission mask to the mask of the role specified in the /// BaseRole property. /// </summary> /// <param name="role">The role to be modified.</param> private void SetBaseRolePermissions(SPRole role) { if (!BaseRole.Equals(string.Empty)) { SPRole baseRole = role.ParentWeb.Roles[BaseRole]; role.PermissionMask = baseRole.PermissionMask; } }
/// <summary> /// Adds additional permissions to the mask for this role /// </summary> /// <param name="newRole">The Role whose permissions are to be /// modified.</param> private void AddPermissionsToRole(SPRole newRole) { if (!AddPermissions.Equals(string.Empty)) { newRole.PermissionMask = newRole.PermissionMask | (SPRights)Enum.Parse(typeof(SPRights), AddPermissions); } }
public void TestRemoveRoleFromSPSite() { SPSite site = new SPSite(_newSPSiteUrl); string result = result = RunBuild(String.Format(CultureInfo.InvariantCulture, _xmlProjectTemplate, _newSPSiteUrl, "true", _testRoleName)); try { SPRole removed = site.RootWeb.Roles[_testRoleName]; Assertion.Fail("The role was not removed."); } catch (SPException ignored) { // This is the desired behavior. } }
protected override void SetUp() { base.SetUp(); DirectoryEntry root = new DirectoryEntry(_rootDirectory); DirectoryEntry newUserEntry = root.Children.Add(_newUserName, "user"); newUserEntry.Invoke("SetPassword", _password); newUserEntry.CommitChanges(); DirectoryEntry userEntry = root.Children.Add(_userName, "user"); userEntry.Invoke("SetPassword", _password); userEntry.CommitChanges(); Uri uri = new Uri(_newSPSiteServerUrl); SPSite site = new SPSite(uri.ToString()); SPSite newSite = site.SelfServiceCreateSite( _newSPSiteUrl, _newSPSiteTitle, _newSPSiteDesc, site.RootWeb.Language, "STS", _newSPSiteOwnerLogin, _newSPSiteOwnerName, _newSPSiteOwnerEmail, _newSPSiteContactLogin, _newSPSiteContactName, _newSPSiteContactEmail); // Add the test user to the site. // SPSite site = new SPSite(_newSPSiteUrl); SPRole role = newSite.RootWeb.Roles["Contributor"]; if (role == null) { throw new Exception(string.Format( "The role '{0}' does not exist", "Contributor")); } role.AddUser(_newUserLogin, "", "", ""); }
/// <summary> /// Task for adding a SPUser to a site. /// </summary> protected override void ExecuteTask() { try { SPSite site = new SPSite(Url); SPRole role = site.RootWeb.Roles[Role]; if (role == null) { throw new BuildException(string.Format( "The role '{0}' does not exist", Role)); } role.AddUser(LoginId, "", "", ""); } catch (SPException ex) { throw new BuildException( string.Format("Cannot add user: {0} to site: {1}.", LoginId, Url), Location, ex); } }
// Private Methods (1) private void AddGroup(SPWeb spWeb, SPGroup owner, SPUser user, string groupName) { LogMessage(groupName, 3); try { SPGroup spGroup = null; try { spGroup = spWeb.SiteGroups.GetByName(groupName); } catch { } if (spGroup == null) { spWeb.SiteGroups.Add(groupName, owner, user, null); SPRole roll = spWeb.Roles["Read"]; roll.AddGroup(spWeb.SiteGroups[groupName]); try { SPList spList = spWeb.Lists["User Information List"]; spList.Items.GetItemById(spWeb.SiteGroups[groupName].ID).SystemUpdate(); } catch { } LogMessage(null, MessageKind.SUCCESS, 4); } else { LogMessage("Group already exists.", MessageKind.SKIPPED, 4); } } catch (Exception exception) { LogMessage(exception.Message, MessageKind.FAILURE, 4); } }
public void TestAddRoleToSPSite() { // Create some list of permissions to add to the role we are // creating. String addPerms = SPRights.ApplyStyleSheets.ToString() + ", " + SPRights.ManageListPermissions.ToString() + ", " + SPRights.ManageRoles.ToString() + ", " + SPRights.CreateSSCSite.ToString(); string result = result = RunBuild(String.Format(CultureInfo.InvariantCulture, _xmlProjectTemplate, _newSPSiteUrl, "true", _testRoleName, _roleDescription, _baseRoleName, addPerms)); SPSite site = new SPSite(_newSPSiteUrl); SPRole newRole = site.RootWeb.Roles[_testRoleName]; Assertion.AssertNotNull("New Role is null!", newRole); // Construct the rights the role should have for comparison. The // base permission of a role defaults to the permission of the // "Guest" role. From there we add anything other permissions // the role should have. SPRole guest = site.RootWeb.Roles[_baseRoleName]; SPRights rights = guest.PermissionMask | SPRights.ApplyStyleSheets | SPRights.ManageListPermissions | SPRights.ManageRoles | SPRights.CreateSSCSite; Assertion.AssertEquals("permissions were not correctly modified.", 0, rights.CompareTo(newRole.PermissionMask)); }
/// <summary> /// Task for adding a SPRole to a site. /// </summary> protected override void ExecuteTask() { try { SPSite site = new SPSite(Url); // Create the role by adding it to the site. site.RootWeb.Roles.Add(Role, Description, SPRights.EmptyMask); // Get a reference to the newly created role SPRole newRole = site.RootWeb.Roles[Role]; // Set the permission of the new role to the role specified. SetBaseRolePermissions(newRole); // Set any additional permissions AddPermissionsToRole(newRole); } catch (SPException ex) { throw new BuildException( string.Format("Cannot add role: {0} to site: {1}.", Role, Url), Location, ex); } }
public static Dictionary <string, SPRoleType> AddBasicSecurityToWorkspace(SPWeb eleWeb, string safeTitle, SPUser owner) { var safeGroupTitle = string.Empty; safeGroupTitle = CoreFunctions.GetSafeGroupTitle(safeTitle); eleWeb.AllowUnsafeUpdates = true; Dictionary <string, SPRoleType> pNewGroups = new Dictionary <string, SPRoleType>(); string[] grps = new[] { "Owner", "Member", "Visitor" }; SPGroup ownerGrp = null; // add groups and set group owners foreach (string grp in grps) { string finalName = string.Empty; SPGroup testGrp = null; try { testGrp = eleWeb.SiteGroups[safeGroupTitle + " " + grp]; } catch { } //Commented code is no longer use and commented to resolve Jira Id 2321 //if (testGrp != null) //{ // finalName = testGrp.Name; //} //else //{ try { finalName = CoreFunctions.AddGroup(eleWeb, safeGroupTitle, grp, owner, eleWeb.CurrentUser, string.Empty); eleWeb.Update(); } catch { } //} SPGroup finalGrp = eleWeb.SiteGroups[finalName]; SPRoleType rType; switch (grp) { case "Owner": ownerGrp = finalGrp; rType = SPRoleType.Administrator; finalGrp.Owner = owner; finalGrp.AddUser(owner); finalGrp.Update(); eleWeb.Update(); break; default: rType = SPRoleType.Reader; finalGrp.Owner = ownerGrp; finalGrp.Update(); eleWeb.Update(); break; } pNewGroups.Add(finalGrp.Name, rType); } // now set groups to become web's owner, member, visitor SPGroup group = null; SPRole roll = null; foreach (KeyValuePair <string, SPRoleType> g in pNewGroups) { if (g.Key.Contains("Owner")) { group = eleWeb.SiteGroups[g.Key]; eleWeb.AssociatedOwnerGroup = group; roll = eleWeb.Roles["Full Control"]; roll.AddGroup(group); } else if (g.Key.Contains("Member")) { group = eleWeb.SiteGroups[g.Key]; eleWeb.AssociatedOwnerGroup = group; roll = eleWeb.Roles["Contribute"]; roll.AddGroup(group); } else if (g.Key.Contains("Visitor")) { group = eleWeb.SiteGroups[g.Key]; eleWeb.AssociatedOwnerGroup = group; roll = eleWeb.Roles["Read"]; roll.AddGroup(group); } } eleWeb.Update(); return(pNewGroups); }
public void setUpGroups() { SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(myWebToPublish.Url)) { using (SPWeb web = site.OpenWeb()) { SPUser owner = null; try { owner = web.SiteUsers[_username]; }catch {} string aowner = ""; string amember = ""; string avisitor = ""; string strEPMLiveGroupsPermAssignments = ""; Guid lockweb = EPMLiveCore.CoreFunctions.getLockedWeb(web); if (lockweb != Guid.Empty) { using (SPWeb lweb = web.Site.OpenWeb(lockweb)) { aowner = EPMLiveCore.CoreFunctions.getConfigSetting(lweb, "EPMLiveNewProjectRoleOwners"); amember = EPMLiveCore.CoreFunctions.getConfigSetting(lweb, "EPMLiveNewProjectRoleMembers"); avisitor = EPMLiveCore.CoreFunctions.getConfigSetting(lweb, "EPMLiveNewProjectRoleVisitors"); strEPMLiveGroupsPermAssignments = EPMLiveCore.CoreFunctions.getConfigSetting(lweb, "EPMLiveGroupsPermAssignments"); } } web.Update(); //=========Owner Group======================== SPRole roll = web.Roles["Full Control"]; try { if (aowner != "") { web.AssociatedOwnerGroup = web.SiteGroups.GetByID(int.Parse(aowner)); roll.AddGroup(web.SiteGroups.GetByID(int.Parse(aowner))); } else { web.AssociatedOwnerGroup = GetSiteGroup(web, web.Title + " Administrators", owner); roll.AddGroup(web.SiteGroups[web.Title + " Administrators"]); } } catch (Exception ex) { myLog.WriteEntry("Error Setting up Owner Group: " + ex.Message + ex.StackTrace, EventLogEntryType.Error, 334); } //=========Member Group======================== try { if (amember != "") { web.AssociatedMemberGroup = web.SiteGroups.GetByID(int.Parse(amember)); roll = web.Roles["Contribute"]; roll.AddGroup(web.SiteGroups.GetByID(int.Parse(amember))); } else { web.AssociatedMemberGroup = GetSiteGroup(web, web.Title + " Members", owner); roll = web.Roles["Contribute"]; roll.AddGroup(web.SiteGroups[web.Title + " Members"]); } } catch (Exception ex) { myLog.WriteEntry("Error Setting up Member Group: " + ex.Message + ex.StackTrace, EventLogEntryType.Error, 334); } //=========Visitor Group======================== try { if (avisitor != "") { web.AssociatedVisitorGroup = web.SiteGroups.GetByID(int.Parse(avisitor)); roll = web.Roles["Read"]; roll.AddGroup(web.SiteGroups.GetByID(int.Parse(avisitor))); } else { web.AssociatedVisitorGroup = GetSiteGroup(web, web.Title + " Visitors", owner); roll = web.Roles["Read"]; roll.AddGroup(web.SiteGroups[web.Title + " Visitors"]); } } catch (Exception ex) { myLog.WriteEntry("Error Setting up Visitor Group: " + ex.Message + ex.StackTrace, EventLogEntryType.Error, 334); } web.Update(); if (strEPMLiveGroupsPermAssignments.Length > 1) { string[] strOuter = strEPMLiveGroupsPermAssignments.Split(new string[] { "|~|" }, StringSplitOptions.None); foreach (string strInner in strOuter) { string[] strInnerMost = strInner.Split('~'); SPGroup spGroup = web.SiteGroups.GetByID(Convert.ToInt32(strInnerMost[0])); //Persist groups & permissions to the database if (spGroup != null) { SPRoleAssignment spRA = new SPRoleAssignment(spGroup); spRA.RoleDefinitionBindings.Add(web.RoleDefinitions.GetById(Convert.ToInt32(strInnerMost[1]))); web.RoleAssignments.Add(spRA); } } } web.Update(); } } }); }
private string createSite(string url, SPSiteSubscription subscription, string title, string description, string user, string fullName, string email, string template, out Guid siteid, bool useHostHeader) { siteid = Guid.Empty; string errors = ""; try { SPSite site; if (subscription != null) { site = app.Sites.Add(subscription, url, title, description, 1033, "", user, fullName, email, null, null, null, useHostHeader); } else { site = app.Sites.Add(url, title, description, 1033, "", user, fullName, email, null, null, null, useHostHeader); } try { //SPSite site = spApp.Sites.Add(bUrl + url, user, email); site.AllowUnsafeUpdates = true; using (SPWeb web = site.OpenWeb()) { web.Title = title; web.AllowUnsafeUpdates = true; web.Site.AllowUnsafeUpdates = true; web.Site.RootWeb.AllowUnsafeUpdates = true; //SPDocumentLibrary solGallery1 = (SPDocumentLibrary)web.Site.RootWeb.Site.GetCatalog(SPListTemplateType.SolutionCatalog); string[] files = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles) + "\\workengine\\templates\\" + template); int counter = 1; foreach (string file in files) { addfile(Path.GetFileNameWithoutExtension(file), web, file, counter); counter++; } SPWebTemplate webtemplate = null; foreach (SPWebTemplate t in web.GetAvailableWebTemplates((uint)web.Locale.LCID)) { if (t.Title == template) { webtemplate = t; break; } } if (webtemplate != null) { web.ApplyWebTemplate(webtemplate); } //web.Update(); //web.AllUsers.Add(user, email, fullName, ""); ////web.AllUsers.Add(System.Configuration.ConfigurationManager.AppSettings["owner"].ToString(), "", System.Configuration.ConfigurationManager.AppSettings["owner"].ToString(), ""); //web.Users[user].Name = fullName; //web.Users[user].Update(); //web.Update(); SPUser owner = web.AllUsers[user]; web.SiteGroups.Add("Administrators", owner, owner, ""); //web.Update(); web.AssociatedOwnerGroup = GetSiteGroup(web, "Administrators"); SPRole roll = web.Roles["Full Control"]; roll.AddGroup(web.SiteGroups["Administrators"]); SPMember newOwner = web.SiteGroups["Administrators"]; web.SiteGroups.Add("Team Members", newOwner, owner, ""); web.SiteGroups.Add("Visitors", newOwner, owner, ""); web.SiteGroups.Add("Project Managers", newOwner, owner, ""); //web.Update(); web.AssociatedVisitorGroup = GetSiteGroup(web, "Visitors"); web.AssociatedOwnerGroup = GetSiteGroup(web, "Administrators"); web.AssociatedMemberGroup = GetSiteGroup(web, "Team Members"); // web.Update(); //web.SiteGroups["Administrators"].Users[user].Name = fullName; //web.SiteGroups["Project Managers"].Users[user].Name = fullName; //web.SiteGroups["Team Members"].Users[user].Name = fullName; //web.SiteGroups["Visitors"].Users[user].Name = fullName; web.Roles.Add("Contribute2", "Can view, add, update, delete and manage subwebs", web.Roles["Contribute"].PermissionMask | SPRights.ManageSubwebs); roll = web.Roles["Full Control"]; roll.AddGroup(web.SiteGroups["Administrators"]); roll = web.Roles["Contribute"]; roll.AddGroup(web.SiteGroups["Team Members"]); roll = web.Roles["Read"]; roll.AddGroup(web.SiteGroups["Visitors"]); roll = web.Roles["Contribute2"]; roll.AddGroup(web.SiteGroups["Project Managers"]); siteid = site.ID; if (txtDatabaseServer.Text != "") { errors = mapReports(site); } } } catch (Exception ex) { errors = ex.ToString(); } finally { if (site != null) { site.Dispose(); } } } catch (Exception ex) { errors = ex.Message; } return(errors); }