### File

`packages/core/src/lib/services/permission/permission.service.ts`

### Description

Service for managing user permissions within the application. This class extends `EuiService` and is responsible for handling user rights and permissions. It provides methods to initialize the service with user rights, check specific rights and permissions, and subscribe to changes in the user rights state.

See EuiService

Example :

    // Example of using the EuiPermissionService in an application
    const permissionService = new EuiPermissionService();
    permissionService.init([{ id: 'ADMIN', permissions: ['CREATE', 'READ', 'UPDATE', 'DELETE'] }]);
    if (permissionService.checkRight('ADMIN')) {
      console.log('User has ADMIN rights');
    }

### Extends

`EuiService`

### Index

###### Methods

- checkAttributePermission
- checkPermission
- checkRight
- getState
- init
- updateState

### Constructor

`constructor(log: LogService, store: StoreService)`

Parameters :

Name | Type | Optional
log | `LogService` | No
store | `StoreService` | No

### Methods

checkAttributePermission
`checkAttributePermission(rightsAndPermission: string)`

Checks if the user has the specified attribute permission. This method interprets a string that represents a combination of rights and/or permissions and determines whether the user has the specified permissions. The string format can be 'RIGHT.PERMISSION' or just 'RIGHT'. For multiple rights, it can be 'RIGHT1.RIGHT2.PERMISSION'.

Example :

                                      Format can be 'RIGHT', 'RIGHT.PERMISSION', or 'RIGHT1.RIGHT2.PERMISSION'.

Example :

    // Check a single right
    const hasRight = permissionService.checkAttributePermission('VIEW');

// Check a right with a specific permission const hasRightWithPermission = permissionService.checkAttributePermission('EDIT.APPROVE');

// Check multiple rights with a specific permission const hasMultipleRights = permissionService.checkAttributePermission('ADMIN.VIEW.APPROVE');

Parameters :

Name | Type | Optional | Description

rightsAndPermission

`string`

No

- A string representing the rights and/or permissions to check. Format can be 'RIGHT', 'RIGHT.PERMISSION', or 'RIGHT1.RIGHT2.PERMISSION'.

Returns : `boolean`

True if the user has the specified attribute permission, false otherwise.

checkPermission
`checkPermission(rightId: string, permission: string)`

Checks if the user has a specific permission within a right. This method determines whether the user has a specific permission associated with a given right.

Example :

    // Check if the user has 'APPROVE' permission in the 'EDIT' right
    const canApproveEdit = permissionService.checkPermission('EDIT', 'APPROVE');

Parameters :

Name | Type | Optional | Description

rightId

`string`

No

- The ID of the right.

permission

`string`

No

- The permission to check within the right.

Returns : `boolean`

True if the user has the specified permission in the given right, false otherwise.

checkRight
`checkRight(rightId: string)`

Checks if the user has a specific right. This method determines whether the user possesses a particular right identified by its ID.

Example :

    // Check if the user has the 'ADMIN' right
    const isAdmin = permissionService.checkRight('ADMIN');

Parameters :

Name | Type | Optional | Description

rightId

`string`

No

- The ID of the right to check.

Returns : `boolean`

True if the user has the specified right, false otherwise.

getState
`getState()`

Type parameters :

- K

Retrieves the current state of user rights by subscribing to changes. This method is used to access the current state of user rights in the service. It returns an Observable that emits the current user rights. The method is generic and can be parameterized to return a specific type that extends from `EuiUserRight[]`.

Example :

                         The type of the emitted value is `K`, which is `EuiUserRight[]` by default.

Example :

    // Example of subscribing to the user rights state
    permissionService.getState().subscribe(currentRights => {
      console.log('Current User Rights:', currentRights);
    });

// Example with a specific type permissionService.getState<CustomUserRightType[]>().subscribe(customRights => { console.log('Custom User Rights:', customRights); });

Returns : `Observable<K>`

An Observable that emits the current state of user rights. The type of the emitted value is `K`, which is `EuiUserRight[]` by default.

init
`init(rights: EuiUserRight[])`

Initializes the permission service with a set of user rights. This method sets up the initial state of the service based on the provided user rights. It should be called before using other methods of the service to ensure it is properly configured.

Example :

                                 Each `EuiUserRight` object should contain at least an `id` property,
                                 and optionally a `permissions` property which is an array of strings.

                                        The status object includes a `success` boolean indicating
                                        whether the initialization was successful, and an `error` property
                                        with a message in case of failure.

Example :

    // Example of initializing the service with two user rights
    const userRights = [
      { id: 'ADMIN', permissions: ['CREATE', 'READ', 'UPDATE', 'DELETE'] },
      { id: 'USER', permissions: ['READ'] }
    ];
    permissionService.init(userRights).subscribe(status => {
      if (status.success) {
        console.log('Initialization successful');
      } else {
        console.error('Initialization failed:', status.error);
      }
    });

Parameters :

Name | Type | Optional | Description

rights

`EuiUserRight[]`

No

- An array of user rights to initialize the service with. Each `EuiUserRight` object should contain at least an `id` property, and optionally a `permissions` property which is an array of strings.

Returns : `Observable<EuiServiceStatus>`

An Observable that emits the status of the initialization. The status object includes a `success` boolean indicating whether the initialization was successful, and an `error` property with a message in case of failure.

updateState
`updateState(rights: EuiUserRight[])`

Updates the state of the service with a new set of user rights. This method is used to modify the current user rights in the service. It takes an array of `EuiUserRight` objects and updates the service's state. This change is then propagated to all subscribers of the state.

Example :

                                 Each `EuiUserRight` object should contain an `id` property,
                                 and optionally a `permissions` property which is an array of strings.

Example :

    // Example of updating the service's state with new user rights
    const newUserRights = [
      { id: 'USER', permissions: ['READ', 'COMMENT'] },
      { id: 'MODERATOR', permissions: ['READ', 'WRITE', 'DELETE'] }
    ];
    permissionService.updateState(newUserRights);

Parameters :

Name | Type | Optional | Description

rights

`EuiUserRight[]`

No

- An array of `EuiUserRight` objects to update the service's state with. Each `EuiUserRight` object should contain an `id` property, and optionally a `permissions` property which is an array of strings.

Returns : `void`

# results matching ""

# No results matching ""