diff --git a/src/Acl.Net.Core.Database/AclDbContext.cs b/src/Acl.Net.Core.Database/AclDbContext.cs index 6157ad2..47e97a8 100644 --- a/src/Acl.Net.Core.Database/AclDbContext.cs +++ b/src/Acl.Net.Core.Database/AclDbContext.cs @@ -62,7 +62,7 @@ public abstract class AclDbContext : DbContext where TRole : Role where TResource : Resource { - private readonly IInitialDataSeeder seeder; + private readonly IInitialDataSeeder _seeder; /// /// Initializes a new instance of the class with the provided data seeder. @@ -70,7 +70,7 @@ public abstract class AclDbContext : DbContext /// The initial data seeder. protected AclDbContext(IInitialDataSeeder seeder) { - this.seeder = seeder; + _seeder = seeder; } /// @@ -80,7 +80,7 @@ protected AclDbContext(IInitialDataSeeder seeder) /// The initial data seeder. protected AclDbContext(DbContextOptions options, IInitialDataSeeder seeder) : base(options) { - this.seeder = seeder; + _seeder = seeder; } /// @@ -122,7 +122,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) entity.HasMany().WithOne().HasForeignKey(res => res.RoleId).IsRequired(); - entity.HasData(seeder.SeedAdminRole(), seeder.SeedUserRole()); + entity.HasData(_seeder.SeedAdminRole(), _seeder.SeedUserRole()); }); modelBuilder.Entity(entity => diff --git a/src/Acl.Net.Core.Database/Entities/Resource.cs b/src/Acl.Net.Core.Database/Entities/Resource.cs index 718527f..41556eb 100644 --- a/src/Acl.Net.Core.Database/Entities/Resource.cs +++ b/src/Acl.Net.Core.Database/Entities/Resource.cs @@ -3,7 +3,7 @@ /// /// Represents a specific resource in the system. /// -public class Resource : Resource { } +public class Resource : Resource; /// /// Represents a specific resource in the system, identified by a generic key type. diff --git a/src/Acl.Net.Core.Database/Entities/Role.cs b/src/Acl.Net.Core.Database/Entities/Role.cs index 831c0bc..ee31f5d 100644 --- a/src/Acl.Net.Core.Database/Entities/Role.cs +++ b/src/Acl.Net.Core.Database/Entities/Role.cs @@ -3,7 +3,7 @@ /// /// Represents a role within the Access Control List (ACL) system. /// -public class Role : Role { } +public class Role : Role; /// /// Represents a role within the Access Control List (ACL) system, with a specific key type. diff --git a/src/Acl.Net.Core.Database/Entities/User.cs b/src/Acl.Net.Core.Database/Entities/User.cs index 9532996..d16de60 100644 --- a/src/Acl.Net.Core.Database/Entities/User.cs +++ b/src/Acl.Net.Core.Database/Entities/User.cs @@ -3,7 +3,7 @@ /// /// Represents a user in the system. /// -public class User : User { } +public class User : User; /// /// Represents a user in the system, identified by a generic key type. diff --git a/src/Acl.Net.Core.Managers/AclManager.cs b/src/Acl.Net.Core.Managers/AclManager.cs index 7af5b12..731e102 100644 --- a/src/Acl.Net.Core.Managers/AclManager.cs +++ b/src/Acl.Net.Core.Managers/AclManager.cs @@ -118,9 +118,9 @@ public class AclManager : IAclManager where TResource : Resource { - protected readonly IInitialDataSeeder initialDataSeeder; - protected readonly IUserManager userManager; - protected readonly IResourceManager resourceManager; + protected readonly IInitialDataSeeder InitialDataSeeder; + protected readonly IUserManager UserManager; + protected readonly IResourceManager ResourceManager; /// /// Initializes a new instance of the class @@ -134,9 +134,9 @@ public AclManager( AclDbContext context ) { - this.initialDataSeeder = initialDataSeeder; - userManager = new UserManager(context); - resourceManager = new ResourceManager(context, initialDataSeeder); + InitialDataSeeder = initialDataSeeder; + UserManager = new UserManager(context); + ResourceManager = new ResourceManager(context, initialDataSeeder); } /// @@ -152,9 +152,9 @@ public AclManager( IResourceManager resourceManager ) { - this.initialDataSeeder = initialDataSeeder; - this.userManager = userManager; - this.resourceManager = resourceManager; + InitialDataSeeder = initialDataSeeder; + UserManager = userManager; + ResourceManager = resourceManager; } /// @@ -168,8 +168,8 @@ IResourceManager resourceManager /// 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); } /// @@ -183,7 +183,7 @@ public virtual bool IsPermitted(string userName, string resourceName) /// public virtual bool IsPermitted(TUser user, string resourceName) { - return resourceManager.IsPermitted(user, resourceName); + return ResourceManager.IsPermitted(user, resourceName); } /// @@ -194,8 +194,8 @@ public virtual bool IsPermitted(TUser user, string resourceName) /// if the user is permitted to access the resource; otherwise, . 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); } /// @@ -206,7 +206,7 @@ public virtual bool IsPermitted(string userName, TResource resource) /// if the user is permitted to access the resource; otherwise, . public virtual bool IsPermitted(TUser user, TResource resource) { - return resourceManager.IsPermitted(user, resource); + return ResourceManager.IsPermitted(user, resource); } /// @@ -221,8 +221,8 @@ public virtual bool IsPermitted(TUser user, TResource resource) /// public virtual IEnumerable IsPermitted(string userName, IEnumerable 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); } /// @@ -237,8 +237,8 @@ public virtual IEnumerable IsPermitted(string userName, IEnumerable public virtual async Task 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); } /// @@ -253,7 +253,7 @@ public virtual async Task IsPermittedAsync(string userName, string resourc /// public virtual async Task IsPermittedAsync(TUser user, string resourceName) { - return await resourceManager.IsPermittedAsync(user, resourceName); + return await ResourceManager.IsPermittedAsync(user, resourceName); } /// @@ -265,8 +265,8 @@ public virtual async Task IsPermittedAsync(TUser user, string resourceName /// The task result contains if the user is permitted to access the resource; otherwise, . public virtual async Task 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); } /// @@ -278,7 +278,7 @@ public virtual async Task IsPermittedAsync(string userName, TResource reso /// The task result contains if the user is permitted to access the resource; otherwise, . public virtual async Task IsPermittedAsync(TUser user, TResource resource) { - return await resourceManager.IsPermittedAsync(user, resource); + return await ResourceManager.IsPermittedAsync(user, resource); } /// @@ -294,7 +294,7 @@ public virtual async Task IsPermittedAsync(TUser user, TResource resource) /// public virtual async Task> IsPermittedAsync(string userName, IEnumerable 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); } } diff --git a/src/Acl.Net.Core.Managers/Exceptions/ResourceNotFoundException.cs b/src/Acl.Net.Core.Managers/Exceptions/ResourceNotFoundException.cs index 78659e7..d452f43 100644 --- a/src/Acl.Net.Core.Managers/Exceptions/ResourceNotFoundException.cs +++ b/src/Acl.Net.Core.Managers/Exceptions/ResourceNotFoundException.cs @@ -1,11 +1,8 @@ -using System.Runtime.Serialization; - -namespace Acl.Net.Core.Managers.Exceptions; +namespace Acl.Net.Core.Managers.Exceptions; /// /// Represents an exception that is thrown when a specific resource is not found. /// -[Serializable] public class ResourceNotFoundException : Exception { /// @@ -15,13 +12,4 @@ public class ResourceNotFoundException : Exception public ResourceNotFoundException(string resourceName) : base($"Resource with name '{resourceName}' not found.") { } - - /// - /// Initializes a new instance of the class with serialized data. - /// - /// The that holds the serialized object data about the exception being thrown. - /// The that contains contextual information about the source or destination. - protected ResourceNotFoundException(SerializationInfo info, StreamingContext context) - : base(info, context) - { } } \ No newline at end of file diff --git a/src/Acl.Net.Core.Managers/IAclManager.cs b/src/Acl.Net.Core.Managers/IAclManager.cs index 7681801..ccc9ce7 100644 --- a/src/Acl.Net.Core.Managers/IAclManager.cs +++ b/src/Acl.Net.Core.Managers/IAclManager.cs @@ -5,18 +5,14 @@ namespace Acl.Net.Core.Managers; /// /// Defines the basic contract for Access Control List (ACL) management. /// -public interface IAclManager : IAclManager -{ -} +public interface IAclManager : IAclManager; /// /// Defines the contract for Access Control List (ACL) management with support for a specific key type. /// /// The type of key used to identify users and resources. public interface IAclManager : IAclManager, Resource> - where TKey : IEquatable -{ -} + where TKey : IEquatable; /// /// Defines the contract for Access Control List (ACL) management with support for specific key, user, and resource types. diff --git a/src/Acl.Net.Core.Managers/IResourceManager.cs b/src/Acl.Net.Core.Managers/IResourceManager.cs index 2cc51f8..f4a9f09 100644 --- a/src/Acl.Net.Core.Managers/IResourceManager.cs +++ b/src/Acl.Net.Core.Managers/IResourceManager.cs @@ -5,18 +5,14 @@ namespace Acl.Net.Core.Managers; /// /// Manages resource-related operations with a specific integer key type. /// -public interface IResourceManager : IResourceManager -{ -} +public interface IResourceManager : IResourceManager; /// /// Manages resource-related operations with a specific key type. /// /// The type of the key. public interface IResourceManager : IResourceManager, Resource> - where TKey : IEquatable -{ -} + where TKey : IEquatable; /// /// Manages resource-related operations with specific user and resource types. diff --git a/src/Acl.Net.Core.Managers/IUserManager.cs b/src/Acl.Net.Core.Managers/IUserManager.cs index 8384d90..4ecda31 100644 --- a/src/Acl.Net.Core.Managers/IUserManager.cs +++ b/src/Acl.Net.Core.Managers/IUserManager.cs @@ -5,18 +5,14 @@ namespace Acl.Net.Core.Managers; /// /// Defines the contract for managing user-related operations using integer keys. /// -public interface IUserManager : IUserManager -{ -} +public interface IUserManager : IUserManager; /// /// Defines the contract for managing user-related operations with a specific key type. /// /// The type of the key. public interface IUserManager : IUserManager, Role> - where TKey : IEquatable -{ -} + where TKey : IEquatable; /// /// Defines the contract for managing user-related operations with specific user and role types. diff --git a/src/Acl.Net.Core.Managers/ResourceManager.cs b/src/Acl.Net.Core.Managers/ResourceManager.cs index 1a933e7..fad7af2 100644 --- a/src/Acl.Net.Core.Managers/ResourceManager.cs +++ b/src/Acl.Net.Core.Managers/ResourceManager.cs @@ -52,8 +52,8 @@ public class ResourceManager : IResourceManager where TResource : Resource { - protected readonly AclDbContext context; - protected readonly IInitialDataSeeder initialDataSeeder; + protected readonly AclDbContext Context; + protected readonly IInitialDataSeeder InitialDataSeeder; /// /// Initializes a new instance of the class. @@ -65,8 +65,8 @@ public ResourceManager( IInitialDataSeeder initialDataSeeder ) { - this.context = context; - this.initialDataSeeder = initialDataSeeder; + Context = context; + InitialDataSeeder = initialDataSeeder; } /// @@ -92,8 +92,8 @@ public virtual bool IsPermitted(TUser user, string resourceName) /// if the user is permitted to access the resource; otherwise, . 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)); } /// @@ -146,8 +146,8 @@ public virtual async Task IsPermittedAsync(TUser user, string resourceName /// The task result contains if the user is permitted to access the resource; otherwise, . public virtual async Task 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)); } /// @@ -204,7 +204,7 @@ public virtual async Task> IsPermittedAsync(TUser user, I /// 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); } @@ -219,7 +219,7 @@ public virtual TResource GetResourceByName(string resourceName) /// public virtual async Task 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); } } diff --git a/src/Acl.Net.Core.Managers/UserManager.cs b/src/Acl.Net.Core.Managers/UserManager.cs index 6e795cc..805df16 100644 --- a/src/Acl.Net.Core.Managers/UserManager.cs +++ b/src/Acl.Net.Core.Managers/UserManager.cs @@ -47,7 +47,7 @@ public class UserManager : IUserManager where TResource : Resource { - protected readonly AclDbContext context; + protected readonly AclDbContext Context; /// /// Initializes a new instance of the class. @@ -55,7 +55,7 @@ public class UserManager : IUserManagerThe ACL database context. public UserManager(AclDbContext context) { - this.context = context; + Context = context; } /// @@ -66,12 +66,12 @@ public UserManager(AclDbContext context) /// The retrieved or created user. public virtual TUser UserProcessing(string userName, TRole roleForNewUsers) { - var user = context.Users.FirstOrDefault(x => x.Name == userName) + var user = Context.Users.FirstOrDefault(x => x.Name == userName) ?? new TUser { Name = userName, RoleId = roleForNewUsers.Id }; if (!user.Id.Equals(default)) return user; - context.Users.Add(user); - context.SaveChanges(); + Context.Users.Add(user); + Context.SaveChanges(); return user; } @@ -84,12 +84,12 @@ public virtual TUser UserProcessing(string userName, TRole roleForNewUsers) /// A task that represents the asynchronous operation. The task result returns the retrieved or created user. public virtual async Task UserProcessingAsync(string userName, TRole roleForNewUsers) { - var user = await context.Users.FirstOrDefaultAsync(x => x.Name == userName) + var user = await Context.Users.FirstOrDefaultAsync(x => x.Name == userName) ?? new TUser { Name = userName, RoleId = roleForNewUsers.Id }; if (!user.Id.Equals(default)) return user; - await context.Users.AddAsync(user); - await context.SaveChangesAsync(); + await Context.Users.AddAsync(user); + await Context.SaveChangesAsync(); return user; } diff --git a/tests/Acl.Net.Core.Managers.Tests/Exceptions/ResourceNotFoundExceptionTests.cs b/tests/Acl.Net.Core.Managers.Tests/Exceptions/ResourceNotFoundExceptionTests.cs deleted file mode 100644 index da0ce5b..0000000 --- a/tests/Acl.Net.Core.Managers.Tests/Exceptions/ResourceNotFoundExceptionTests.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Xunit; -using System.Runtime.Serialization; -using Acl.Net.Core.Managers.Exceptions; -using Acl.Net.Core.Managers.Tests.Exceptions.Testable; - -namespace Acl.Net.Core.Managers.Tests.Exceptions; - -public class ResourceNotFoundExceptionTests : ExceptionSerializationTestBase -{ - [Fact] - internal void GasPriceExceededException_SerializationTest() - { - const string expectedMessage = "exception message"; - - RunSerializationTest(expectedMessage); - - var actualMessage = infoForGetObjectData.GetString("Message"); - Assert.Equal(expectedMessage, actualMessage); - } - - protected override TestableResourceNotFoundException CreateTestableException(SerializationInfo info, StreamingContext context) => new(info, context); -} \ No newline at end of file diff --git a/tests/Acl.Net.Core.Managers.Tests/Exceptions/Testable/ExceptionSerializationTestBase.cs b/tests/Acl.Net.Core.Managers.Tests/Exceptions/Testable/ExceptionSerializationTestBase.cs deleted file mode 100644 index 44094cb..0000000 --- a/tests/Acl.Net.Core.Managers.Tests/Exceptions/Testable/ExceptionSerializationTestBase.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Runtime.Serialization; - -namespace Acl.Net.Core.Managers.Tests.Exceptions.Testable; - -public abstract class ExceptionSerializationTestBase - where TTestableException : Exception -{ - protected SerializationInfo infoForGetObjectData = null!; - - protected void RunSerializationTest(string message) - { - var info = new SerializationInfo(typeof(TException), new FormatterConverter()); - info.AddValue("Message", message); - info.AddValue("InnerException", null, typeof(Exception)); - info.AddValue("HelpURL", null, typeof(string)); - info.AddValue("StackTraceString", null, typeof(string)); - info.AddValue("RemoteStackTraceString", null, typeof(string)); - info.AddValue("HResult", int.MinValue, typeof(int)); - info.AddValue("Source", null, typeof(string)); - info.AddValue("ClassName", "Exception"); - info.AddValue("RemoteStackIndex", 0, typeof(int)); - info.AddValue("ExceptionMethod", null, typeof(string)); - - var context = new StreamingContext(); - - var exception = CreateTestableException(info, context); - - infoForGetObjectData = new SerializationInfo(typeof(TException), new FormatterConverter()); - exception.GetObjectData(infoForGetObjectData, context); - } - - protected abstract TTestableException CreateTestableException(SerializationInfo info, StreamingContext context); -} diff --git a/tests/Acl.Net.Core.Managers.Tests/Exceptions/Testable/TestableResourceNotFoundException.cs b/tests/Acl.Net.Core.Managers.Tests/Exceptions/Testable/TestableResourceNotFoundException.cs deleted file mode 100644 index c568253..0000000 --- a/tests/Acl.Net.Core.Managers.Tests/Exceptions/Testable/TestableResourceNotFoundException.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Runtime.Serialization; -using Acl.Net.Core.Managers.Exceptions; - -namespace Acl.Net.Core.Managers.Tests.Exceptions.Testable; - -public class TestableResourceNotFoundException : ResourceNotFoundException -{ - public TestableResourceNotFoundException(SerializationInfo info, StreamingContext context) - : base(info, context) - { } -} diff --git a/tests/Acl.Net.Core.Managers.Tests/Mock/InMemoryAclDbContext.cs b/tests/Acl.Net.Core.Managers.Tests/Mock/InMemoryAclDbContext.cs index 41b7eb3..1824a55 100644 --- a/tests/Acl.Net.Core.Managers.Tests/Mock/InMemoryAclDbContext.cs +++ b/tests/Acl.Net.Core.Managers.Tests/Mock/InMemoryAclDbContext.cs @@ -6,39 +6,39 @@ namespace Acl.Net.Core.Managers.Tests.Mock; internal static class InMemoryAclDbContext { - private static readonly RoleDataSeeder roleDataSeeder = new(); - private static readonly Role userRole = roleDataSeeder.SeedUserRole(); - private static readonly Role adminRole = roleDataSeeder.SeedAdminRole(); + private static readonly RoleDataSeeder RoleDataSeeder = new(); + private static readonly Role UserRole = RoleDataSeeder.SeedUserRole(); + private static readonly Role AdminRole = RoleDataSeeder.SeedAdminRole(); internal static User UserAccount = new() { - Id = 1, Name = "UserAccount", RoleId = userRole.Id + Id = 1, Name = "UserAccount", RoleId = UserRole.Id }; internal static User AdminAccount = new() { Id = 2, Name = "AdminAccount", - RoleId = adminRole.Id + RoleId = AdminRole.Id }; internal static Resource PublicResource = new() { Id = 1, Name = "PublicResource", - RoleId = userRole.Id + RoleId = UserRole.Id }; internal static Resource PrivateResource = new() { Id = 2, Name = "PrivateResource", - RoleId = adminRole.Id + RoleId = AdminRole.Id }; - internal static List Users => new() { UserAccount, AdminAccount }; + internal static List Users => [UserAccount, AdminAccount]; - internal static List Resources => new() { PublicResource, PrivateResource }; + internal static List Resources => [PublicResource, PrivateResource]; internal static AclDbContext CreateContext() {