Skip to content

Add optional context to directives #145

Open
@mfrieling

Description

@mfrieling

#2 I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[x] Feature request
[ ] Documentation issue or request

Current behavior

Currently using the ngxPermissionsOnly and ngxPermissionsExcept we can pass one or more permission or role names as string and basically it checks whether the current user has that/one of them or not. The permission/role itself can provide a validator function which is able to perform simple validations like in the roles example checking the session state.

Expected behavior

What in many cases would be helpful is to validate permissions or roles with a given context. For example within a list of objects where each object has actions like view/edit/delete we not only need to check if user X can view/edit/delete objects of that type but for example if he can view/edit/delete that specific object.In my actual use case the action is editing permissions of contacts. But the contact can either be a "simple" data item or connected with an user account which can login or not. So user X editing (assigning) permissions to contact A only makes sense if user X has the permission "edit contact permissions¨ and contact A is able to login.

This could be solved by providing an optional context to the validator function via the directives, pipes and maybe also the routes (the question is how to define what to pass from routing).

Directives:

<ul>
<li *ngFor="let item of items">
<a href="#" *ngxPermissionsOnly="['changeSomething']" [ngxPermissionsContext]="item">
    Visible if user has permission and item fullfils specific criteria
</a>

Pipes:
(example from #124 adapted)

<button [disabled]="'changeSomething' | ngxPermission(something)">Change</button>

Permission/role validator function:

# Current validator functions:
function(): boolean|Promise<boolean>
function(permissionName: string, permissionObject: object): boolean|Promise<boolean>

# New validator functions:
function(context?: any): boolean|Promise<boolean>
function(context?: any, permissionName: string, permissionObject: object): boolean|Promise<boolean>

# Examples:
this.permissionsService.addPermission('changeSomething', (context) => {
        return typeof context === User && user.canLogin;
    })


this.permissionsService.addPermission('changeSomething', (context, permissionName, permissionsObject) => {
        return !!permissionsObject[permissionName] && typeof context === User && user.canLogin;
    })

Environment


Angular version: 9.1.8
ngx-permissions version: 8.0.0


Browser:
- [x] Chrome (desktop) version 83
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions