mirror of https://github.com/Squidex/squidex.git
8 changed files with 109 additions and 11 deletions
@ -0,0 +1,39 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure.Json; |
|||
using Squidex.Infrastructure.Security; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Collections.Immutable; |
|||
using System.Linq; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Apps.Json |
|||
{ |
|||
public sealed class RolesConverter : JsonClassConverter<Roles> |
|||
{ |
|||
protected override void WriteValue(JsonWriter writer, Roles value, JsonSerializer serializer) |
|||
{ |
|||
var json = new Dictionary<string, string[]>(value.Count); |
|||
|
|||
foreach (var role in value) |
|||
{ |
|||
json.Add(role.Key, role.Value.Permissions.ToIds().ToArray()); |
|||
} |
|||
|
|||
serializer.Serialize(writer, json); |
|||
} |
|||
|
|||
protected override Roles ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var json = serializer.Deserialize<Dictionary<string, string[]>>(reader); |
|||
|
|||
return new Roles(json.ToImmutableDictionary(x => x.Key, x => new Role(x.Key, new PermissionSet(x.Value)))); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using FluentAssertions; |
|||
using Newtonsoft.Json; |
|||
using Newtonsoft.Json.Linq; |
|||
using Squidex.Domain.Apps.Core.Apps; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Model.Apps |
|||
{ |
|||
public class RolesJsonTests |
|||
{ |
|||
private readonly JsonSerializer serializer = TestData.DefaultSerializer(); |
|||
|
|||
[Fact] |
|||
public void Should_serialize_and_deserialize() |
|||
{ |
|||
var sut = Roles.CreateDefaults("my-app"); |
|||
|
|||
var serialized = JToken.FromObject(sut, serializer).ToObject<Roles>(serializer); |
|||
|
|||
serialized.Should().BeEquivalentTo(sut); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,31 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using Squidex.Domain.Apps.Core.Apps; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Model.Apps |
|||
{ |
|||
public class RolesTests |
|||
{ |
|||
[Fact] |
|||
public void Should_create_defaults() |
|||
{ |
|||
var sut = Roles.CreateDefaults("my-app"); |
|||
|
|||
Assert.Equal(4, sut.Count); |
|||
|
|||
foreach (var role in sut) |
|||
{ |
|||
foreach (var permission in role.Value.Permissions) |
|||
{ |
|||
Assert.StartsWith("squidex.apps.my-app", permission.Id); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue