All Projects → mselerin → ngx-security

mselerin / ngx-security

Licence: MIT license
Security directives for your Angular application to show/hide elements based on a user roles / permissions.

Programming Languages

typescript
32286 projects
HTML
75241 projects
javascript
184084 projects - #8 most used programming language
CSS
56736 projects

Projects that are alternatives of or similar to ngx-security

rbac
Simple RBAC/ACL for Laravel 8 caching and permission groups.
Stars: ✭ 43 (+138.89%)
Mutual labels:  permissions, acl, roles
Think Authz
An authorization library that supports access control models like ACL, RBAC, ABAC in ThinkPHP 6.0 .
Stars: ✭ 155 (+761.11%)
Mutual labels:  permissions, acl, roles
Adonis Acl
demo app: https://github.com/enniel/adonis-acl-blog-demo
Stars: ✭ 195 (+983.33%)
Mutual labels:  permissions, acl, roles
Laratrust
Handle roles and permissions in your Laravel application
Stars: ✭ 1,799 (+9894.44%)
Mutual labels:  permissions, acl, roles
Brandenburg
Laravel Authentication Package
Stars: ✭ 79 (+338.89%)
Mutual labels:  permissions, acl, roles
laravel-acl
Laravel ACL is a simple role, permission ACL for Laravel Framework.
Stars: ✭ 78 (+333.33%)
Mutual labels:  permissions, acl, roles
Bouncer
Eloquent roles and abilities.
Stars: ✭ 2,763 (+15250%)
Mutual labels:  permissions, acl, roles
Ngx Permissions
Permission and roles based access control for your angular(angular 2,4,5,6,7,9+) applications(AOT, lazy modules compatible
Stars: ✭ 749 (+4061.11%)
Mutual labels:  permissions, acl, roles
Accesscontrol
Role and Attribute based Access Control for Node.js
Stars: ✭ 1,723 (+9472.22%)
Mutual labels:  permissions, acl, roles
nova-permissions
Add Permissions based authorization for your Nova installation via User-based Roles and Permissions. Roles are defined in the database whereas Permissions are defined in the code base.
Stars: ✭ 115 (+538.89%)
Mutual labels:  permissions, acl, roles
laminas-permissions-acl
Provides a lightweight and flexible access control list (ACL) implementation for privileges management
Stars: ✭ 29 (+61.11%)
Mutual labels:  permissions, acl
Vue Router User Roles
A Vue.js plugin that protects routes based on user roles. Add your own authentication.
Stars: ✭ 237 (+1216.67%)
Mutual labels:  permissions, roles
Rbac
Hierarchical Role-Based Access Control for Node.js
Stars: ✭ 254 (+1311.11%)
Mutual labels:  permissions, acl
Vue Gates
🔒 A Vue.js & Nuxt.js plugin that allows you to use roles and permissions in your components or DOM elements, also compatible as middleware and methods.
Stars: ✭ 184 (+922.22%)
Mutual labels:  permissions, roles
Lock Laravel
This package is a Laravel 5 driver for Lock
Stars: ✭ 161 (+794.44%)
Mutual labels:  permissions, acl
shyft
⬡ Shyft is a server-side framework for building powerful GraphQL APIs 🚀
Stars: ✭ 56 (+211.11%)
Mutual labels:  permissions, acl
spicedb
Open Source, Google Zanzibar-inspired fine-grained permissions database
Stars: ✭ 3,358 (+18555.56%)
Mutual labels:  permissions, acl
ngx-access
Add access control to your components using hierarchical configuration with logical expressions.
Stars: ✭ 21 (+16.67%)
Mutual labels:  permissions, roles
React-Express-JWT-UserPortal
React.js & Express.js User portal Using Core UI, JWT, JWT Token, Refresh Token, Role & Permission management, User manamgenet, Event Log.
Stars: ✭ 22 (+22.22%)
Mutual labels:  permissions, roles
rbac-tool
Rapid7 | insightCloudSec | Kubernetes RBAC Power Toys - Visualize, Analyze, Generate & Query
Stars: ✭ 546 (+2933.33%)
Mutual labels:  permissions, acl

Ngx-Security

License: MIT npm CI codecov

🔐 Security directives for your Angular application to show/hide elements based on a user roles / permissions.

View changelog

View migration guide

Installation

Install the library with:

npm install ngx-security --save

Then import it in your AppModule:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

// Import your library
import { NgxSecurityModule } from 'ngx-security';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    
    // Importing ngx-security module
    NgxSecurityModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Usage

The security directives use a security state controlled by the NgxSecurityService.
You need to set/change this state to use the directives:

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { NgxSecurityService } from 'ngx-security';

@Component({
  selector: 'app-sample',
  templateUrl: './sample.component.html'
})
export class SampleComponent
{
  constructor(
    private http: HttpClient,
    private security: NgxSecurityService
  ) {}

  login() {
    this.security.setAuthenticationChecker(() => {
      return of(true);
    });
    
    this.security.setPermissionChecker((perm: string) => {
      return this.http.get(`/api/auth/permissions/has/${perm}`).pipe(
        map(() => true)
      );
    });
  }
  
  logout() {
    // Reset the security state to it's initial value
    this.security.reset();
  }
}

Of course, you can change the security state wherever and whenever you want !

You can now use the differents directives and the guard.

Directives

IsAuthenticated

<div *secuIsAuthenticated>I'm authenticated !</div>

IsAnonymous

<div *secuIsAnonymous>I'm an anonymous user (not authenticated)</div>

HasRoles / HasPermissions / IsMemberOf

<div *secuHasRoles="'ADMIN'">I have the role 'ADMIN'</div>
<div *secuHasRoles="['CREATOR', 'EDITOR']; else roleElse">I have the role 'CREATOR' and 'EDITOR'</div>
<ng-template #roleElse>
  <div>I don't have the roles</div>
</ng-template>

HasAnyRoles / HasAnyPermissions / IsMemberOfAny

<div *secuHasAnyRoles="['CREATOR', 'EDITOR']; else roleElse">I have the role 'CREATOR' or 'EDITOR'</div>
<ng-template #roleElse>
  <div>I don't have the roles</div>
</ng-template>

HasNotRoles / HasNotPermissions / IsNotMemberOf

<div *secuHasNotRoles="'POWERUSER'">I don't have the role 'POWERUSER'</div>

Route Guard

The NgxSecurityGuard can prevent an unauthorized user to load / access parts of your application.

import {
  ActivatedRouteSnapshot,
  Route, Routes,
  RouterStateSnapshot
} from '@angular/router';

import { NgxSecurityGuard } from 'ngx-security';

export const ROUTES: Routes = [
  {
    path: 'secured-page',
    canActivate: [ NgxSecurityGuard ],
    data: {
      security: {
        isAuthenticated: true,
        hasAllRoles: ['ADMIN', 'USER'],
        redirectTo: '/access-denied',
        unauthorizedHandler: (route: Route | ActivatedRouteSnapshot, state?: RouterStateSnapshot) => {
          console.warn('No, no, no, you cannot access this !');
        }
      }
    },
    component: SecuredComponent
  }
];

Tips

Log unauthorized access

You can use the unauthorizedHandler to log unauthorized access to route path :

unauthorizedHandler: (route: Route | ActivatedRouteSnapshot, state?: RouterStateSnapshot) => {
  let path = (state ? state.url : null);
    if (!path && route) {
      path = '/' + (route as Route).path;
    }
  
    console.warn('Unauthorized access', path);
}

Contributing

Feel free to introduce a feature request, an issue or a pull request. 👌

Changelog

Changelog is available here.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].