Last active
January 10, 2020 17:01
-
-
Save garpunkal/ce9cc465d46213c8ff586f6136a51e9a to your computer and use it in GitHub Desktop.
CreateUserGroups
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
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using Umbraco.Core; | |
using Umbraco.Core.Logging; | |
using Umbraco.Core.Models; | |
using Umbraco.Core.Models.Membership; | |
using Umbraco.Core.Services; | |
using Umbraco.Core.Strings; | |
using Umbraco.Web; | |
using Umbraco.Web.Mvc; | |
namespace Web.Controllers | |
{ | |
public class CmsController : UmbracoAuthorizedController | |
{ | |
private readonly UmbracoHelper _umbracoHelper; | |
private readonly IUserService _userService; | |
private readonly IContentService _contentService; | |
private List<string> _excludedUserGroups = new List<string>() { "admin", "sensitiveData", "editor", "translator", "viewer" }; | |
private const string _groupAliasPrefix = "msm_"; | |
private List<char> _browseCopyPermissions = new List<char>() { 'F', 'O' }; | |
public CmsController(UmbracoHelper umbracoHelper, IUserService userService, IContentService contentService) | |
{ | |
_umbracoHelper = umbracoHelper; | |
_userService = userService; | |
_contentService = contentService; | |
} | |
public ActionResult Welcome() | |
{ | |
return PartialView("Partials/CMS/Welcome"); | |
} | |
public class AddedUserGroup | |
{ | |
public string Alias { get; set; } | |
} | |
/// <summary> | |
/// https://our.umbraco.com/forum/extending-umbraco-and-using-the-api/97511-default-user-group-permissions | |
/// </summary> | |
/// <returns></returns> | |
public ActionResult CreateUserGroups() | |
{ | |
var globalHomepage = _umbracoHelper.TypedContentAtRoot().FirstOrDefault(x => x.DocumentTypeAlias == "globalHomepage"); | |
if (globalHomepage == null) throw new NullReferenceException("globalHomepage"); | |
var globalContent = globalHomepage.FirstChild("site"); | |
if (globalContent == null) throw new NullReferenceException("globalContent"); | |
var markets = globalContent.Children("market"); | |
//var markets = globalContent.Children("language"); | |
if (!markets?.Any() ?? true) throw new NullReferenceException("markets"); | |
IEnumerable<IPublishedContent> mediaRootItems = _umbracoHelper.TypedMediaAtRoot(); | |
if (mediaRootItems == null) throw new NullReferenceException("mediaRootItems"); | |
var globalMedia = mediaRootItems?.FirstOrDefault(x => string.Equals(x.Name, "Media", StringComparison.OrdinalIgnoreCase)); | |
if (globalMedia == null) throw new NullReferenceException("globalMedia"); | |
var groupsAdded = new List<AddedUserGroup>(); | |
foreach (IPublishedContent market in markets) | |
{ | |
var alias = _groupAliasPrefix + market.Name.ToCleanString(CleanStringType.UnderscoreAlias); | |
UserGroup group = (UserGroup)_userService.GetUserGroupByAlias(alias); | |
if (group == null || group.Id == 0) | |
group = new UserGroup | |
{ | |
Alias = alias, | |
Key = Guid.NewGuid() | |
}; | |
group.Name = $"Editor - {market.Name}"; | |
group.Icon = "icon-umb-translation color-purple"; | |
group.Permissions = new List<string> { "K", "F", "D", "C", "U", "A", "O", "M", "S", "Z" }; | |
group.StartContentId = globalContent?.Id ?? 0; | |
group.StartMediaId = globalMedia?.Id ?? 0; | |
group.AddAllowedSection("content"); | |
group.AddAllowedSection("media"); | |
group.AddAllowedSection("users"); | |
_userService.Save(group); | |
_userService.ReplaceUserGroupPermissions(group.Id, | |
_browseCopyPermissions, | |
markets | |
.Where(x => x.Id != market.Id) | |
.Select(x => x.Id) | |
.ToArray()); | |
groupsAdded.Add(new AddedUserGroup | |
{ | |
Alias = group.Alias, | |
}); | |
} | |
return Json(JsonConvert.SerializeObject(groupsAdded), JsonRequestBehavior.AllowGet); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment