Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions ServiceStack.OrmLite/src/ServiceStack.OrmLite/FieldDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ public string GetQuotedValue(object fromInstance, IOrmLiteDialectProvider dialec

public bool IsReference { get; set; }

/// <summary>
/// Whether the PK for the Reference Table is a field on the same table
/// </summary>
public string ReferenceSelfId { get; set; }

/// <summary>
/// The PK to use for the Reference Table (e.g. what ReferenceSelfId references)
/// </summary>
public string ReferenceRefId { get; set; }

/// <summary>
/// References a Field on another Table
/// [ReferenceField(typeof(Target), nameof(TargetId))]
/// public TargetFieldType TargetFieldName { get; set; }
/// </summary>
public FieldReference FieldReference { get; set; }

public string CustomFieldDefinition { get; set; }
Expand Down Expand Up @@ -196,6 +211,8 @@ public FieldDefinition Clone(Action<FieldDefinition> modifier = null)
CustomSelect = CustomSelect,
BelongToModelName = BelongToModelName,
IsReference = IsReference,
ReferenceRefId = ReferenceRefId,
ReferenceSelfId = ReferenceSelfId,
FieldReference = FieldReference,
CustomFieldDefinition = CustomFieldDefinition,
IsRefType = IsRefType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ string ToAddForeignKeyStatement<T, TForeign>(Expression<Func<T, object>> field,
string ToInsertStatement<T>(IDbCommand dbCmd, T item, ICollection<string> insertFields = null);
string MergeParamsIntoSql(string sql, IEnumerable<IDbDataParameter> dbParams);

string GetRefSelfSql<From>(SqlExpression<From> refQ, ModelDefinition modelDef, FieldDefinition refSelf, ModelDefinition refModelDef);
string GetRefSelfSql<From>(SqlExpression<From> refQ, ModelDefinition modelDef, FieldDefinition refSelf, ModelDefinition refModelDef, FieldDefinition refId);
string GetRefFieldSql(string subSql, ModelDefinition refModelDef, FieldDefinition refField);
string GetFieldReferenceSql(string subSql, FieldDefinition fieldDef, FieldReference fieldRef);

Expand Down
16 changes: 4 additions & 12 deletions ServiceStack.OrmLite/src/ServiceStack.OrmLite/ModelDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ namespace ServiceStack.OrmLite;

public class ModelDefinition
{
public ModelDefinition()
{
this.FieldDefinitions = new List<FieldDefinition>();
this.IgnoredFieldDefinitions = new List<FieldDefinition>();
this.CompositeIndexes = new List<CompositeIndexAttribute>();
this.UniqueConstraints = new List<UniqueConstraintAttribute>();
}

public const string RowVersionName = "RowVersion";

public string Name { get; set; }
Expand Down Expand Up @@ -69,13 +61,13 @@ public object GetPrimaryKey(object instance)
: instance.GetId();
}

public List<FieldDefinition> FieldDefinitions { get; set; }
public List<FieldDefinition> FieldDefinitions { get; set; } = [];

public FieldDefinition[] FieldDefinitionsArray { get; private set; }

public FieldDefinition[] FieldDefinitionsWithAliases { get; private set; }

public List<FieldDefinition> IgnoredFieldDefinitions { get; set; }
public List<FieldDefinition> IgnoredFieldDefinitions { get; set; } = [];

public FieldDefinition[] IgnoredFieldDefinitionsArray { get; private set; }

Expand Down Expand Up @@ -138,9 +130,9 @@ public Dictionary<string, FieldDefinition> GetFieldDefinitionMap(Func<string, st
}
}

public List<CompositeIndexAttribute> CompositeIndexes { get; set; }
public List<CompositeIndexAttribute> CompositeIndexes { get; set; } = [];

public List<UniqueConstraintAttribute> UniqueConstraints { get; set; }
public List<UniqueConstraintAttribute> UniqueConstraints { get; set; } = [];

public FieldDefinition GetFieldDefinition<T>(Expression<Func<T, object>> field)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ string JoinSql(List<string> statements)
? referencesAttr != null ? new ForeignKeyConstraint(referencesAttr.Type) : null
: new ForeignKeyConstraint(fkAttr.Type, fkAttr.OnDelete, fkAttr.OnUpdate, fkAttr.ForeignKeyName),
IsReference = isReference,
ReferenceSelfId = referenceAttr?.SelfId,
ReferenceRefId = referenceAttr?.RefId,
GetValueFn = propertyInfo.CreateGetter(),
SetValueFn = propertyInfo.CreateSetter(),
Sequence = sequenceAttr?.Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ public virtual string MergeParamsIntoSql(string sql, IEnumerable<IDbDataParamete
}

//Load Self Table.RefTableId PK
public virtual string GetRefSelfSql<From>(SqlExpression<From> refQ, ModelDefinition modelDef, FieldDefinition refSelf, ModelDefinition refModelDef)
public virtual string GetRefSelfSql<From>(SqlExpression<From> refQ, ModelDefinition modelDef, FieldDefinition refSelf, ModelDefinition refModelDef, FieldDefinition refId)
{
refQ.Select(this.GetQuotedColumnName(modelDef, refSelf));
refQ.OrderBy().ClearLimits(); //clear any ORDER BY or LIMIT's in Sub Select's
Expand All @@ -967,7 +967,7 @@ public virtual string GetRefSelfSql<From>(SqlExpression<From> refQ, ModelDefinit

var sqlRef = $"SELECT {GetColumnNames(refModelDef)} " +
$"FROM {GetQuotedTableName(refModelDef)} " +
$"WHERE {this.GetQuotedColumnName(refModelDef.PrimaryKey)} " +
$"WHERE {this.GetQuotedColumnName(refId)} " +
$"IN ({subSqlRef})";

if (OrmLiteConfig.LoadReferenceSelectFilter != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1029,9 +1029,15 @@ public static FieldDefinition GetRefFieldDefIfExists(this ModelDefinition modelD

public static FieldDefinition GetSelfRefFieldDefIfExists(this ModelDefinition modelDef, ModelDefinition refModelDef, FieldDefinition fieldDef)
{
var refField = (fieldDef == null ? null
: modelDef.FieldDefinitions.FirstOrDefault(x => x.ForeignKey != null && x.ForeignKey.ReferenceType == refModelDef.ModelType
&& fieldDef.IsSelfRefField(x)))
if (fieldDef == null)
return null;

if (fieldDef.ReferenceSelfId != null)
{
return modelDef.FieldDefinitions.FirstOrDefault(x => x.Name == fieldDef.ReferenceSelfId);
}

var refField = modelDef.FieldDefinitions.FirstOrDefault(x => x.ForeignKey != null && x.ForeignKey.ReferenceType == refModelDef.ModelType && fieldDef.IsSelfRefField(x))
?? modelDef.FieldDefinitions.FirstOrDefault(x => x.ForeignKey != null && x.ForeignKey.ReferenceType == refModelDef.ModelType)
?? modelDef.FieldDefinitions.FirstOrDefault(refModelDef.IsRefField);

Expand Down
24 changes: 16 additions & 8 deletions ServiceStack.OrmLite/src/ServiceStack.OrmLite/Support/LoadList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ protected void SetListChildResults(FieldDefinition fieldDef, Type refType, IList
}
}

protected string GetRefSelfSql(ModelDefinition modelDef, FieldDefinition refSelf, ModelDefinition refModelDef) =>
dialectProvider.GetRefSelfSql(q.Clone(), modelDef, refSelf, refModelDef);
protected string GetRefSelfSql(ModelDefinition modelDef, FieldDefinition refSelf, ModelDefinition refModelDef, FieldDefinition refId) =>
dialectProvider.GetRefSelfSql(q.Clone(), modelDef, refSelf, refModelDef, refId);

protected string GetRefFieldSql(ModelDefinition refModelDef, FieldDefinition refField) =>
dialectProvider.GetRefFieldSql(subSql, refModelDef, refField);
Expand Down Expand Up @@ -125,13 +125,13 @@ public int GetHashCode(object obj)
}
}

protected void SetRefSelfChildResults(FieldDefinition fieldDef, ModelDefinition refModelDef, FieldDefinition refSelf, IList childResults)
protected void SetRefSelfChildResults(FieldDefinition fieldDef, ModelDefinition refModelDef, FieldDefinition refSelf, IList childResults, FieldDefinition refId)
{
var map = CreateRefMap();

foreach (var result in childResults)
{
var pkValue = refModelDef.PrimaryKey.GetValue(result);
var pkValue = refId.GetValue(result);
map[pkValue] = result;
}

Expand Down Expand Up @@ -215,9 +215,13 @@ public void SetRefField(FieldDefinition fieldDef, Type refType)

if (refSelf != null)
{
var sqlRef = GetRefSelfSql(modelDef, refSelf, refModelDef);
var refId = fieldDef.ReferenceRefId != null
? refModelDef.GetFieldDefinition(fieldDef.ReferenceRefId)
?? throw new NotSupportedException($"{fieldDef.ReferenceRefId} is not a property of {refModelDef.Name}")
: refModelDef.PrimaryKey;
var sqlRef = GetRefSelfSql(modelDef, refSelf, refModelDef, refId);
var childResults = dbCmd.ConvertToList(refType, sqlRef);
SetRefSelfChildResults(fieldDef, refModelDef, refSelf, childResults);
SetRefSelfChildResults(fieldDef, refModelDef, refSelf, childResults, refId);
}
else if (refField != null)
{
Expand Down Expand Up @@ -261,9 +265,13 @@ public async Task SetRefFieldAsync(FieldDefinition fieldDef, Type refType, Cance

if (refSelf != null)
{
var sqlRef = GetRefSelfSql(modelDef, refSelf, refModelDef);
var refId = fieldDef.ReferenceRefId != null
? refModelDef.GetFieldDefinition(fieldDef.ReferenceRefId)
?? throw new NotSupportedException($"{fieldDef.ReferenceRefId} is not a property of {refModelDef.Name}")
: refModelDef.PrimaryKey;
var sqlRef = GetRefSelfSql(modelDef, refSelf, refModelDef, refId);
var childResults = await dbCmd.ConvertToListAsync(refType, sqlRef, token).ConfigAwait();
SetRefSelfChildResults(fieldDef, refModelDef, refSelf, childResults);
SetRefSelfChildResults(fieldDef, refModelDef, refSelf, childResults, refId);
}
else if (refField != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public class IdentityApplicationAuthProvider<TUser,TKey> : IdentityAuthProvider<
[JwtClaimTypes.Picture] = nameof(AuthUserSession.ProfileUrl),
};

/// <summary>
/// Override claims where exists
/// </summary>
public Dictionary<string, string> PriorityMapClaimsToSession { get; set; } = new()
{
[JwtClaimTypes.PreferredUserName] = nameof(AuthUserSession.UserAuthName),
};

/// <summary>
/// Run custom filter after session is restored from ClaimsPrincipal
/// </summary>
Expand Down Expand Up @@ -180,6 +188,12 @@ public virtual void PopulateSession(IRequest req, IAuthSession session, ClaimsPr
meta[claim.Type] = claim.Value;
}
}
foreach (var entry in PriorityMapClaimsToSession)
{
var claim = claimsPrincipal.Claims.FirstOrDefault(x => x.Type == entry.Key);
if (claim != null)
sessionValues[entry.Value] = claim.Value;
}

session.PopulateFromMap(sessionValues);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;

namespace ServiceStack.DataAnnotations;
Expand All @@ -8,4 +9,6 @@ namespace ServiceStack.DataAnnotations;
[AttributeUsage(AttributeTargets.Property)]
public class ReferenceAttribute : AttributeBase
{
public string? SelfId { get; set; }
public string? RefId { get; set; }
}
2 changes: 2 additions & 0 deletions ServiceStack/tests/NorthwindAuto/Configure.Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public void Configure(IWebHostBuilder builder) => builder
options.CredentialsAuth();
options.JwtAuth();
options.BasicAuth();
// options.ApplicationAuth(feature =>
// feature.PriorityMapClaimsToSession.Clear());

options.AdminUsersFeature(feature =>
{
Expand Down
19 changes: 12 additions & 7 deletions ServiceStack/tests/NorthwindAuto/ServiceModel/AppUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
using System;
using System.Collections.Generic;

namespace TalentBlazor.ServiceModel;
namespace MyApp.ServiceModel;

public enum Department
/// <summary>
/// Public User DTO
/// </summary>
[Alias("AspNetUsers")]
public class User
{
None,
Marketing,
Accounts,
Legal,
HumanResources,
public string? Id { get; set; }
public string? UserName { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? DisplayName { get; set; }
public string? ProfileUrl { get; set; }
}
5 changes: 4 additions & 1 deletion ServiceStack/tests/NorthwindAuto/ServiceModel/Bookings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ public class Booking : AuditBase
public long? PostalAddressId { get; set; }

[Reference]
public Address? PostalAddress { get; set; }
public Address? PostalAddress { get; set; }

[Reference(SelfId = nameof(CreatedBy), RefId = nameof(User.UserName))]
public User? Employee { get; set; }
}

public enum RoomType
Expand Down