Skip to content

Commit

Permalink
- downgrade version of EFCore at DB package
Browse files Browse the repository at this point in the history
- update interface IAclmanager
- update `IsPermitted(TRole role, TResource resource)`: check by resource name, not by Id
  • Loading branch information
ArdenHide committed Nov 22, 2024
1 parent 25a9653 commit 12dcb8b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/Acl.Net.Core.Database/Acl.Net.Core.Database.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Acl.Net.Core.Database/RoleDataSeeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ public Role<int> SeedAdminRole()
{
return new Role<int> { Id = 1, Name = "Admin" };
}
}
}
15 changes: 9 additions & 6 deletions src/Acl.Net.Core.Managers/AclManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Acl.Net.Core.Managers;

/// <summary>
/// Manages access control lists (ACLs) using integer keys.
/// This class provides a simplified interface for managing ACLs with integer keys, by extending the more generic <see cref="AclManager{TKey}"/> with TKey.
/// Manages access control lists (ACLs) using integer keys.<br/>
/// This class provides a simplified interface for managing ACLs with integer keys, by extending the more generic <see cref="AclManager{TKey}"/>.
/// </summary>
public class AclManager : AclManager<int>, IAclManager
{
Expand Down Expand Up @@ -35,7 +35,7 @@ AclDbContext context
}

/// <summary>
/// Manages access control lists (ACLs) using keys of type <see cref="TKey"/>.
/// Manages access control lists (ACLs) using keys of type <see cref="TKey"/>.<br/>
/// This class provides the base functionality for managing ACLs with specified key types.
/// </summary>
/// <typeparam name="TKey">The type of the key, which must implement <see cref="IEquatable{TKey}"/>.</typeparam>
Expand All @@ -57,14 +57,14 @@ AclDbContext<TKey> context
}

/// <summary>
/// Manages access control lists (ACLs) using keys, users, roles, and resources of specified types.
/// Manages access control lists (ACLs) using keys, users, roles, and resources of specified types. <br/>
/// This class provides the complete functionality for managing ACLs with specified key, user, role, and resource types.
/// </summary>
/// <typeparam name="TKey">The type of the key, which must implement <see cref="IEquatable{TKey}"/>.</typeparam>
/// <typeparam name="TUser">The type of the user, which must be a derived type of <see cref="User{TKey}"/>.</typeparam>
/// <typeparam name="TRole">The type of the role, which must be a derived type of <see cref="Role{TKey}"/></typeparam>
/// <typeparam name="TResource">The type of the resource, which must be a derived type of <see cref="Resource{TKey}"/></typeparam>
public class AclManager<TKey, TUser, TRole, TResource> : IAclManager<TKey, TUser, TResource>
public class AclManager<TKey, TUser, TRole, TResource> : IAclManager<TKey, TUser, TRole, TResource>

Check notice on line 67 in src/Acl.Net.Core.Managers/AclManager.cs

View check run for this annotation

codefactor.io / CodeFactor

src/Acl.Net.Core.Managers/AclManager.cs#L67

File may only contain a single type. (SA1402)

Check warning on line 67 in src/Acl.Net.Core.Managers/AclManager.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Reduce the number of generic parameters in the 'AclManager' class to no more than the 2 authorized. (https://rules.sonarsource.com/csharp/RSPEC-2436)

Check warning on line 67 in src/Acl.Net.Core.Managers/AclManager.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Reduce the number of generic parameters in the 'AclManager' class to no more than the 2 authorized. (https://rules.sonarsource.com/csharp/RSPEC-2436)
where TKey : IEquatable<TKey>
where TUser : User<TKey>, new()
where TRole : Role<TKey>
Expand All @@ -88,20 +88,23 @@ AclDbContext<TKey, TUser, TRole, TResource> context
Context = context;
}

/// <inheritdoc />
public bool IsPermitted(string userName, string resourceName)
{
var user = GetUserByName(userName);
return IsAdmin(user) || GetUserRoles(user.Name).Any(role => IsPermitted(role, GetResourceByName(resourceName)));
}

/// <inheritdoc />
public bool IsPermitted(TUser user, TResource resource)
{
return IsAdmin(user) || GetUserRoles(user.Name).Any(role => IsPermitted(role, resource));
}

/// <inheritdoc />
public bool IsPermitted(TRole role, TResource resource)
{
return IsAdmin(role) || Context.Resources.Any(r => r.RoleId.Equals(role.Id) && r.Id.Equals(resource.Id));
return IsAdmin(role) || Context.Resources.Any(res => res.Name.Equals(resource.Name) && res.RoleId.Equals(role.Id));
}

public bool IsAdmin(TKey roleId) => roleId.Equals(InitialDataSeeder.SeedAdminRole().Id);
Expand Down
56 changes: 15 additions & 41 deletions src/Acl.Net.Core.Managers/IAclManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,71 +12,45 @@ public interface IAclManager : IAclManager<int>;
/// 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>>
public interface IAclManager<TKey> : IAclManager<TKey, User<TKey>, Role<TKey>, Resource<TKey>>
where TKey : IEquatable<TKey>;

/// <summary>
/// Defines the contract for Access Control List (ACL) management with support for specific key, user, and resource types.
/// </summary>
/// <typeparam name="TKey">The type of key, which must implement <see cref="IEquatable{TKey}"/>.</typeparam>
/// <typeparam name="TUser">The type representing a user, which must inherit from <see cref="User{TKey}"/>.</typeparam>
/// <typeparam name="TRole">The type representing a role, which must inherit from <see cref="Role{TKey}"/>.</typeparam>
/// <typeparam name="TResource">The type representing a resource, which must inherit from <see cref="Resource{TKey}"/>.</typeparam>
public interface IAclManager<TKey, in TUser, TResource>
public interface IAclManager<in TKey, in TUser, in TRole, in TResource>

Check warning on line 25 in src/Acl.Net.Core.Managers/IAclManager.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Reduce the number of generic parameters in the 'IAclManager' interface to no more than the 2 authorized. (https://rules.sonarsource.com/csharp/RSPEC-2436)

Check warning on line 25 in src/Acl.Net.Core.Managers/IAclManager.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Reduce the number of generic parameters in the 'IAclManager' interface to no more than the 2 authorized. (https://rules.sonarsource.com/csharp/RSPEC-2436)
where TKey : IEquatable<TKey>
where TUser : User<TKey>
where TRole: Role<TKey>
where TResource : Resource<TKey>
{
/// <summary>
/// Determines if the specified user by name is permitted to access the specified resource by name.
/// </summary>
/// <param name="userName">The name of the user to check permission for.</param>
/// <param name="resourceName">The name of the resource to check permission against.</param>
/// <returns>
/// <see langword="true"/> if the user is permitted to access the resource; otherwise, <see langword="false"/>.
/// </returns>
/// <exception cref="ResourceNotFoundException">Thrown when the specified resource name does not exist.</exception>
//public bool IsPermitted(string userName, string resourceName);

/// <summary>
/// Determines if the specified user object is permitted to access the specified resource by name.
/// </summary>
/// <param name="user">The user object to check permission for.</param>
/// <param name="resourceName">The name of the resource to check permission against.</param>
/// <returns>
/// <see langword="true"/> if the user is permitted to access the resource; otherwise, <see langword="false"/>.
/// </returns>
/// <exception cref="ResourceNotFoundException">Thrown when the specified resource name does not exist.</exception>
//public bool IsPermitted(TUser user, string resourceName);

/// <summary>
/// Determines if the specified user by name is permitted to access the specified resource object.
/// </summary>
/// <param name="userName">The name of the user to check permission for.</param>
/// <param name="resource">The resource object to check permission against.</param>
/// <returns>
/// <see langword="true"/> if the user is permitted to access the resource; otherwise, <see langword="false"/>.
/// </returns>
//public bool IsPermitted(string userName, TResource resource);
/// <returns><see langword="true"/> if the user is permitted to access the resource; otherwise, <see langword="false"/>.</returns>
/// <exception cref="ResourceNotFoundException">Thrown when the specified resource by name does not exist.</exception>
/// <exception cref="UserNotFoundException">Thrown when the specified user by name does not exist.</exception>
public bool IsPermitted(string userName, string resourceName);

/// <summary>
/// Determines if the specified user object is permitted to access the specified resource object.
/// </summary>
/// <param name="user">The user object to check permission for.</param>
/// <param name="resource">The resource object to check permission against.</param>
/// <returns>
/// <see langword="true"/> if the user is permitted to access the resource; otherwise, <see langword="false"/>.
/// </returns>
//public bool IsPermitted(TUser user, TResource resource);
/// <returns> <see langword="true"/> if the user is permitted to access the resource; otherwise, <see langword="false"/>.</returns>
public bool IsPermitted(TUser user, TResource resource);

/// <summary>
/// Determines the resources that the specified user by name is permitted to access from a collection of resource names.
/// Determines whether the specified role is permitted to access the given resource.
/// </summary>
/// <param name="userName">The name of the user to check permission for.</param>
/// <param name="resourceNames">The collection of resource names to check permissions against.</param>
/// <returns>
/// A collection of <see cref="TResource"/> objects that the user is permitted to access;
/// an empty collection if the user is not permitted to access any of the resources.
/// </returns>
/// <exception cref="ResourceNotFoundException">Thrown when one or more of the specified resource names do not exist.</exception>
//public IEnumerable<TResource> IsPermitted(string userName, IEnumerable<string> resourceNames);
/// <param name="role">The role to check permissions for.</param>
/// <param name="resource">The resource to check.</param>
/// <returns><see langword="true"/> if the role is permitted to access the resource; otherwise, <see langword="false"/>.</returns>
public bool IsPermitted(TRole role, TResource resource);
}

0 comments on commit 12dcb8b

Please sign in to comment.