mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Simple dynamic permissions creation
- Other permissions pending --HG-- branch : dev
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using Orchard.ContentManagement.MetaData;
|
||||
using Orchard.ContentManagement.MetaData.Models;
|
||||
using Orchard.Core.Contents.Settings;
|
||||
using Orchard.Environment.Extensions.Models;
|
||||
using Orchard.Security.Permissions;
|
||||
|
||||
namespace Orchard.ContentTypes {
|
||||
public class DynamicPermissions : IPermissionProvider {
|
||||
public static readonly Permission ManageContentType = new Permission { Name = "ManageContentType{0}", Description = "Manage {0}" };
|
||||
|
||||
private readonly IContentDefinitionManager _contentDefinitionManager;
|
||||
|
||||
public virtual Feature Feature { get; set; }
|
||||
|
||||
public DynamicPermissions(IContentDefinitionManager contentDefinitionManager) {
|
||||
_contentDefinitionManager = contentDefinitionManager;
|
||||
}
|
||||
|
||||
public IEnumerable<Permission> GetPermissions() {
|
||||
// manage rights only for Creatable types
|
||||
var creatableTypes = _contentDefinitionManager.ListTypeDefinitions()
|
||||
.Where(ctd => ctd.Settings.GetModel<ContentTypeSettings>().Creatable);
|
||||
|
||||
foreach(var typeDefinition in creatableTypes) {
|
||||
yield return CreateDynamicPersion(ManageContentType, typeDefinition);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<PermissionStereotype> GetDefaultStereotypes() {
|
||||
return new[] {
|
||||
new PermissionStereotype {
|
||||
Name = "Administrator",
|
||||
Permissions = _contentDefinitionManager.ListTypeDefinitions().Select(typeDefinition => CreateDynamicPersion(ManageContentType, typeDefinition))
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static Permission CreateDynamicPersion(Permission template, ContentTypeDefinition typeDefinition) {
|
||||
return new Permission {
|
||||
Name = String.Format(template.Name, typeDefinition.Name),
|
||||
Description = String.Format(template.Description, typeDefinition.DisplayName),
|
||||
Category = typeDefinition.DisplayName
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@@ -70,6 +70,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AdminMenu.cs" />
|
||||
<Compile Include="DynamicPermissions.cs" />
|
||||
<Compile Include="ResourceManifest.cs" />
|
||||
<Compile Include="Extensions\StringExtensions.cs" />
|
||||
<Compile Include="ViewModels\AddPartsViewModel.cs" />
|
||||
|
Reference in New Issue
Block a user