Skip to content

Commit d457803

Browse files
author
github-actions
committed
fix: derived properties not saved
BREAKING CHANGE: RavenDb ids
1 parent 51b0b7b commit d457803

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed

src/Aguacongas.AspNetCore.Authentication.RavenDb/DynamicProviderStore.cs

+19-30
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ public class DynamicProviderStore<TSchemeDefinition> : IDynamicProviderStore<TSc
3737
private readonly IAsyncDocumentSession _session;
3838
private readonly IAuthenticationSchemeOptionsSerializer _authenticationSchemeOptionsSerializer;
3939
private readonly ILogger<DynamicProviderStore<TSchemeDefinition>> _logger;
40+
private readonly string _entitybasePath;
4041

4142
/// <summary>
4243
/// Gets the scheme definitions list.
4344
/// </summary>
4445
/// <value>
4546
/// The scheme definitions list.
4647
/// </value>
47-
public virtual IQueryable<TSchemeDefinition> SchemeDefinitions => _session.Query<SerializedData>()
48+
public virtual IQueryable<TSchemeDefinition> SchemeDefinitions => _session.Query<TSchemeDefinition>()
4849
.ToListAsync().ConfigureAwait(false).GetAwaiter().GetResult()
4950
.Select(Deserialize)
5051
.AsQueryable();
@@ -67,6 +68,7 @@ public DynamicProviderStore(IAsyncDocumentSession session, IAuthenticationScheme
6768
_session = session ?? throw new ArgumentNullException(nameof(session));
6869
_authenticationSchemeOptionsSerializer = authenticationSchemeOptionsSerializer ?? throw new ArgumentNullException(nameof(authenticationSchemeOptionsSerializer));
6970
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
71+
_entitybasePath = typeof(TSchemeDefinition).Name.ToLower() + "/";
7072
}
7173

7274
/// <summary>
@@ -83,7 +85,7 @@ public virtual async Task AddAsync(TSchemeDefinition definition, CancellationTok
8385
cancellationToken.ThrowIfCancellationRequested();
8486

8587
var data = Serialize(definition);
86-
await _session.StoreAsync(data, cancellationToken).ConfigureAwait(false);
88+
await _session.StoreAsync(data, $"{_entitybasePath}{definition.Scheme}", cancellationToken).ConfigureAwait(false);
8789
await _session.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
8890

8991
_logger.LogInformation("Scheme {scheme} added for {handlerType} with options: {options}", definition.Scheme, definition.HandlerType, data.SerializedOptions);
@@ -102,7 +104,7 @@ public virtual async Task RemoveAsync(TSchemeDefinition definition, Cancellation
102104

103105
cancellationToken.ThrowIfCancellationRequested();
104106

105-
var data = await _session.LoadAsync<SerializedData>(definition.Scheme, cancellationToken).ConfigureAwait(false);
107+
var data = await _session.LoadAsync<TSchemeDefinition>($"{_entitybasePath}{definition.Scheme}", cancellationToken).ConfigureAwait(false);
106108
_session.Delete(data);
107109
await _session.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
108110

@@ -124,7 +126,7 @@ public virtual async Task UpdateAsync(TSchemeDefinition definition, Cancellation
124126

125127
var serialized = Serialize(definition);
126128

127-
var data = await _session.LoadAsync<SerializedData>(definition.Scheme, cancellationToken).ConfigureAwait(false);
129+
var data = await _session.LoadAsync<TSchemeDefinition>($"{_entitybasePath}{definition.Scheme}", cancellationToken).ConfigureAwait(false);
128130

129131
data.SerializedOptions = serialized.SerializedOptions;
130132
data.SerializedHandlerType = serialized.SerializedHandlerType;
@@ -149,7 +151,7 @@ public virtual async Task<TSchemeDefinition> FindBySchemeAsync(string scheme, Ca
149151
CheckScheme(scheme);
150152

151153
cancellationToken.ThrowIfCancellationRequested();
152-
var data = await _session.LoadAsync<SerializedData>(scheme, cancellationToken).ConfigureAwait(false);
154+
var data = await _session.LoadAsync<TSchemeDefinition>($"{_entitybasePath}{scheme}", cancellationToken).ConfigureAwait(false);
153155

154156
if (data != null)
155157
{
@@ -167,35 +169,22 @@ private static void CheckScheme(string scheme)
167169
}
168170
}
169171

170-
private SerializedData Serialize(TSchemeDefinition definition)
172+
private TSchemeDefinition Serialize(TSchemeDefinition definition)
171173
{
172-
return new SerializedData
173-
{
174-
Id = definition.Scheme,
175-
SerializedHandlerType = _authenticationSchemeOptionsSerializer.SerializeType(definition.HandlerType),
176-
SerializedOptions = _authenticationSchemeOptionsSerializer.SerializeOptions(definition.Options, definition.HandlerType.GetAuthenticationSchemeOptionsType())
177-
};
174+
var data = (TSchemeDefinition)definition.Clone();
175+
data.SerializedHandlerType = _authenticationSchemeOptionsSerializer.SerializeType(definition.HandlerType);
176+
data.SerializedOptions = _authenticationSchemeOptionsSerializer.SerializeOptions(definition.Options, definition.HandlerType.GetAuthenticationSchemeOptionsType());
177+
data.HandlerType = null;
178+
data.Options = null;
179+
return data;
178180
}
179181

180-
private TSchemeDefinition Deserialize(SerializedData data)
182+
private TSchemeDefinition Deserialize(TSchemeDefinition data)
181183
{
182184
var handlerType = _authenticationSchemeOptionsSerializer.DeserializeType(data.SerializedHandlerType);
183-
return new TSchemeDefinition
184-
{
185-
Scheme = data.Id,
186-
HandlerType = handlerType,
187-
Options = _authenticationSchemeOptionsSerializer.DeserializeOptions(data.SerializedOptions, handlerType.GetAuthenticationSchemeOptionsType())
188-
};
189-
}
190-
191-
class SerializedData
192-
{
193-
public string Id { get; set; }
194-
195-
public string SerializedOptions { get; set; }
196-
197-
public string SerializedHandlerType { get; set; }
198-
199-
}
185+
data.HandlerType = handlerType;
186+
data.Options = _authenticationSchemeOptionsSerializer.DeserializeOptions(data.SerializedOptions, handlerType.GetAuthenticationSchemeOptionsType());
187+
return data;
188+
}
200189
}
201190
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,40 @@
11
// Project: aguacongas/DymamicAuthProviders
22
// Copyright (c) 2021 @Olivier Lefebvre
3+
using System;
4+
35
namespace Aguacongas.AspNetCore.Authentication.RavenDb
46
{
57
/// <summary>
68
/// Scheme definition for entity framework store
79
/// </summary>
810
/// <seealso cref="SchemeDefinitionBase" />
9-
public class SchemeDefinition: SchemeDefinitionBase
11+
public class SchemeDefinition: SchemeDefinitionBase, ICloneable
1012
{
13+
/// <summary>
14+
/// Gets or sets the serialized handler type.
15+
/// </summary>
16+
/// <value>
17+
/// The name of the serialized handler type.
18+
/// </value>
19+
public string SerializedHandlerType { get; set; }
20+
21+
/// <summary>
22+
/// Gets or sets the serialized options.
23+
/// </summary>
24+
/// <value>
25+
/// The serialized options.
26+
/// </value>
27+
public string SerializedOptions { get; set; }
28+
29+
/// <summary>
30+
/// Creates a new object that is a copy of the current instance.
31+
/// </summary>
32+
/// <returns>
33+
/// A new object that is a copy of this instance.
34+
/// </returns>
35+
public object Clone()
36+
{
37+
return MemberwiseClone();
38+
}
1139
}
1240
}

0 commit comments

Comments
 (0)