-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1049 from solliancenet/aa-datasource-rbac
Add RBAC support for data source resource provider
- Loading branch information
Showing
6 changed files
with
108 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/dotnet/Common/Extensions/AuthorizationServiceExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using FoundationaLLM.Common.Interfaces; | ||
using FoundationaLLM.Common.Models.Authentication; | ||
using FoundationaLLM.Common.Models.Authorization; | ||
using FoundationaLLM.Common.Models.ResourceProviders; | ||
|
||
namespace FoundationaLLM.Common.Extensions | ||
{ | ||
/// <summary> | ||
/// Extends the <see cref="IAuthorizationService"/> interface with helper methods. | ||
/// </summary> | ||
public static class AuthorizationServiceExtensions | ||
{ | ||
/// <summary> | ||
/// Filters the list of resources based on the authorizable action. | ||
/// </summary> | ||
/// <typeparam name="T">The object type of the resource being retrieved.</typeparam> | ||
/// <param name="authorizationService">The <see cref="IAuthorizationService"/> service.</param> | ||
/// <param name="instanceId">The FoundationaLLM instance identifier.</param> | ||
/// <param name="userIdentity">The <see cref="UnifiedUserIdentity"/> providing information about the calling user identity.</param> | ||
/// <param name="resources">The list of all resources.</param> | ||
/// <param name="authorizableAction">The authorizable action to be checked.</param> | ||
/// <returns>A list of resources on which the user identity is allowed to perform the authorizable action.</returns> | ||
public static async Task<List<ResourceProviderGetResult<T>>> FilterResourcesByAuthorizableAction<T>( | ||
this IAuthorizationService authorizationService, | ||
string instanceId, | ||
UnifiedUserIdentity userIdentity, | ||
List<T> resources, | ||
string authorizableAction) | ||
where T : ResourceBase | ||
{ | ||
var rolesWithActions = await authorizationService.ProcessRoleAssignmentsWithActionsRequest( | ||
instanceId, | ||
new RoleAssignmentsWithActionsRequest() | ||
{ | ||
Scopes = resources.Select(x => x.ObjectId!).ToList(), | ||
PrincipalId = userIdentity.UserId!, | ||
SecurityGroupIds = userIdentity.GroupIds | ||
}); | ||
|
||
var results = new List<ResourceProviderGetResult<T>>(); | ||
|
||
foreach (var resource in resources) | ||
if (rolesWithActions[resource.ObjectId!].Actions.Contains(authorizableAction)) | ||
results.Add(new ResourceProviderGetResult<T>() | ||
{ | ||
Resource = resource, | ||
Actions = rolesWithActions[resource.ObjectId!].Actions, | ||
Roles = rolesWithActions[resource.ObjectId!].Roles | ||
}); | ||
|
||
return results; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters