2017-09-23 10:08:18 +02:00
|
|
|
|
using System;
|
2017-09-12 16:22:16 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.CodeAnnotations;
|
|
|
|
|
|
using Umbraco.Core.Models.Membership;
|
|
|
|
|
|
using Umbraco.Core.Services;
|
2018-10-29 17:27:33 +11:00
|
|
|
|
using Umbraco.Web.Actions;
|
2017-09-13 17:35:20 +02:00
|
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
2018-10-29 17:27:33 +11:00
|
|
|
|
|
2017-09-12 16:22:16 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Models.Mapping
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Converts an IUserGroup instance into a dictionary of permissions by category
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal class UserGroupDefaultPermissionsResolver
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ILocalizedTextService _textService;
|
|
|
|
|
|
private readonly ActionCollection _actions;
|
|
|
|
|
|
|
|
|
|
|
|
public UserGroupDefaultPermissionsResolver(ILocalizedTextService textService, ActionCollection actions)
|
|
|
|
|
|
{
|
|
|
|
|
|
_actions = actions;
|
|
|
|
|
|
_textService = textService ?? throw new ArgumentNullException(nameof(textService));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IDictionary<string, IEnumerable<Permission>> Resolve(IUserGroup source)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _actions
|
|
|
|
|
|
.Where(x => x.CanBePermissionAssigned)
|
|
|
|
|
|
.Select(x => GetPermission(x, source))
|
|
|
|
|
|
.GroupBy(x => x.Category)
|
|
|
|
|
|
.ToDictionary(x => x.Key, x => (IEnumerable<Permission>) x.ToArray());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Permission GetPermission(IAction action, IUserGroup source)
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = new Permission();
|
2018-10-29 17:27:33 +11:00
|
|
|
|
|
|
|
|
|
|
result.Category = action.Category.IsNullOrWhiteSpace()
|
2017-09-12 16:22:16 +02:00
|
|
|
|
? _textService.Localize($"actionCategories/{Constants.Conventions.PermissionCategories.OtherCategory}")
|
2018-10-29 17:27:33 +11:00
|
|
|
|
: _textService.Localize($"actionCategories/{action.Category}");
|
|
|
|
|
|
result.Name = _textService.Localize($"actions/{action.Alias}");
|
2017-09-12 16:22:16 +02:00
|
|
|
|
result.Description = _textService.Localize($"actionDescriptions/{action.Alias}");
|
|
|
|
|
|
result.Icon = action.Icon;
|
|
|
|
|
|
result.Checked = source.Permissions != null && source.Permissions.Contains(action.Letter.ToString(CultureInfo.InvariantCulture));
|
|
|
|
|
|
result.PermissionCode = action.Letter.ToString(CultureInfo.InvariantCulture);
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-09-23 10:08:18 +02:00
|
|
|
|
}
|