Skip to content

Commit

Permalink
- remove redundant brackets in: Resource, Role and User
Browse files Browse the repository at this point in the history
- add underscore prefix for all `private readonly` fields
- renamed all `protected readonly` fields
- remove `[Serializable]` attribute and `ctor` for serializing in `ResourceNotFoundException`, cause it's obsolete in net8.0. See: SYSLIB0051
- remove exception tests
  • Loading branch information
ArdenHide committed Nov 20, 2024
1 parent 575da01 commit 6fcb283
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 156 deletions.
8 changes: 4 additions & 4 deletions src/Acl.Net.Core.Database/AclDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ public abstract class AclDbContext<TKey, TUser, TRole, TResource> : DbContext
where TRole : Role<TKey>
where TResource : Resource<TKey>
{
private readonly IInitialDataSeeder<TKey, TRole> seeder;
private readonly IInitialDataSeeder<TKey, TRole> _seeder;

/// <summary>
/// Initializes a new instance of the <see cref="AclDbContext{TKey, TUser, TRole, TResource}"/> class with the provided data seeder.
/// </summary>
/// <param name="seeder">The initial data seeder.</param>
protected AclDbContext(IInitialDataSeeder<TKey, TRole> seeder)
{
this.seeder = seeder;
_seeder = seeder;
}

/// <summary>
Expand All @@ -80,7 +80,7 @@ protected AclDbContext(IInitialDataSeeder<TKey, TRole> seeder)
/// <param name="seeder">The initial data seeder.</param>
protected AclDbContext(DbContextOptions options, IInitialDataSeeder<TKey, TRole> seeder) : base(options)
{
this.seeder = seeder;
_seeder = seeder;
}

/// <summary>
Expand Down Expand Up @@ -122,7 +122,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

entity.HasMany<TResource>().WithOne().HasForeignKey(res => res.RoleId).IsRequired();

entity.HasData(seeder.SeedAdminRole(), seeder.SeedUserRole());
entity.HasData(_seeder.SeedAdminRole(), _seeder.SeedUserRole());
});

modelBuilder.Entity<TResource>(entity =>
Expand Down
2 changes: 1 addition & 1 deletion src/Acl.Net.Core.Database/Entities/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Represents a specific resource in the system.
/// </summary>
public class Resource : Resource<int> { }
public class Resource : Resource<int>;

/// <summary>
/// Represents a specific resource in the system, identified by a generic key type.
Expand Down
2 changes: 1 addition & 1 deletion src/Acl.Net.Core.Database/Entities/Role.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Represents a role within the Access Control List (ACL) system.
/// </summary>
public class Role : Role<int> { }
public class Role : Role<int>;

/// <summary>
/// Represents a role within the Access Control List (ACL) system, with a specific key type.
Expand Down
2 changes: 1 addition & 1 deletion src/Acl.Net.Core.Database/Entities/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Represents a user in the system.
/// </summary>
public class User : User<int> { }
public class User : User<int>;

/// <summary>
/// Represents a user in the system, identified by a generic key type.
Expand Down
50 changes: 25 additions & 25 deletions src/Acl.Net.Core.Managers/AclManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ public class AclManager<TKey, TUser, TRole, TResource> : IAclManager<TKey, TUser
where TRole : Role<TKey>
where TResource : Resource<TKey>
{
protected readonly IInitialDataSeeder<TKey, TRole> initialDataSeeder;
protected readonly IUserManager<TKey, TUser, TRole> userManager;
protected readonly IResourceManager<TKey, TUser, TResource> resourceManager;
protected readonly IInitialDataSeeder<TKey, TRole> InitialDataSeeder;
protected readonly IUserManager<TKey, TUser, TRole> UserManager;
protected readonly IResourceManager<TKey, TUser, TResource> ResourceManager;

/// <summary>
/// Initializes a new instance of the <see cref="AclManager{TKey,TUser,TRole,TResource}"/> class
Expand All @@ -134,9 +134,9 @@ public AclManager(
AclDbContext<TKey, TUser, TRole, TResource> context
)
{
this.initialDataSeeder = initialDataSeeder;
userManager = new UserManager<TKey, TUser, TRole, TResource>(context);
resourceManager = new ResourceManager<TKey, TUser, TRole, TResource>(context, initialDataSeeder);
InitialDataSeeder = initialDataSeeder;
UserManager = new UserManager<TKey, TUser, TRole, TResource>(context);
ResourceManager = new ResourceManager<TKey, TUser, TRole, TResource>(context, initialDataSeeder);
}

/// <summary>
Expand All @@ -152,9 +152,9 @@ public AclManager(
IResourceManager<TKey, TUser, TResource> resourceManager
)
{
this.initialDataSeeder = initialDataSeeder;
this.userManager = userManager;
this.resourceManager = resourceManager;
InitialDataSeeder = initialDataSeeder;
UserManager = userManager;
ResourceManager = resourceManager;
}

/// <summary>
Expand All @@ -168,8 +168,8 @@ IResourceManager<TKey, TUser, TResource> resourceManager
/// </exception>
public virtual bool IsPermitted(string userName, string resourceName)
{
var user = userManager.UserProcessing(userName, initialDataSeeder.SeedUserRole());
return resourceManager.IsPermitted(user, resourceName);
var user = UserManager.UserProcessing(userName, InitialDataSeeder.SeedUserRole());
return ResourceManager.IsPermitted(user, resourceName);
}

/// <summary>
Expand All @@ -183,7 +183,7 @@ public virtual bool IsPermitted(string userName, string resourceName)
/// </exception>
public virtual bool IsPermitted(TUser user, string resourceName)
{
return resourceManager.IsPermitted(user, resourceName);
return ResourceManager.IsPermitted(user, resourceName);
}

/// <summary>
Expand All @@ -194,8 +194,8 @@ public virtual bool IsPermitted(TUser user, string resourceName)
/// <returns><see langword="true"/> if the user is permitted to access the resource; otherwise, <see langword="false"/>.</returns>
public virtual bool IsPermitted(string userName, TResource resource)
{
var user = userManager.UserProcessing(userName, initialDataSeeder.SeedUserRole());
return resourceManager.IsPermitted(user, resource);
var user = UserManager.UserProcessing(userName, InitialDataSeeder.SeedUserRole());
return ResourceManager.IsPermitted(user, resource);
}

/// <summary>
Expand All @@ -206,7 +206,7 @@ public virtual bool IsPermitted(string userName, TResource resource)
/// <returns><see langword="true"/> if the user is permitted to access the resource; otherwise, <see langword="false"/>.</returns>
public virtual bool IsPermitted(TUser user, TResource resource)
{
return resourceManager.IsPermitted(user, resource);
return ResourceManager.IsPermitted(user, resource);
}

/// <summary>
Expand All @@ -221,8 +221,8 @@ public virtual bool IsPermitted(TUser user, TResource resource)
/// </exception>
public virtual IEnumerable<TResource> IsPermitted(string userName, IEnumerable<string> resourceNames)
{
var user = userManager.UserProcessing(userName, initialDataSeeder.SeedUserRole());
return resourceManager.IsPermitted(user, resourceNames);
var user = UserManager.UserProcessing(userName, InitialDataSeeder.SeedUserRole());
return ResourceManager.IsPermitted(user, resourceNames);
}

/// <summary>
Expand All @@ -237,8 +237,8 @@ public virtual IEnumerable<TResource> IsPermitted(string userName, IEnumerable<s
/// </exception>
public virtual async Task<bool> IsPermittedAsync(string userName, string resourceName)
{
var user = await userManager.UserProcessingAsync(userName, initialDataSeeder.SeedUserRole());
return await resourceManager.IsPermittedAsync(user, resourceName);
var user = await UserManager.UserProcessingAsync(userName, InitialDataSeeder.SeedUserRole());
return await ResourceManager.IsPermittedAsync(user, resourceName);
}

/// <summary>
Expand All @@ -253,7 +253,7 @@ public virtual async Task<bool> IsPermittedAsync(string userName, string resourc
/// </exception>
public virtual async Task<bool> IsPermittedAsync(TUser user, string resourceName)
{
return await resourceManager.IsPermittedAsync(user, resourceName);
return await ResourceManager.IsPermittedAsync(user, resourceName);
}

/// <summary>
Expand All @@ -265,8 +265,8 @@ public virtual async Task<bool> IsPermittedAsync(TUser user, string resourceName
/// The task result contains <see langword="true"/> if the user is permitted to access the resource; otherwise, <see langword="false"/>.</returns>
public virtual async Task<bool> IsPermittedAsync(string userName, TResource resource)
{
var user = await userManager.UserProcessingAsync(userName, initialDataSeeder.SeedUserRole());
return await resourceManager.IsPermittedAsync(user, resource);
var user = await UserManager.UserProcessingAsync(userName, InitialDataSeeder.SeedUserRole());
return await ResourceManager.IsPermittedAsync(user, resource);
}

/// <summary>
Expand All @@ -278,7 +278,7 @@ public virtual async Task<bool> IsPermittedAsync(string userName, TResource reso
/// The task result contains <see langword="true"/> if the user is permitted to access the resource; otherwise, <see langword="false"/>.</returns>
public virtual async Task<bool> IsPermittedAsync(TUser user, TResource resource)
{
return await resourceManager.IsPermittedAsync(user, resource);
return await ResourceManager.IsPermittedAsync(user, resource);
}

/// <summary>
Expand All @@ -294,7 +294,7 @@ public virtual async Task<bool> IsPermittedAsync(TUser user, TResource resource)
/// </exception>
public virtual async Task<IEnumerable<TResource>> IsPermittedAsync(string userName, IEnumerable<string> resourceNames)
{
var user = await userManager.UserProcessingAsync(userName, initialDataSeeder.SeedUserRole());
return await resourceManager.IsPermittedAsync(user, resourceNames);
var user = await UserManager.UserProcessingAsync(userName, InitialDataSeeder.SeedUserRole());
return await ResourceManager.IsPermittedAsync(user, resourceNames);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System.Runtime.Serialization;

namespace Acl.Net.Core.Managers.Exceptions;
namespace Acl.Net.Core.Managers.Exceptions;

/// <summary>
/// Represents an exception that is thrown when a specific resource is not found.
/// </summary>
[Serializable]
public class ResourceNotFoundException : Exception
{
/// <summary>
Expand All @@ -15,13 +12,4 @@ public class ResourceNotFoundException : Exception
public ResourceNotFoundException(string resourceName)
: base($"Resource with name '{resourceName}' not found.")
{ }

/// <summary>
/// Initializes a new instance of the <see cref="ResourceNotFoundException"/> class with serialized data.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
protected ResourceNotFoundException(SerializationInfo info, StreamingContext context)
: base(info, context)
{ }
}
8 changes: 2 additions & 6 deletions src/Acl.Net.Core.Managers/IAclManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ namespace Acl.Net.Core.Managers;
/// <summary>
/// Defines the basic contract for Access Control List (ACL) management.
/// </summary>
public interface IAclManager : IAclManager<int>
{
}
public interface IAclManager : IAclManager<int>;

/// <summary>
/// Defines the contract for Access Control List (ACL) management with support for a specific key type.
/// </summary>
/// <typeparam name="TKey">The type of key used to identify users and resources.</typeparam>
public interface IAclManager<TKey> : IAclManager<TKey, User<TKey>, Resource<TKey>>
where TKey : IEquatable<TKey>
{
}
where TKey : IEquatable<TKey>;

/// <summary>
/// Defines the contract for Access Control List (ACL) management with support for specific key, user, and resource types.
Expand Down
8 changes: 2 additions & 6 deletions src/Acl.Net.Core.Managers/IResourceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ namespace Acl.Net.Core.Managers;
/// <summary>
/// Manages resource-related operations with a specific integer key type.
/// </summary>
public interface IResourceManager : IResourceManager<int>
{
}
public interface IResourceManager : IResourceManager<int>;

/// <summary>
/// Manages resource-related operations with a specific key type.
/// </summary>
/// <typeparam name="TKey">The type of the key.</typeparam>
public interface IResourceManager<TKey> : IResourceManager<TKey, User<TKey>, Resource<TKey>>
where TKey : IEquatable<TKey>
{
}
where TKey : IEquatable<TKey>;

/// <summary>
/// Manages resource-related operations with specific user and resource types.
Expand Down
8 changes: 2 additions & 6 deletions src/Acl.Net.Core.Managers/IUserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ namespace Acl.Net.Core.Managers;
/// <summary>
/// Defines the contract for managing user-related operations using integer keys.
/// </summary>
public interface IUserManager : IUserManager<int>
{
}
public interface IUserManager : IUserManager<int>;

/// <summary>
/// Defines the contract for managing user-related operations with a specific key type.
/// </summary>
/// <typeparam name="TKey">The type of the key.</typeparam>
public interface IUserManager<TKey> : IUserManager<TKey, User<TKey>, Role<TKey>>
where TKey : IEquatable<TKey>
{
}
where TKey : IEquatable<TKey>;

/// <summary>
/// Defines the contract for managing user-related operations with specific user and role types.
Expand Down
20 changes: 10 additions & 10 deletions src/Acl.Net.Core.Managers/ResourceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public class ResourceManager<TKey, TUser, TRole, TResource> : IResourceManager<T
where TRole : Role<TKey>
where TResource : Resource<TKey>
{
protected readonly AclDbContext<TKey, TUser, TRole, TResource> context;
protected readonly IInitialDataSeeder<TKey, TRole> initialDataSeeder;
protected readonly AclDbContext<TKey, TUser, TRole, TResource> Context;
protected readonly IInitialDataSeeder<TKey, TRole> InitialDataSeeder;

/// <summary>
/// Initializes a new instance of the <see cref="ResourceManager{TKey, TUser, TRole, TResource}"/> class.
Expand All @@ -65,8 +65,8 @@ public ResourceManager(
IInitialDataSeeder<TKey, TRole> initialDataSeeder
)
{
this.context = context;
this.initialDataSeeder = initialDataSeeder;
Context = context;
InitialDataSeeder = initialDataSeeder;
}

/// <summary>
Expand All @@ -92,8 +92,8 @@ public virtual bool IsPermitted(TUser user, string resourceName)
/// <returns><see langword="true"/> if the user is permitted to access the resource; otherwise, <see langword="false"/>.</returns>
public virtual bool IsPermitted(TUser user, TResource resource)
{
return user.RoleId.Equals(initialDataSeeder.SeedAdminRole().Id) ||
context.Resources.Any(r => r.RoleId.Equals(user.RoleId) && r.Id.Equals(resource.Id));
return user.RoleId.Equals(InitialDataSeeder.SeedAdminRole().Id) ||
Context.Resources.Any(r => r.RoleId.Equals(user.RoleId) && r.Id.Equals(resource.Id));
}

/// <summary>
Expand Down Expand Up @@ -146,8 +146,8 @@ public virtual async Task<bool> IsPermittedAsync(TUser user, string resourceName
/// The task result contains <see langword="true"/> if the user is permitted to access the resource; otherwise, <see langword="false"/>.</returns>
public virtual async Task<bool> IsPermittedAsync(TUser user, TResource resource)
{
return user.RoleId.Equals(initialDataSeeder.SeedAdminRole().Id) ||
await context.Resources.AnyAsync(r => r.RoleId.Equals(user.RoleId) && r.Id.Equals(resource.Id));
return user.RoleId.Equals(InitialDataSeeder.SeedAdminRole().Id) ||
await Context.Resources.AnyAsync(r => r.RoleId.Equals(user.RoleId) && r.Id.Equals(resource.Id));
}

/// <summary>
Expand Down Expand Up @@ -204,7 +204,7 @@ public virtual async Task<IEnumerable<TResource>> IsPermittedAsync(TUser user, I
/// </exception>
public virtual TResource GetResourceByName(string resourceName)
{
return context.Resources.FirstOrDefault(r => r.Name == resourceName)
return Context.Resources.FirstOrDefault(r => r.Name == resourceName)
?? throw new ResourceNotFoundException(resourceName);
}

Expand All @@ -219,7 +219,7 @@ public virtual TResource GetResourceByName(string resourceName)
/// </exception>
public virtual async Task<TResource> GetResourceByNameAsync(string resourceName)
{
return await context.Resources.FirstOrDefaultAsync(r => r.Name == resourceName)
return await Context.Resources.FirstOrDefaultAsync(r => r.Name == resourceName)
?? throw new ResourceNotFoundException(resourceName);
}
}
Loading

0 comments on commit 6fcb283

Please sign in to comment.