sec:authorize="hasRole('ROLE_ADMIN')" in angular

1.2k Views Asked by At

I am implementing Role based spring security

In this example they have used thymeleaf for frontend purpose, but I am using angular9 with html.

they are using sec:authorize="hasRole('ROLE_ADMIN')" to provide access to the admin,in the same way if I want to provide the same thing in html, for that I have used the following code,

    <li *ngFor="let user of users">
        {{user.username}} ({{user.firstName}} {{user.lastName}})
        - <a sec:authorize="hasRole('ROLE_ADMIN')" (click)="deleteUser(user.userid)" class="text-danger">Delete</a>
    </li>

The person logged in is Role_user, eventhough the delete link is visible to the user. How can I restrict.

Thanks in advance.

1

There are 1 best solutions below

0
Sahil Ralkar On

use *ngIf directive to show and hide the anchor tag

<li *ngFor="let user of users">
        {{user.username}} ({{user.firstName}} {{user.lastName}})
        - <a *ngIf="hasRole('ROLE_ADMIN')" (click)="deleteUser(user.userid)" class="text-danger">Delete</a>
    </li>

but the hasRole() must return boolean. because *ngIf accepts the boolean