The `@rxap/authorization` package provides a robust and flexible way to manage user permissions in Angular applications. It supports permission-based view rendering, component enabling/disabling, and hierarchical scoping.
## Installation
1.**Directives**: Import the `HasPermissionModule` in your application or feature module to use the directives in your templates:
```typescript
import { HasPermissionModule } from '@rxap/authorization';
@NgModule({
imports: [
HasPermissionModule,
// ...
],
})
export class AppModule {}
```
Alternatively, you can import individual standalone directives as needed (e.g., `IfHasPermissionDirective`, `MatButtonHasEnablePermissionDirective`).
2.**Providers**: Use the `provideAuthorization()` utility to configure the service and its dependencies (like disabling authorization via config).
```typescript
import { provideAuthorization } from '@rxap/authorization';
import { ApplicationConfig } from '@angular/core';
export const appConfig: ApplicationConfig = {
providers: [
provideAuthorization(),
// ... other providers, ensure ConfigService is also provided/available if needed
]
};
```
## Authorization Service
The core of the package is the `AuthorizationService`. It holds the current user's permissions and provides methods to check access.
### Setting Permissions
Permissions are stored as a list of strings. You typically set these after user authentication.
If the user has the permission `'products/create'`, they will see the button. If they have `'users/create'`, they will not (unless they are also in the `'users'` scope).
### Nested Scopes
You can technically nest scopes by providing dot-separated scopes (e.g., `'admin.users'`), which would look for `'admin.users/permission'`.
## Disabling Authorization for Development
The `provideAuthorization()` function automatically configures the service to check the configuration for `authorization.disabled`.
If you are using `@rxap/config`, you can disable authorization by setting the `authorization.disabled` property to `true` in your configuration file/environment.
```json
{
"authorization":{
"disabled":true
}
}
```
This is useful for local development or testing environments where you want to bypass permission checks.