Skip to content

Commit d24b749

Browse files
authored
Merge pull request #24515 from abpframework/ClientResourcePermission
Add ClientResourcePermissionValueProvider implementation
2 parents 151784f + af584e4 commit d24b749

File tree

85 files changed

+801
-82
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+801
-82
lines changed

framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/AbpAuthorizationModule.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public override void ConfigureServices(ServiceConfigurationContext context)
4747

4848
options.ResourceValueProviders.Add<UserResourcePermissionValueProvider>();
4949
options.ResourceValueProviders.Add<RoleResourcePermissionValueProvider>();
50+
options.ResourceValueProviders.Add<ClientResourcePermissionValueProvider>();
5051
});
5152

5253
Configure<AbpVirtualFileSystemOptions>(options =>

framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/ClientPermissionValueProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public override async Task<MultiplePermissionGrantResult> CheckAsync(PermissionV
4444
var clientId = context.Principal?.FindFirst(AbpClaimTypes.ClientId)?.Value;
4545
if (clientId == null)
4646
{
47-
return new MultiplePermissionGrantResult(permissionNames); ;
47+
return new MultiplePermissionGrantResult(permissionNames);
4848
}
4949

5050
using (CurrentTenant.Change(null))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Linq;
2+
using System.Threading.Tasks;
3+
using Volo.Abp.MultiTenancy;
4+
using Volo.Abp.Security.Claims;
5+
6+
namespace Volo.Abp.Authorization.Permissions.Resources;
7+
8+
public class ClientResourcePermissionValueProvider : ResourcePermissionValueProvider
9+
{
10+
public const string ProviderName = "C";
11+
12+
public override string Name => ProviderName;
13+
14+
protected ICurrentTenant CurrentTenant { get; }
15+
16+
public ClientResourcePermissionValueProvider(IResourcePermissionStore resourcePermissionStore, ICurrentTenant currentTenant)
17+
: base(resourcePermissionStore)
18+
{
19+
CurrentTenant = currentTenant;
20+
}
21+
22+
public override async Task<PermissionGrantResult> CheckAsync(ResourcePermissionValueCheckContext context)
23+
{
24+
var clientId = context.Principal?.FindFirst(AbpClaimTypes.ClientId)?.Value;
25+
26+
if (clientId == null)
27+
{
28+
return PermissionGrantResult.Undefined;
29+
}
30+
31+
using (CurrentTenant.Change(null))
32+
{
33+
return await ResourcePermissionStore.IsGrantedAsync(context.Permission.Name, context.ResourceName, context.ResourceKey, Name, clientId)
34+
? PermissionGrantResult.Granted
35+
: PermissionGrantResult.Undefined;
36+
}
37+
}
38+
39+
public override async Task<MultiplePermissionGrantResult> CheckAsync(ResourcePermissionValuesCheckContext context)
40+
{
41+
var permissionNames = context.Permissions.Select(x => x.Name).Distinct().ToArray();
42+
Check.NotNullOrEmpty(permissionNames, nameof(permissionNames));
43+
44+
var clientId = context.Principal?.FindFirst(AbpClaimTypes.ClientId)?.Value;
45+
if (clientId == null)
46+
{
47+
return new MultiplePermissionGrantResult(permissionNames);
48+
}
49+
50+
using (CurrentTenant.Change(null))
51+
{
52+
return await ResourcePermissionStore.IsGrantedAsync(permissionNames, context.ResourceName, context.ResourceKey, Name, clientId);
53+
}
54+
}
55+
}

modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/UserRoleFinder.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public virtual async Task<List<UserFinderResult>> SearchUserAsync(string filter,
3535
{
3636
page = page < 1 ? 1 : page;
3737
var users = await IdentityUserRepository.GetListAsync(filter: filter, skipCount: (page - 1) * 10, maxResultCount: 10);
38-
return users.Select(user => new UserFinderResult
38+
return users.Select(x => new UserFinderResult
3939
{
40-
Id = user.Id,
41-
UserName = user.UserName
40+
Id = x.Id,
41+
UserName = x.UserName
4242
}).ToList();
4343
}
4444
}
@@ -49,10 +49,10 @@ public virtual async Task<List<RoleFinderResult>> SearchRoleAsync(string filter,
4949
{
5050
page = page < 1 ? 1 : page;
5151
var roles = await IdentityRoleRepository.GetListAsync(filter: filter, skipCount: (page - 1) * 10, maxResultCount: 10);
52-
return roles.Select(user => new RoleFinderResult
52+
return roles.Select(x => new RoleFinderResult
5353
{
54-
Id = user.Id,
55-
RoleName = user.Name
54+
Id = x.Id,
55+
RoleName = x.Name
5656
}).ToList();
5757
}
5858
}
@@ -62,10 +62,10 @@ public virtual async Task<List<UserFinderResult>> SearchUserByIdsAsync(Guid[] id
6262
using (IdentityUserRepository.DisableTracking())
6363
{
6464
var users = await IdentityUserRepository.GetListByIdsAsync(ids);
65-
return users.Select(user => new UserFinderResult
65+
return users.Select(x => new UserFinderResult
6666
{
67-
Id = user.Id,
68-
UserName = user.UserName
67+
Id = x.Id,
68+
UserName = x.UserName
6969
}).ToList();
7070
}
7171
}
@@ -75,10 +75,10 @@ public virtual async Task<List<RoleFinderResult>> SearchRoleByNamesAsync(string[
7575
using (IdentityUserRepository.DisableTracking())
7676
{
7777
var roles = await IdentityRoleRepository.GetListAsync(names);
78-
return roles.Select(user => new RoleFinderResult
78+
return roles.Select(x => new RoleFinderResult
7979
{
80-
Id = user.Id,
81-
RoleName = user.Name
80+
Id = x.Id,
81+
RoleName = x.Name
8282
}).ToList();
8383
}
8484
}

modules/identity/src/Volo.Abp.PermissionManagement.Domain.Identity/Volo/Abp/PermissionManagement/Identity/RoleResourcePermissionProviderKeyLookupService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public virtual async Task<List<ResourcePermissionProviderKeyInfo>> SearchAsync(s
3030
return roles.Select(r => new ResourcePermissionProviderKeyInfo(r.RoleName, r.RoleName)).ToList();
3131
}
3232

33-
public virtual async Task<List<ResourcePermissionProviderKeyInfo>> SearchAsync(string[] keys, CancellationToken cancellationToken = default)
33+
public virtual Task<List<ResourcePermissionProviderKeyInfo>> SearchAsync(string[] keys, CancellationToken cancellationToken = default)
3434
{
35-
var roles = await UserRoleFinder.SearchRoleByNamesAsync(keys.Distinct().ToArray());
36-
return roles.Select(r => new ResourcePermissionProviderKeyInfo(r.RoleName, r.RoleName)).ToList();
35+
// Keys are role names
36+
return Task.FromResult(keys.Select(x => new ResourcePermissionProviderKeyInfo(x, x)).ToList());
3737
}
3838
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace Volo.Abp.IdentityServer.Clients;
4+
5+
public class ClientFinderResult
6+
{
7+
public Guid Id { get; set; }
8+
9+
public string ClientId { get; set; }
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
4+
namespace Volo.Abp.IdentityServer.Clients;
5+
6+
public interface IClientFinder
7+
{
8+
Task<List<ClientFinderResult>> SearchAsync(string filter, int page = 1);
9+
}

modules/identityserver/src/Volo.Abp.IdentityServer.Domain.Shared/Volo/Abp/IdentityServer/Localization/Resources/FR.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"InvalidUsername": "Nom d'utilisateur ou mot de passe invalide!",
1212
"InvalidAuthenticatorCode": "Code d'authentification invalide !",
1313
"InvalidRecoveryCode": "Code de récupération invalide !",
14-
"TheTargetUserIsNotLinkedToYou": "L'utilisateur cible n'est pas lié à vous!"
14+
"TheTargetUserIsNotLinkedToYou": "L'utilisateur cible n'est pas lié à vous!",
15+
"ClientResourcePermissionProviderKeyLookupService": "Client"
1516
}
1617
}

modules/identityserver/src/Volo.Abp.IdentityServer.Domain.Shared/Volo/Abp/IdentityServer/Localization/Resources/ar.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"InvalidUsername": "اسم المستخدم أو كلمة المرور غير صالحة!",
1212
"InvalidAuthenticatorCode": "كود المصدق غير صالح!",
1313
"InvalidRecoveryCode": "رمز الاسترداد غير صالح!",
14-
"TheTargetUserIsNotLinkedToYou": "المستخدم المستهدف غير مرتبط بك!"
14+
"TheTargetUserIsNotLinkedToYou": "المستخدم المستهدف غير مرتبط بك!",
15+
"ClientResourcePermissionProviderKeyLookupService": "العميل"
1516
}
1617
}

modules/identityserver/src/Volo.Abp.IdentityServer.Domain.Shared/Volo/Abp/IdentityServer/Localization/Resources/cs.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"InvalidUsername": "Neplatné uživatelské jméno či heslo!",
1212
"InvalidAuthenticatorCode": "Neplatný ověřovací kód!",
1313
"InvalidRecoveryCode": "Neplatný kód pro obnovení!",
14-
"TheTargetUserIsNotLinkedToYou": "Cílový uživatel s vámi není spojen!"
14+
"TheTargetUserIsNotLinkedToYou": "Cílový uživatel s vámi není spojen!",
15+
"ClientResourcePermissionProviderKeyLookupService": "Klient"
1516
}
1617
}

0 commit comments

Comments
 (0)