Abp: Question Angular template: Is there any function available to check if user has permission

Created on 1 Feb 2020  路  7Comments  路  Source: abpframework/abp

Angular template v2.0.1

I need to hide or disable components, I need something to check policies and act like so:

````xml

...

````

Is there any way to do this, using directive, service, something?

effort-0.5 question

Most helpful comment

You can use the PermissionDirective to manage the permission of a DOM element. It can be used in two ways.

Attiribute usage:

<button abpPermission="Type here a policy key e.g: AbpIdentity.Users.Create">Create a new user</button>

Structural usage:

<button *abpPermission="'Type here a policy key'">Create a new user</button>

You should use the structural usage when you would like to hide a component.

You can also manage the permission of a route with PermissinGuard:

import { PermissionGuard } from '@abp/ng.core';
// ...
{
  path: 'your-route-path',
  component: YourComponent
  canActivate: [PermissionGuard],
  data: {
    routes: {
      requiredPolicy: 'Type here a policy key',
    },
  },
}

To disable a DOM element

Add a select decorator to your component as shown below:

import {聽ConfigState } from '@abp/ng.core';
import { Select } from '@ngxs/store';
// ...
export class YourComponent {
  @Select(ConfigState.getGrantedPolicy('Type here a policy key'))
  createButtonPermission$: Observable<boolean>
// ...

Use disabled attiribute on a DOM element like this:

<button [disabled]="!(createButtonPermission$ | async)">Create new</button>

All 7 comments

@thiag0coelho do you mean permissions ?

@mehmet-erim can you write for different use cases?

@hikalkan That worked for hiding. Thanks!
It'll be nice if there's anything to disable components also. :)

You can use the PermissionDirective to manage the permission of a DOM element. It can be used in two ways.

Attiribute usage:

<button abpPermission="Type here a policy key e.g: AbpIdentity.Users.Create">Create a new user</button>

Structural usage:

<button *abpPermission="'Type here a policy key'">Create a new user</button>

You should use the structural usage when you would like to hide a component.

You can also manage the permission of a route with PermissinGuard:

import { PermissionGuard } from '@abp/ng.core';
// ...
{
  path: 'your-route-path',
  component: YourComponent
  canActivate: [PermissionGuard],
  data: {
    routes: {
      requiredPolicy: 'Type here a policy key',
    },
  },
}

To disable a DOM element

Add a select decorator to your component as shown below:

import {聽ConfigState } from '@abp/ng.core';
import { Select } from '@ngxs/store';
// ...
export class YourComponent {
  @Select(ConfigState.getGrantedPolicy('Type here a policy key'))
  createButtonPermission$: Observable<boolean>
// ...

Use disabled attiribute on a DOM element like this:

<button [disabled]="!(createButtonPermission$ | async)">Create new</button>

@mehmet-erim Is there a way to use OR/AND within *abpPermission? Thats because I have a "more actions" menu button that shows edit and delete buttons, and want to hide the menu button if cannot edit and delete

@luisp88 You can use permission directive like this:

*abpPermission="'YourModule.Edit || YourModule.Delete'"

or

*abpPermission="'YourModule.Edit && YourModule.Delete'"

For further information, see the permission management documentation

You can also get permissions manually via getGrantedPolicy selector of ConfigState. See https://docs.abp.io/en/abp/latest/UI/Angular/Config-State#how-to-get-a-specific-permission-from-the-store

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hikalkan picture hikalkan  路  3Comments

wocar picture wocar  路  3Comments

mehdihadeli picture mehdihadeli  路  3Comments

hikalkan picture hikalkan  路  3Comments

ugurozturk picture ugurozturk  路  3Comments