Browse Source

Introduce IKeyedObject and unify object key access

Added the IKeyedObject interface to provide a standard way to retrieve object keys, including support for composite keys via KeyedObjectHelper. Updated IEntity, IEntityDto, and related DTOs to implement IKeyedObject, and refactored resource permission logic to use GetObjectKey for key retrieval. This change improves consistency and reliability in handling object keys across the framework.
pull/24374/head
Halil İbrahim Kalkan 9 months ago
parent
commit
f5404df925
  1. 6
      framework/src/Volo.Abp.Authorization.Abstractions/Volo/Abp/Authorization/Permissions/Resources/IHasResourcePermissions.cs
  2. 12
      framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/Resources/ResourcePermissionPopulator.cs
  3. 6
      framework/src/Volo.Abp.Core/Volo/Abp/IKeyedObject.cs
  4. 39
      framework/src/Volo.Abp.Core/Volo/Abp/KeyedObjectHelper.cs
  5. 5
      framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/EntityDto.cs
  6. 5
      framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/ExtensibleEntityDto.cs
  7. 2
      framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/IEntityDto.cs
  8. 13
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Entity.cs
  9. 2
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/IEntity.cs

6
framework/src/Volo.Abp.Authorization.Abstractions/Volo/Abp/Authorization/Permissions/Resources/IHasResourcePermissions.cs

@ -2,9 +2,7 @@ using System.Collections.Generic;
namespace Volo.Abp.Authorization.Permissions.Resources;
public interface IHasResourcePermissions
public interface IHasResourcePermissions : IKeyedObject
{
public Dictionary<string, bool> ResourcePermissions { get; }
string GetResourceKey();
Dictionary<string, bool> ResourcePermissions { get; }
}

12
framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/Resources/ResourcePermissionPopulator.cs

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@ -42,13 +43,20 @@ public class ResourcePermissionPopulator : ITransientDependency
foreach (var resource in resources)
{
var results = await ResourcePermissionChecker.IsGrantedAsync(resopurcePermissionNames, resourceName, resource.GetResourceKey());
var resourceKey = resource.GetObjectKey();
if (resourceKey.IsNullOrEmpty())
{
throw new AbpException("Resource key can not be null or empty.");
}
var results = await ResourcePermissionChecker.IsGrantedAsync(resopurcePermissionNames, resourceName, resourceKey);
foreach (var resopurcePermission in resopurcePermissionNames)
{
if(resource.ResourcePermissions == null)
if (resource.ResourcePermissions == null)
{
ObjectHelper.TrySetProperty(resource, x => x.ResourcePermissions, () => new Dictionary<string, bool>());
}
var hasPermission = results.Result.TryGetValue(resopurcePermission, out var granted) && granted == PermissionGrantResult.Granted;
resource.ResourcePermissions![resopurcePermission] = hasPermission;
}

6
framework/src/Volo.Abp.Core/Volo/Abp/IKeyedObject.cs

@ -0,0 +1,6 @@
namespace Volo.Abp;
public interface IKeyedObject
{
string? GetObjectKey();
}

39
framework/src/Volo.Abp.Core/Volo/Abp/KeyedObjectHelper.cs

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Volo.Abp;
public static class KeyedObjectHelper
{
public static string EncodeCompositeKey(params object?[] keys)
{
var raw = keys.JoinAsString("||");
var bytes = Encoding.UTF8.GetBytes(raw);
var base64 = Convert.ToBase64String(bytes);
var base64Url = base64
.Replace("+", "-")
.Replace("/", "_")
.TrimEnd('=');
return base64Url;
}
public static string DecodeCompositeKey(string encoded)
{
var base64 = encoded
.Replace("-", "+")
.Replace("_", "/");
switch (encoded.Length % 4)
{
case 2: base64 += "=="; break;
case 3: base64 += "="; break;
}
var bytes = Convert.FromBase64String(base64);
var raw = Encoding.UTF8.GetString(bytes);
return raw;
}
}

5
framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/EntityDto.cs

@ -23,4 +23,9 @@ public abstract class EntityDto<TKey> : EntityDto, IEntityDto<TKey>
{
return $"[DTO: {GetType().Name}] Id = {Id}";
}
public virtual string? GetObjectKey()
{
return Id?.ToString();
}
}

5
framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/ExtensibleEntityDto.cs

@ -27,6 +27,11 @@ public abstract class ExtensibleEntityDto<TKey> : ExtensibleObject, IEntityDto<T
{
return $"[DTO: {GetType().Name}] Id = {Id}";
}
public virtual string? GetObjectKey()
{
return Id?.ToString();
}
}
[Serializable]

2
framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/IEntityDto.cs

@ -5,7 +5,7 @@ public interface IEntityDto
}
public interface IEntityDto<TKey> : IEntityDto
public interface IEntityDto<TKey> : IEntityDto, IKeyedObject
{
TKey Id { get; set; }
}

13
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Entity.cs

@ -18,6 +18,17 @@ public abstract class Entity : IEntity
return $"[ENTITY: {GetType().Name}] Keys = {GetKeys().JoinAsString(", ")}";
}
public virtual string? GetObjectKey()
{
var keys = GetKeys();
return keys.Length switch
{
0 => null,
1 when keys[0] != null => keys[0]?.ToString(),
_ => KeyedObjectHelper.EncodeCompositeKey(keys)
};
}
public abstract object?[] GetKeys();
public bool EntityEquals(IEntity other)
@ -45,7 +56,7 @@ public abstract class Entity<TKey> : Entity, IEntity<TKey>
public override object?[] GetKeys()
{
return new object?[] { Id };
return [Id];
}
/// <inheritdoc/>

2
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/IEntity.cs

@ -4,7 +4,7 @@
/// Defines an entity. It's primary key may not be "Id" or it may have a composite primary key.
/// Use <see cref="IEntity{TKey}"/> where possible for better integration to repositories and other structures in the framework.
/// </summary>
public interface IEntity
public interface IEntity : IKeyedObject
{
/// <summary>
/// Returns an array of ordered keys for this entity.

Loading…
Cancel
Save