mirror of https://github.com/Squidex/squidex.git
75 changed files with 463 additions and 420 deletions
@ -0,0 +1,25 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using MongoDB.Bson.Serialization; |
||||
|
using Squidex.Domain.Apps.Entities.Contents.Text.State; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Contents.Text; |
||||
|
|
||||
|
internal static class MongoTextStateClassMap |
||||
|
{ |
||||
|
public static void RegisterClassMap() |
||||
|
{ |
||||
|
BsonClassMap.RegisterClassMap<TextContentState>(cm => |
||||
|
{ |
||||
|
cm.MapIdProperty(x => x.UniqueContentId); |
||||
|
|
||||
|
cm.MapProperty(x => x.State) |
||||
|
.SetElementName("s"); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using MongoDB.Bson.Serialization; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.History; |
||||
|
|
||||
|
internal static class MongoHistoryClassMap |
||||
|
{ |
||||
|
public static void RegisterClassMap() |
||||
|
{ |
||||
|
BsonClassMap.RegisterClassMap<HistoryEvent>(cm => |
||||
|
{ |
||||
|
cm.AutoMap(); |
||||
|
|
||||
|
cm.MapProperty(x => x.OwnerId) |
||||
|
.SetElementName("AppId"); |
||||
|
|
||||
|
cm.MapProperty(x => x.EventType) |
||||
|
.SetElementName("Message"); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,37 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using Microsoft.AspNetCore.Identity; |
||||
|
using MongoDB.Bson.Serialization; |
||||
|
using MongoDB.Bson.Serialization.Serializers; |
||||
|
|
||||
|
namespace Squidex.Domain.Users; |
||||
|
|
||||
|
internal sealed class IdentityUserForwardingSerializer |
||||
|
: SerializerBase<IdentityUser>, IBsonDocumentSerializer |
||||
|
{ |
||||
|
private static readonly IBsonSerializer<MongoUser> MongoUserSerializer = |
||||
|
BsonSerializer.LookupSerializer<MongoUser>(); |
||||
|
|
||||
|
public override IdentityUser Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) |
||||
|
{ |
||||
|
args.NominalType = typeof(MongoUser); |
||||
|
return MongoUserSerializer.Deserialize(context, args); |
||||
|
} |
||||
|
|
||||
|
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, IdentityUser value) |
||||
|
{ |
||||
|
args.NominalType = typeof(MongoUser); |
||||
|
MongoUserSerializer.Serialize(context, args, (MongoUser)value); |
||||
|
} |
||||
|
|
||||
|
public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo) |
||||
|
{ |
||||
|
return ((IBsonDocumentSerializer)MongoUserSerializer) |
||||
|
.TryGetMemberSerializationInfo(memberName, out serializationInfo); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,116 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Security.Claims; |
||||
|
using Microsoft.AspNetCore.Identity; |
||||
|
using MongoDB.Bson; |
||||
|
using MongoDB.Bson.Serialization; |
||||
|
using MongoDB.Bson.Serialization.Serializers; |
||||
|
|
||||
|
namespace Squidex.Domain.Users; |
||||
|
|
||||
|
internal static class MongoIdentityClassMap |
||||
|
{ |
||||
|
public static void RegisterClassMap() |
||||
|
{ |
||||
|
BsonClassMap.RegisterClassMap<IdentityRole<string>>(cm => |
||||
|
{ |
||||
|
cm.AutoMap(); |
||||
|
|
||||
|
cm.MapMember(x => x.Id) |
||||
|
.SetSerializer(new StringSerializer(BsonType.ObjectId)); |
||||
|
|
||||
|
cm.UnmapMember(x => x.ConcurrencyStamp); |
||||
|
}); |
||||
|
|
||||
|
BsonClassMap.RegisterClassMap<Claim>(cm => |
||||
|
{ |
||||
|
cm.MapConstructor(typeof(Claim).GetConstructors() |
||||
|
.First(x => |
||||
|
{ |
||||
|
var parameters = x.GetParameters(); |
||||
|
|
||||
|
return parameters.Length == 2 && |
||||
|
parameters[0].Name == "type" && |
||||
|
parameters[0].ParameterType == typeof(string) && |
||||
|
parameters[1].Name == "value" && |
||||
|
parameters[1].ParameterType == typeof(string); |
||||
|
})) |
||||
|
.SetArguments( |
||||
|
[ |
||||
|
nameof(Claim.Type), |
||||
|
nameof(Claim.Value), |
||||
|
]); |
||||
|
|
||||
|
cm.MapMember(x => x.Type); |
||||
|
cm.MapMember(x => x.Value); |
||||
|
}); |
||||
|
|
||||
|
BsonClassMap.RegisterClassMap<UserLogin>(cm => |
||||
|
{ |
||||
|
cm.MapConstructor(typeof(UserLogin).GetConstructors() |
||||
|
.First(x => |
||||
|
{ |
||||
|
var parameters = x.GetParameters(); |
||||
|
|
||||
|
return parameters.Length == 3; |
||||
|
})) |
||||
|
.SetArguments( |
||||
|
[ |
||||
|
nameof(UserLogin.LoginProvider), |
||||
|
nameof(UserLogin.ProviderKey), |
||||
|
nameof(UserLogin.ProviderDisplayName), |
||||
|
]); |
||||
|
|
||||
|
cm.AutoMap(); |
||||
|
}); |
||||
|
|
||||
|
BsonClassMap.RegisterClassMap<IdentityUserToken<string>>(cm => |
||||
|
{ |
||||
|
cm.AutoMap(); |
||||
|
|
||||
|
cm.UnmapMember(x => x.UserId); |
||||
|
}); |
||||
|
|
||||
|
BsonClassMap.RegisterClassMap<IdentityUser<string>>(cm => |
||||
|
{ |
||||
|
cm.AutoMap(); |
||||
|
|
||||
|
cm.MapMember(x => x.Id) |
||||
|
.SetSerializer(new StringSerializer(BsonType.ObjectId)); |
||||
|
|
||||
|
cm.MapMember(x => x.AccessFailedCount) |
||||
|
.SetIgnoreIfDefault(true); |
||||
|
|
||||
|
cm.MapMember(x => x.EmailConfirmed) |
||||
|
.SetIgnoreIfDefault(true); |
||||
|
|
||||
|
cm.MapMember(x => x.LockoutEnd) |
||||
|
.SetElementName("LockoutEndDateUtc").SetIgnoreIfNull(true); |
||||
|
|
||||
|
cm.MapMember(x => x.LockoutEnabled) |
||||
|
.SetIgnoreIfDefault(true); |
||||
|
|
||||
|
cm.MapMember(x => x.PasswordHash) |
||||
|
.SetIgnoreIfNull(true); |
||||
|
|
||||
|
cm.MapMember(x => x.PhoneNumber) |
||||
|
.SetIgnoreIfNull(true); |
||||
|
|
||||
|
cm.MapMember(x => x.PhoneNumberConfirmed) |
||||
|
.SetIgnoreIfDefault(true); |
||||
|
|
||||
|
cm.MapMember(x => x.SecurityStamp) |
||||
|
.SetIgnoreIfNull(true); |
||||
|
|
||||
|
cm.MapMember(x => x.TwoFactorEnabled) |
||||
|
.SetIgnoreIfDefault(true); |
||||
|
}); |
||||
|
|
||||
|
BsonSerializer.RegisterSerializer(new IdentityUserForwardingSerializer()); |
||||
|
} |
||||
|
} |
||||
@ -1,31 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// Squidex Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
||||
// All rights reserved. Licensed under the MIT license.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using MongoDB.Driver; |
|
||||
using Squidex.Infrastructure.Json.Objects; |
|
||||
|
|
||||
namespace Squidex.Infrastructure; |
|
||||
|
|
||||
public static class MongoClientFactory |
|
||||
{ |
|
||||
public static MongoClient Create(string? connectionString, Action<MongoClientSettings>? configure = null) |
|
||||
{ |
|
||||
BsonDefaultConventions.Register(); |
|
||||
BsonDomainIdSerializer.Register(); |
|
||||
BsonEscapedDictionarySerializer<JsonValue, JsonObject>.Register(); |
|
||||
BsonInstantSerializer.Register(); |
|
||||
BsonJsonValueSerializer.Register(); |
|
||||
BsonStringSerializer<RefToken>.Register(); |
|
||||
|
|
||||
var clientSettings = MongoClientSettings.FromConnectionString(connectionString); |
|
||||
|
|
||||
// If we really need custom config.
|
|
||||
configure?.Invoke(clientSettings); |
|
||||
|
|
||||
return new MongoClient(clientSettings); |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,59 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Text.Json; |
||||
|
using MongoDB.Bson; |
||||
|
using MongoDB.Driver; |
||||
|
using Squidex.Domain.Apps.Entities; |
||||
|
using Squidex.Domain.Apps.Entities.Assets; |
||||
|
using Squidex.Domain.Apps.Entities.Contents; |
||||
|
using Squidex.Domain.Apps.Entities.Contents.Text; |
||||
|
using Squidex.Domain.Apps.Entities.History; |
||||
|
using Squidex.Domain.Apps.Entities.MongoDb; |
||||
|
using Squidex.Domain.Users; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Squidex.Infrastructure.Json.Objects; |
||||
|
|
||||
|
namespace Squidex; |
||||
|
|
||||
|
public static class MongoClientFactory |
||||
|
{ |
||||
|
public static void SetupSerializer(JsonSerializerOptions jsonSerializerOptions, BsonType representation) |
||||
|
{ |
||||
|
// Register the serializers first.
|
||||
|
BsonDomainIdSerializer.Register(); |
||||
|
BsonEscapedDictionarySerializer<JsonValue, JsonObject>.Register(); |
||||
|
BsonInstantSerializer.Register(); |
||||
|
BsonJsonValueSerializer.Register(); |
||||
|
BsonStringSerializer<RefToken>.Register(); |
||||
|
BsonUniqueContentIdSerializer.Register(); |
||||
|
BsonJsonConvention.Register(jsonSerializerOptions, representation); |
||||
|
|
||||
|
// Keep the order, because we have an inheritance between entities.
|
||||
|
MongoEntityClassMap.RegisterClassMap(); |
||||
|
MongoAppEntityClassMap.RegisterClassMap(); |
||||
|
MongoAssetItemClassMap.RegisterClassMap(); |
||||
|
MongoAssetEntity.RegisterClassMap(); |
||||
|
MongoAssetFolderEntity.RegisterClassMap(); |
||||
|
MongoContentEntity.RegisterClassMap(); |
||||
|
MongoHistoryClassMap.RegisterClassMap(); |
||||
|
MongoIdentityClassMap.RegisterClassMap(); |
||||
|
MongoTextStateClassMap.RegisterClassMap(); |
||||
|
|
||||
|
BsonDefaultConventions.Register(); |
||||
|
} |
||||
|
|
||||
|
public static MongoClient Create(string? connectionString, Action<MongoClientSettings>? configure = null) |
||||
|
{ |
||||
|
var clientSettings = MongoClientSettings.FromConnectionString(connectionString); |
||||
|
|
||||
|
// If we really need custom config.
|
||||
|
configure?.Invoke(clientSettings); |
||||
|
|
||||
|
return new MongoClient(clientSettings); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using MongoDB.Bson; |
||||
|
using Squidex.Domain.Apps.Core.TestHelpers; |
||||
|
using Squidex.MongoDb.TestHelpers; |
||||
|
|
||||
|
[assembly: AssemblyFixture(typeof(MongoSerializerFixture))] |
||||
|
|
||||
|
namespace Squidex.MongoDb.TestHelpers; |
||||
|
|
||||
|
internal sealed class MongoSerializerFixture : IDisposable |
||||
|
{ |
||||
|
public MongoSerializerFixture() |
||||
|
{ |
||||
|
MongoClientFactory.SetupSerializer(TestUtils.DefaultOptions(), BsonType.String); |
||||
|
} |
||||
|
|
||||
|
public void Dispose() |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue