How to create a structural directive that renders elements based on user permissions

0 votes
Can i know How to create a structural directive that renders elements based on user permissions?
3 days ago in Node-js by Nidhi
• 14,600 points
45 views

1 answer to this question.

0 votes

Here's how to create a structural directive that conditionally renders elements based on user permissions:

1. Create the Permission Directive

import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';

import { AuthService } from './auth.service'; // Your authentication service

@Directive({

  selector: '[appHasPermission]'

})

export class HasPermissionDirective {

  private hasView = false;

  constructor(

    private templateRef: TemplateRef<any>,

    private viewContainer: ViewContainerRef,

    private authService: AuthService

  ) {}

  @Input() set appHasPermission(permission: string | string[]) {

    const hasPermission = this.authService.hasPermission(permission);

     if (hasPermission && !this.hasView) {

      this.viewContainer.createEmbeddedView(this.templateRef);

      this.hasView = true;

    } else if (!hasPermission && this.hasView) {

      this.viewContainer.clear();

      this.hasView = false;

    }

  }

}

2. Implement the Auth Service

import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })

export class AuthService {

  private currentUser = {

    permissions: ['read', 'edit'] // Example permissions

  };

  hasPermission(requiredPermission: string | string[]): boolean {

    if (!requiredPermission) return true; 

    const required = Array.isArray(requiredPermission) 

      ? requiredPermission 

      : [requiredPermission];

    return required.every(perm => 

      this.currentUser.permissions.includes(perm)

    );

  }

}

3. Use the Directive in Templates

<!-- Single permission check -->

<div *appHasPermission="'edit'">

  Only visible to users with 'edit' permission

</div>

<!-- Multiple permission check (AND logic) -->

<div *appHasPermission="['read', 'edit']">

  Visible to users with both 'read' AND 'edit' permissions

</div>

answered 2 days ago by anonymous

Related Questions In Node-js

0 votes
0 answers

How to create a responsive navbar that collapses on smaller screens in Bootstrap 3?

With the help of proper code example ...READ MORE

Apr 1 in Node-js by Nidhi
• 14,600 points
37 views
0 votes
0 answers

How to install NodeJS LTS on Windows as a local user (without admin rights)

I'm using Windows as a simple user ...READ MORE

Aug 11, 2022 in Node-js by Neha
• 9,020 points
2,123 views
0 votes
1 answer
0 votes
1 answer

How to handle the swiperight event to trigger custom actions in jQuery Mobile?

To handle the swiperight event and trigger ...READ MORE

answered 2 days ago in Node-js by anonymous
26 views
0 votes
1 answer

How to implement a directive that auto-saves form data periodically?

To create a directive that automatically saves ...READ MORE

answered 2 days ago in Node-js by anonymous
28 views
0 votes
1 answer

How to dynamically inject components into the DOM using @Component?

You need to use ComponentFactoryResolver and ViewContainerRef. ...READ MORE

answered 2 days ago in Node-js by anonymous
37 views
0 votes
1 answer

How to use host bindings to bind properties to the host element in a component?

In Angular (2+), the @HostBinding() decorator allows ...READ MORE

answered 2 days ago in Node-js by anonymous
36 views
0 votes
1 answer
0 votes
1 answer

How to develop a directive that restricts user input based on custom conditions?

To create an Angular directive that restricts ...READ MORE

answered 2 days ago in Node-js by anonymous
29 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP