Browse Source

Merge pull request #8663 from abpframework/enisn/rel-4.3/permission-state-provider

Docs - Add Permission State Provider section to Authorization.md
pull/8868/head
Halil İbrahim Kalkan 5 years ago
committed by GitHub
parent
commit
33e1245286
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 82
      docs/en/Authorization.md

82
docs/en/Authorization.md

@ -236,6 +236,88 @@ When you write this code inside your permission definition provider, it finds th
> Tip: It is better to check the value returned by the `GetPermissionOrNull` method since it may return null if the given permission was not defined.
### Permission depending on a Condition
You may want to disable a permission based on a condition. Disabled permissions are not visible on the UI and always returns `prohibited` when you check them
Permission conditions can be managed according to a condition such as a Feature, Global Feature, or a custom condition. Any condition which implements `IPermissionStateProvider` can be added via calling `AddStateProvider()` method for **PermissionDefinition**.
See examples below:
- To depend on a Global Feature
```csharp
var authorPermission = myGroup.AddPermission("Author_Management");
// There are a couple of ways to do:
authorPermission.RequireGlobalFeatures("AuthorManagementFeature");
authorPermission.RequireGlobalFeatures(typeof(AuthorManagementFeature));
authorPermission.AddStateProviders(new RequireGlobalFeaturesPermissionStateProvider("AuthorManagementFeature"));
// Even it can be depend on multiple:
authorPermission.RequireGlobalFeatures("BookStoreFeature", "AuthorManagementFeature");
authorPermission.RequireGlobalFeatures(requiresAll: true, "BookStoreFeature", "AuthorManagementFeature");
authorPermission.RequireGlobalFeatures(requiresAll: true, typeof(BookStoreFeature), (AuthorManagementFeature);
```
- To depend on a Feature
```csharp
var authorPermission = myGroup.AddPermission("Author_Management");
// Pre-defined State Provider can be used:
authorPermission.AddStateProviders(new RequireFeaturesPermissionStateProvider("BookStore.AuthorManagement"));
// Also can be depend on multiple features
authorPermission.AddStateProviders(
new RequireFeaturesPermissionStateProvider("BookStore.Authors","BookStore.AuthorManagement"));
authorPermission.AddStateProviders(
new RequireFeaturesPermissionStateProvider(requiresAll: true, "BookStore.Authors","BookStore.AuthorManagement"));
```
- To depend on a Custom Condition
```csharp
var authorPermission = myGroup.AddPermission("Author_Management");
// To depend on your custom condition
authorPermission.AddStateProviders(new MyCustomPermissionStateProvider());
```
Custom condition class have to implement `IPermissionStateProvider`.
- Also State Providers can be configured globally via using `GlobalStateProviders` in **AbpPermissionOptions**
```csharp
Configure<AbpPermissionOptions>(options =>
{
options.GlobalStateProviders.Add<MyCustomPermissionStateProvider>();
// Or type can be pass as parameter:
options.GlobalStateProviders.Add(typeof(MyCustomPermissionStateProvider));
});
```
Content of **MyCustomPermissionStateProvider** looks like:
```csharp
public class MyCustomPermissionStateProvider : IPermissionStateProvider
{
public Task<bool> IsEnabledAsync(PermissionStateContext context)
{
// You can implement your own logic here.
// That check works globally for each permission.
return Task.FromResult(
context.Permission.Name.StartsWith("Acme.BookStore"));
}
}
```
## IAuthorizationService
ASP.NET Core provides the `IAuthorizationService` that can be used to check for authorization. Once you inject, you can use it in your code to conditionally control the authorization.

Loading…
Cancel
Save