Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
thiag0coelho opened this issue Feb 1, 2020 · 7 comments
Assignees
Milestone

Comments

@thiag0coelho
Copy link
Contributor

thiag0coelho commented Feb 1, 2020

Angular template v2.0.1

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

<div disabled="hasPermission('Permission:PropostaAlterarComissao')">
...
</div>
<div *ngIf="hasPermission('Permission:PropostaAlterarComissao')">

</div>

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

@ismcagdas
Copy link
Member

@thiag0coelho do you mean permissions ?

@hikalkan hikalkan assigned mehmet-erim and unassigned mehmet-erim Feb 1, 2020
@hikalkan
Copy link
Member

hikalkan commented Feb 1, 2020

@hikalkan
Copy link
Member

hikalkan commented Feb 1, 2020

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

@thiag0coelho
Copy link
Contributor Author

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

@hikalkan hikalkan added this to the 2.1 milestone Feb 1, 2020
@mehmet-erim
Copy link
Contributor

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>

@luisp88
Copy link

luisp88 commented Mar 25, 2020

@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

@mehmet-erim
Copy link
Contributor

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants