Created
September 25, 2014 09:54
-
-
Save asmagin/b88357c92812ebe0c137 to your computer and use it in GitHub Desktop.
Users and roles init
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<pipelines> | |
<initialize> | |
... | |
<processor type="SampleSite.RolesInitializer, SampleSite"> | |
<processor type="SampleSite.UsersInitializer, SampleSite"> | |
... | |
</initialize> | |
</pipelines> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace SampleSite | |
{ | |
using System.Linq; | |
using System.Web.Security; | |
using Sitecore.Diagnostics; | |
using Sitecore.Pipelines; | |
using Sitecore.Security.Accounts; | |
public class RolesInitializer | |
{ | |
public const string ContentContentEditorRole = @"sitecore\sample_content_editor"; | |
private const string SitecoreAuthorRole = @"sitecore\Author"; | |
private const string SitecoreClientPublishingRole = @"sitecore\Sitecore Client Publishing"; | |
private const string SitecoreDesignerRole = @"sitecore\Designer"; | |
public virtual void Process(PipelineArgs args) | |
{ | |
Assert.ArgumentNotNull(args, "args"); | |
Assert.IsTrue(Role.Exists(SitecoreAuthorRole), SitecoreAuthorRole + " is missing"); | |
Assert.IsTrue(Role.Exists(SitecoreDesignerRole), SitecoreDesignerRole + " is missing"); | |
Assert.IsTrue(Role.Exists(SitecoreClientPublishingRole), SitecoreClientPublishingRole + " is missing"); | |
this.CreateRole(ContentContentEditorRole, SitecoreDesignerRole, SitecoreAuthorRole, SitecoreClientPublishingRole); | |
} | |
protected virtual void CreateRole(string name, params string[] args) | |
{ | |
// check & create content_editor role | |
if (!Role.Exists(name)) | |
{ | |
Roles.CreateRole(name); | |
} | |
if (args != null && args.Length > 0) | |
{ | |
foreach (var s in args.Where(s => !RolesInRolesManager.IsRoleInRole(Role.FromName(name), Role.FromName(s), false))) | |
{ | |
RolesInRolesManager.AddRoleToRole(Role.FromName(name), Role.FromName(s)); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment