|
1 | 1 | // Copyright (c) Umbraco. |
2 | 2 | // See LICENSE for more details. |
3 | 3 |
|
4 | | -using System.Collections.Generic; |
5 | | -using System.Linq; |
6 | 4 | using Microsoft.Extensions.Logging; |
7 | 5 | using Microsoft.Extensions.Options; |
8 | 6 | using Moq; |
9 | 7 | using NUnit.Framework; |
| 8 | +using Umbraco.Cms.Api.Management.Mapping.Permissions; |
10 | 9 | using Umbraco.Cms.Core; |
11 | 10 | using Umbraco.Cms.Core.Cache; |
12 | 11 | using Umbraco.Cms.Core.Configuration.Models; |
13 | 12 | using Umbraco.Cms.Core.IO; |
14 | 13 | using Umbraco.Cms.Core.Mapping; |
15 | 14 | using Umbraco.Cms.Core.Models; |
16 | | -using Umbraco.Cms.Core.Models.ContentEditing; |
| 15 | +using Umbraco.Cms.Core.Models.Membership; |
| 16 | +using Umbraco.Cms.Core.Models.Membership.Permissions; |
17 | 17 | using Umbraco.Cms.Core.Persistence; |
18 | 18 | using Umbraco.Cms.Core.Persistence.Repositories; |
19 | 19 | using Umbraco.Cms.Core.Services; |
@@ -52,8 +52,11 @@ internal sealed class ContentTypeRepositoryTest : UmbracoIntegrationTest |
52 | 52 | private IMediaTypeRepository MediaTypeRepository => GetRequiredService<IMediaTypeRepository>(); |
53 | 53 |
|
54 | 54 | private IDocumentRepository DocumentRepository => GetRequiredService<IDocumentRepository>(); |
| 55 | + |
55 | 56 | private IContentService ContentService => GetRequiredService<IContentService>(); |
56 | 57 |
|
| 58 | + private IUserGroupRepository UserGroupRepository => GetRequiredService<IUserGroupRepository>(); |
| 59 | + |
57 | 60 | private ContentTypeRepository ContentTypeRepository => |
58 | 61 | (ContentTypeRepository)GetRequiredService<IContentTypeRepository>(); |
59 | 62 |
|
@@ -918,4 +921,83 @@ public void Can_Update_Variation_Of_Element_Type_Property() |
918 | 921 | Assert.That(hasCulture, Is.True); |
919 | 922 | } |
920 | 923 | } |
| 924 | + |
| 925 | + [Test] |
| 926 | + public void Can_Remove_Property_Value_Permissions_On_Removal_Of_Property_Types() |
| 927 | + { |
| 928 | + var provider = ScopeProvider; |
| 929 | + using (var scope = provider.CreateScope()) |
| 930 | + { |
| 931 | + // Create, save and re-retrieve a content type and user group. |
| 932 | + IContentType contentType = ContentTypeBuilder.CreateSimpleContentType(defaultTemplateId: 0); |
| 933 | + ContentTypeRepository.Save(contentType); |
| 934 | + contentType = ContentTypeRepository.Get(contentType.Id); |
| 935 | + |
| 936 | + var userGroup = CreateUserGroupWithGranularPermissions(contentType); |
| 937 | + |
| 938 | + // Remove property types and verify that the permission is removed from the user group. |
| 939 | + contentType.RemovePropertyType("author"); |
| 940 | + ContentTypeRepository.Save(contentType); |
| 941 | + userGroup = UserGroupRepository.Get(userGroup.Id); |
| 942 | + Assert.AreEqual(3, userGroup.GranularPermissions.Count); |
| 943 | + |
| 944 | + contentType.RemovePropertyType("bodyText"); |
| 945 | + ContentTypeRepository.Save(contentType); |
| 946 | + userGroup = UserGroupRepository.Get(userGroup.Id); |
| 947 | + Assert.AreEqual(2, userGroup.GranularPermissions.Count); |
| 948 | + |
| 949 | + contentType.RemovePropertyType("title"); |
| 950 | + ContentTypeRepository.Save(contentType); |
| 951 | + userGroup = UserGroupRepository.Get(userGroup.Id); |
| 952 | + Assert.AreEqual(0, userGroup.GranularPermissions.Count); |
| 953 | + } |
| 954 | + } |
| 955 | + |
| 956 | + [Test] |
| 957 | + public void Can_Remove_Property_Value_Permissions_On_Removal_Of_Content_Type() |
| 958 | + { |
| 959 | + var provider = ScopeProvider; |
| 960 | + using (var scope = provider.CreateScope()) |
| 961 | + { |
| 962 | + // Create, save and re-retrieve a content type and user group. |
| 963 | + IContentType contentType = ContentTypeBuilder.CreateSimpleContentType(defaultTemplateId: 0); |
| 964 | + ContentTypeRepository.Save(contentType); |
| 965 | + contentType = ContentTypeRepository.Get(contentType.Id); |
| 966 | + |
| 967 | + var userGroup = CreateUserGroupWithGranularPermissions(contentType); |
| 968 | + |
| 969 | + // Remove the content type and verify all permissions are removed from the user group. |
| 970 | + ContentTypeRepository.Delete(contentType); |
| 971 | + userGroup = UserGroupRepository.Get(userGroup.Id); |
| 972 | + Assert.AreEqual(0, userGroup.GranularPermissions.Count); |
| 973 | + } |
| 974 | + } |
| 975 | + |
| 976 | + private IUserGroup CreateUserGroupWithGranularPermissions(IContentType contentType) |
| 977 | + { |
| 978 | + DocumentPropertyValueGranularPermission CreatePermission(IPropertyType propertyType, string permission = "") |
| 979 | + => new() |
| 980 | + { |
| 981 | + Key = contentType.Key, |
| 982 | + Permission = propertyType.Key.ToString().ToLowerInvariant() + "|" + permission, |
| 983 | + }; |
| 984 | + |
| 985 | + var titlePropertyType = contentType.PropertyTypes.Single(x => x.Alias == "title"); |
| 986 | + var bodyTextPropertyType = contentType.PropertyTypes.Single(x => x.Alias == "bodyText"); |
| 987 | + var authorPropertyType = contentType.PropertyTypes.Single(x => x.Alias == "author"); |
| 988 | + |
| 989 | + var userGroup = new UserGroupBuilder() |
| 990 | + .WithGranularPermissions([ |
| 991 | + CreatePermission(titlePropertyType, "Umb.Document.PropertyValue.Read"), |
| 992 | + CreatePermission(titlePropertyType, "Umb.Document.PropertyValue.Write"), |
| 993 | + CreatePermission(bodyTextPropertyType, "Umb.Document.PropertyValue.Read"), |
| 994 | + CreatePermission(authorPropertyType) |
| 995 | + ]) |
| 996 | + .Build(); |
| 997 | + UserGroupRepository.Save(userGroup); |
| 998 | + userGroup = UserGroupRepository.Get(userGroup.Id); |
| 999 | + |
| 1000 | + Assert.AreEqual(4, userGroup.GranularPermissions.Count); |
| 1001 | + return userGroup; |
| 1002 | + } |
921 | 1003 | } |
0 commit comments