`getLocalization` method can be used with both `localization key` and [`LocalizationWithDefault`](https://github.com/abpframework/abp/blob/dev/npm/ng-packs/packages/core/src/lib/models/config.ts#L34) interface.
A permission is a simple policy that is granted or prohibited for a particular user, role or client. You can read more about [authorization in ABP](Authorization.md) document.
You can get permission of authenticated user using `getGrantedPolicy` selector of `ConfigState`.
You can get permission as boolean value from store:
```ts
```js
import { Store } from '@ngxs/store';
import { ConfigState } from '../states';
@ -21,7 +23,7 @@ export class YourComponent {
Or you can get it via `ConfigStateService`:
```ts
```js
import { ConfigStateService } from '../services/config-state.service';
export class YourComponent {
@ -55,7 +57,7 @@ You can use `PermissionGuard` if you want to control authenticated user's permis
Add `requiredPolicy` to the `routes` property in your routing module.
@ -4,13 +4,13 @@ You can replace some ABP components with your custom components.
The reason that you **can replace** but **cannot customize** default ABP components is disabling or changing a part of that component can cause problems. So we named those components as _Replaceable Components_.
## How to Use Replaceable Components
## How to Replacea Component
Create a new component that you want to use instead of an ABP component. Add that component to `declarations` and `entryComponents` in the `AppModule`.
Then, open the `app.component.ts` and dispatch the `AddReplaceableComponent` action to replace your component with an ABP component as shown below:
```ts
```js
import { ..., AddReplaceableComponent } from '@abp/ng.core';
Authorization is used to check if a user is allowed to perform some specific operations in the application.
Authorization is used to check if a user is allowed to perform some specific operations in the application.
ABP extends [ASP.NET Core Authorization](https://docs.microsoft.com/en-us/aspnet/core/security/authorization/introduction) by adding **permissions** as auto [policies](https://docs.microsoft.com/en-us/aspnet/core/security/authorization/policies) and allowing authorization system to be usable in the **[application services](Application-Services.md)** too.
@ -12,7 +12,7 @@ ASP.NET Core defines the [**Authorize**](https://docs.microsoft.com/en-us/aspnet
Example:
````csharp
```csharp
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
@ -43,11 +43,11 @@ namespace Acme.BookStore
}
}
````
```
*`Authorize` attribute forces the user to login into the application in order to use the `AuthorAppService` methods. So, `GetListAsync` method is only available to the authenticated users.
*`AllowAnonymous` suppresses the authentication. So, `GetAsync` method is available to everyone including unauthorized users.
*`[Authorize("BookStore_Author_Create")]` defines a policy (see [policy based authorization](https://docs.microsoft.com/en-us/aspnet/core/security/authorization/policies)) that is checked to authorize the current user.
-`Authorize` attribute forces the user to login into the application in order to use the `AuthorAppService` methods. So, `GetListAsync` method is only available to the authenticated users.
-`AllowAnonymous` suppresses the authentication. So, `GetAsync` method is available to everyone including unauthorized users.
-`[Authorize("BookStore_Author_Create")]` defines a policy (see [policy based authorization](https://docs.microsoft.com/en-us/aspnet/core/security/authorization/policies)) that is checked to authorize the current user.
"BookStore_Author_Create" is an arbitrary policy name. If you declare an attribute like that, ASP.NET Core authorization system expects a policy to be defined before.
@ -61,7 +61,7 @@ A permission is a simple policy that is granted or prohibited for a particular u
To define permissions, create a class inheriting from the `PermissionDefinitionProvider` as shown below:
* The "BookStore" group is shown as a new tab on the left side.
* "BookStore_Author_Create" on the right side is the permission name. You can grant or prohibit it for the role.
- The "BookStore" group is shown as a new tab on the left side.
- "BookStore_Author_Create" on the right side is the permission name. You can grant or prohibit it for the role.
When you save the dialog, it is saved to the database and used in the authorization system.
@ -97,7 +97,7 @@ When you save the dialog, it is saved to the database and used in the authorizat
"BookStore_Author_Create" is not a good permission name for the UI. Fortunately, `AddPermission` and `AddGroup` methods can take `LocalizableString` as second parameters:
Then you can define texts for "BookStore" and "Permission:BookStore_Author_Create" keys in the localization file:
````json
```json
"BookStore": "Book Store",
"Permission:BookStore_Author_Create": "Creating a new author"
````
```
> For more information, see the [localization document](Localization.md) on the localization system.
@ -126,21 +126,21 @@ The localized UI will be as seen below:
ABP supports [multi-tenancy](Multi-Tenancy.md) as a first class citizen. You can define multi-tenancy side option while defining a new permission. It gets one of the three values defined below:
***Host**: The permission is available only for the host side.
***Tenant**: The permission is available only for the tenant side.
***Both** (default): The permission is available both for tenant and host sides.
-**Host**: The permission is available only for the host side.
-**Tenant**: The permission is available only for the tenant side.
-**Both** (default): The permission is available both for tenant and host sides.
> If your application is not multi-tenant, you can ignore this option.
To set the multi-tenancy side option, pass to the third parameter of the `AddPermission` method:
The result on the UI is shown below (you probably want to localize permissions for your application):
@ -161,7 +161,7 @@ The result on the UI is shown below (you probably want to localize permissions f
For the example code, it is assumed that a role/user with "Author_Management" permission granted may have additional permissions. Then a typical application service that checks permissions can be defined as shown below:
````csharp
```csharp
[Authorize("Author_Management")]
public class AuthorAppService : ApplicationService, IAuthorAppService
{
@ -193,10 +193,10 @@ public class AuthorAppService : ApplicationService, IAuthorAppService
...
}
}
````
```
*`GetListAsync` and `GetAsync` will be available to users if they have `Author_Management` permission is granted.
* Other methods require additional permissions.
-`GetListAsync` and `GetAsync` will be available to users if they have `Author_Management` permission is granted.
- Other methods require additional permissions.
### Overriding a Permission by a Custom Policy
@ -210,7 +210,7 @@ ASP.NET Core provides the `IAuthorizationService` that can be used to check for
Example:
````csharp
```csharp
public async Task CreateAsync(CreateAuthorDto input)
{
var result = await AuthorizationService
@ -223,7 +223,7 @@ public async Task CreateAsync(CreateAuthorDto input)
//continue to the normal flow...
}
````
```
> `AuthorizationService` is available as a property when you derive from ABP's `ApplicationService` base class. Since it is widely used in application services, `ApplicationService` pre-injects it for you. Otherwise, you can directly [inject](Dependency-Injection.md) it into your class.
@ -231,14 +231,14 @@ Since this is a typical code block, ABP provides extension methods to simplify i
Example:
````csharp
```csharp
public async Task CreateAsync(CreateAuthorDto input)
`CheckAsync` extension method throws `AbpAuthorizationException` if the current user/client is not granted for the given permission. There is also `IsGrantedAsync` extension method that returns `true` or `false`.
@ -250,9 +250,9 @@ public async Task CreateAsync(CreateAuthorDto input)
You may need to check a policy/permission on the client side. For ASP.NET Core MVC / Razor Pages applications, you can use the `abp.auth` API. Example:
````js
```js
abp.auth.isGranted('MyPermissionName');
````
```
See [abp.auth](AspNetCore/JavaScript-API/Auth.md) API documentation for details.
@ -264,7 +264,7 @@ Permission management is normally done by an admin user using the permission man
If you need to manage permissions by code, inject the `IPermissionManager` and use as shown below:
`SetForUserAsync` sets the value (true/false) for a permission of a user. There are more extension methods like `SetForRoleAsync` and `SetForClientAsync`.
@ -296,15 +296,15 @@ public class MyService : ITransientDependency
Permission checking system is extensible. Any class derived from `PermissionValueProvider` (or implements `IPermissionValueProvider`) can contribute to the permission check. There are three pre-defined value providers:
*`UserPermissionValueProvider` checks if the current user is granted for the given permission. It gets user id from the current claims. User claim name is defined with the `AbpClaimTypes.UserId` static property.
*`RolePermissionValueProvider` checks if any of the roles of the current user is granted for the given permission. It gets role names from the current claims. Role claims name is defined with the `AbpClaimTypes.Role` static property.
*`ClientPermissionValueProvider` checks if the current client is granted for the given permission. This is especially useful on a machine to machine interaction where there is no current user. It gets the client id from the current claims. Client claim name is defined with the `AbpClaimTypes.ClientId` static property.
-`UserPermissionValueProvider` checks if the current user is granted for the given permission. It gets user id from the current claims. User claim name is defined with the `AbpClaimTypes.UserId` static property.
-`RolePermissionValueProvider` checks if any of the roles of the current user is granted for the given permission. It gets role names from the current claims. Role claims name is defined with the `AbpClaimTypes.Role` static property.
-`ClientPermissionValueProvider` checks if the current client is granted for the given permission. This is especially useful on a machine to machine interaction where there is no current user. It gets the client id from the current claims. Client claim name is defined with the `AbpClaimTypes.ClientId` static property.
You can extend the permission checking system by defining your own permission value provider.
Example:
````csharp
```csharp
public class SystemAdminPermissionValueProvider : PermissionValueProvider
{
public SystemAdminPermissionValueProvider(IPermissionStore permissionStore)
@ -314,7 +314,7 @@ public class SystemAdminPermissionValueProvider : PermissionValueProvider
public override string Name => "SystemAdmin";
public override async Task<PermissionGrantResult>
public override async Task<PermissionGrantResult>
CheckAsync(PermissionValueCheckContext context)
{
if (context.Principal?.FindFirst("User_Type")?.Value == "SystemAdmin")
@ -325,24 +325,24 @@ public class SystemAdminPermissionValueProvider : PermissionValueProvider
return PermissionGrantResult.Undefined;
}
}
````
```
This provider allows for all permissions to a user with a `User_Type` claim that has `SystemAdmin` value. It is common to use current claims and `IPermissionStore` in a permission value provider.
A permission value provider should return one of the following values from the `CheckAsync` method:
*`PermissionGrantResult.Granted` is returned to grant the user for the permission. If any of the providers return `Granted`, the result will be `Granted`, if no other provider returns `Prohibited`.
*`PermissionGrantResult.Prohibited` is returned to prohibit the user for the permission. If any of the providers return `Prohibited`, the result will always be `Prohibited`. Doesn't matter what other providers return.
*`PermissionGrantResult.Undefined` is returned if this value provider could not decide about the permission value. Return this to let other providers check the permission.
-`PermissionGrantResult.Granted` is returned to grant the user for the permission. If any of the providers return `Granted`, the result will be `Granted`, if no other provider returns `Prohibited`.
-`PermissionGrantResult.Prohibited` is returned to prohibit the user for the permission. If any of the providers return `Prohibited`, the result will always be `Prohibited`. Doesn't matter what other providers return.
-`PermissionGrantResult.Undefined` is returned if this value provider could not decide about the permission value. Return this to let other providers check the permission.
Once a provider is defined, it should be added to the `PermissionOptions` as shown below:
Use `IServiceCollection.AddAlwaysAllowAuthorization()` extension method to register the `AlwaysAllowAuthorizationService` to the [dependency injection](Dependency-Injection.md) system:
````csharp
```csharp
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddAlwaysAllowAuthorization();
}
````
```
This is already done for the startup template integration tests.