diff --git a/Volo.Abp.sln b/Volo.Abp.sln index efe6c1f39e..03b96ae867 100644 --- a/Volo.Abp.sln +++ b/Volo.Abp.sln @@ -280,11 +280,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.EventBus", "src\Vo EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.EventBus.Tests", "test\Volo.Abp.EventBus.Tests\Volo.Abp.EventBus.Tests.csproj", "{8C327AA0-BBED-4F8B-A88E-1DD97B04E58F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Caching.Tests", "test\Volo.Abp.Caching.Tests\Volo.Abp.Caching.Tests.csproj", "{B417D97C-330A-42CE-BDC6-93355B0A959A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Caching.Tests", "test\Volo.Abp.Caching.Tests\Volo.Abp.Caching.Tests.csproj", "{B417D97C-330A-42CE-BDC6-93355B0A959A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Serialization", "src\Volo.Abp.Serialization\Volo.Abp.Serialization.csproj", "{38EF3EC8-9915-4216-B646-4BEE07006943}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Serialization", "src\Volo.Abp.Serialization\Volo.Abp.Serialization.csproj", "{38EF3EC8-9915-4216-B646-4BEE07006943}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Serialization.Tests", "test\Volo.Abp.Serialization.Tests\Volo.Abp.Serialization.Tests.csproj", "{65FB5893-7CB6-4694-A692-7E666E347D29}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Serialization.Tests", "test\Volo.Abp.Serialization.Tests\Volo.Abp.Serialization.Tests.csproj", "{65FB5893-7CB6-4694-A692-7E666E347D29}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Authorization.Tests", "test\Volo.Abp.Authorization.Tests\Volo.Abp.Authorization.Tests.csproj", "{B10E37A1-43A1-4042-BAAA-F589302958D5}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -752,6 +754,10 @@ Global {65FB5893-7CB6-4694-A692-7E666E347D29}.Debug|Any CPU.Build.0 = Debug|Any CPU {65FB5893-7CB6-4694-A692-7E666E347D29}.Release|Any CPU.ActiveCfg = Release|Any CPU {65FB5893-7CB6-4694-A692-7E666E347D29}.Release|Any CPU.Build.0 = Release|Any CPU + {B10E37A1-43A1-4042-BAAA-F589302958D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B10E37A1-43A1-4042-BAAA-F589302958D5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B10E37A1-43A1-4042-BAAA-F589302958D5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B10E37A1-43A1-4042-BAAA-F589302958D5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -891,6 +897,7 @@ Global {B417D97C-330A-42CE-BDC6-93355B0A959A} = {37087D1B-3693-4E96-983D-A69F210BDE53} {38EF3EC8-9915-4216-B646-4BEE07006943} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8} {65FB5893-7CB6-4694-A692-7E666E347D29} = {37087D1B-3693-4E96-983D-A69F210BDE53} + {B10E37A1-43A1-4042-BAAA-F589302958D5} = {37087D1B-3693-4E96-983D-A69F210BDE53} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {BB97ECF4-9A84-433F-A80B-2A3285BDD1D5} diff --git a/src/Volo.Abp.Authorization/Volo/Abp/Authorization/AuthorizationInterceptor.cs b/src/Volo.Abp.Authorization/Volo/Abp/Authorization/AuthorizationInterceptor.cs index e7e99b777d..ad740ad61a 100644 --- a/src/Volo.Abp.Authorization/Volo/Abp/Authorization/AuthorizationInterceptor.cs +++ b/src/Volo.Abp.Authorization/Volo/Abp/Authorization/AuthorizationInterceptor.cs @@ -1,5 +1,4 @@ using System.Threading.Tasks; -using Microsoft.AspNetCore.Authorization; using Volo.Abp.Aspects; using Volo.Abp.DependencyInjection; using Volo.Abp.DynamicProxy; diff --git a/src/Volo.Abp.Authorization/Volo/Abp/Authorization/MethodInvocationAuthorizationService.cs b/src/Volo.Abp.Authorization/Volo/Abp/Authorization/MethodInvocationAuthorizationService.cs index 4e6bf097bb..913d89a7fd 100644 --- a/src/Volo.Abp.Authorization/Volo/Abp/Authorization/MethodInvocationAuthorizationService.cs +++ b/src/Volo.Abp.Authorization/Volo/Abp/Authorization/MethodInvocationAuthorizationService.cs @@ -35,10 +35,15 @@ namespace Volo.Abp.Authorization protected virtual IAuthorizeData[] GetAuthorizationDataAttributes(MethodInvocationAuthorizationContext context) { - return context.Method + var classAttributes = context.Method.DeclaringType .GetCustomAttributes(true) - .OfType() - .ToArray(); + .OfType(); + + var methodAttributes = context.Method + .GetCustomAttributes(true) + .OfType(); + + return classAttributes.Union(methodAttributes).ToArray(); } protected async Task CheckAsync(IAuthorizeData authorizationAttribute) diff --git a/test/Volo.Abp.Authorization.Tests/Volo.Abp.Authorization.Tests.csproj b/test/Volo.Abp.Authorization.Tests/Volo.Abp.Authorization.Tests.csproj new file mode 100644 index 0000000000..78375e68de --- /dev/null +++ b/test/Volo.Abp.Authorization.Tests/Volo.Abp.Authorization.Tests.csproj @@ -0,0 +1,25 @@ + + + + netcoreapp2.0 + latest + Volo.Abp.Authorization.Tests + Volo.Abp.Authorization.Tests + true + false + false + false + + + + + + + + + + + + + + diff --git a/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/AbpAuthorizationTestModule.cs b/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/AbpAuthorizationTestModule.cs new file mode 100644 index 0000000000..2a3ca8c5e9 --- /dev/null +++ b/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/AbpAuthorizationTestModule.cs @@ -0,0 +1,34 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Authorization.Permissions; +using Volo.Abp.Authorization.TestServices; +using Volo.Abp.Autofac; +using Volo.Abp.Modularity; + +namespace Volo.Abp.Authorization +{ + [DependsOn(typeof(AbpAutofacModule))] + [DependsOn(typeof(AbpAuthorizationModule))] + public class AbpAuthorizationTestModule : AbpModule + { + public override void PreConfigureServices(IServiceCollection services) + { + services.OnRegistred(context => + { + if (typeof(IMyAuthorizedService1).IsAssignableFrom(context.ImplementationType)) + { + context.Interceptors.TryAdd(); + } + }); + } + + public override void ConfigureServices(IServiceCollection services) + { + services.Configure(options => + { + options.DefinitionProviders.TryAdd(); + }); + + services.AddAssemblyOf(); + } + } +} \ No newline at end of file diff --git a/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/AuthorizationTestBase.cs b/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/AuthorizationTestBase.cs new file mode 100644 index 0000000000..bb443f9bae --- /dev/null +++ b/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/AuthorizationTestBase.cs @@ -0,0 +1,12 @@ +using Volo.Abp.TestBase; + +namespace Volo.Abp.Authorization +{ + public class AuthorizationTestBase : AbpIntegratedTest + { + protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) + { + options.UseAutofac(); + } + } +} \ No newline at end of file diff --git a/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/Authorization_Tests.cs b/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/Authorization_Tests.cs new file mode 100644 index 0000000000..9e30bfca50 --- /dev/null +++ b/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/Authorization_Tests.cs @@ -0,0 +1,34 @@ +using System.Threading.Tasks; +using Volo.Abp.Authorization.TestServices; +using Xunit; + +namespace Volo.Abp.Authorization +{ + public class Authorization_Tests : AuthorizationTestBase + { + private readonly IMyAuthorizedService1 _myAuthorizedService1; + + public Authorization_Tests() + { + _myAuthorizedService1 = GetRequiredService(); + } + + [Fact] + public void Should_Not_Allow_To_Call_Method_If_Has_No_Permission_ProtectedByClass() + { + Assert.Throws(() => + { + _myAuthorizedService1.ProtectedByClass(); + }); + } + + [Fact] + public async Task Should_Not_Allow_To_Call_Method_If_Has_No_Permission_ProtectedByClass_Async() + { + await Assert.ThrowsAsync(async () => + { + await _myAuthorizedService1.ProtectedByClassAsync(); + }); + } + } +} \ No newline at end of file diff --git a/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/AuthorizationTestPermissionDefinitionProvider.cs b/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/AuthorizationTestPermissionDefinitionProvider.cs new file mode 100644 index 0000000000..5775c5466c --- /dev/null +++ b/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/AuthorizationTestPermissionDefinitionProvider.cs @@ -0,0 +1,13 @@ +using Volo.Abp.Authorization.Permissions; + +namespace Volo.Abp.Authorization.TestServices +{ + public class AuthorizationTestPermissionDefinitionProvider : PermissionDefinitionProvider + { + public override void Define(IPermissionDefinitionContext context) + { + var group = context.AddGroup("TestGroup"); + group.AddPermission("MyAuthorizedService1"); + } + } +} diff --git a/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/IMyAuthorizedService1.cs b/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/IMyAuthorizedService1.cs new file mode 100644 index 0000000000..0ef6bde8a0 --- /dev/null +++ b/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/IMyAuthorizedService1.cs @@ -0,0 +1,15 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.Authorization.TestServices +{ + public interface IMyAuthorizedService1 + { + int Anonymous(); + + Task AnonymousAsync(); + + int ProtectedByClass(); + + Task ProtectedByClassAsync(); + } +} \ No newline at end of file diff --git a/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/MyAuthorizedService1.cs b/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/MyAuthorizedService1.cs new file mode 100644 index 0000000000..0b33dd0c98 --- /dev/null +++ b/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/MyAuthorizedService1.cs @@ -0,0 +1,34 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Authorization.TestServices +{ + [Authorize("MyAuthorizedService1")] + public class MyAuthorizedService1 : IMyAuthorizedService1, ITransientDependency + { + [AllowAnonymous] + public virtual int Anonymous() + { + return 42; + } + + [AllowAnonymous] + public virtual async Task AnonymousAsync() + { + await Task.Delay(10); + return 42; + } + + public virtual int ProtectedByClass() + { + return 42; + } + + public virtual async Task ProtectedByClassAsync() + { + await Task.Delay(10); + return 42; + } + } +} \ No newline at end of file