Angular 2 or Angular 4 implementation of angular-permission
Install through npm:
npm install --save ngx-permission
Then include in your apps module:
import { NgModule } from '@angular/core';
import { NgxPermissionModule } from 'ngx-permission';
@NgModule({
imports: [
NgxPermissionModule
]
})
export class MyModule {}
Define role and role validation function by RoleStoreService
export class AppComponent {
constructor(roleStoreService:RoleStoreService) {
const adminRole: Role = {
name: 'admin',
validationFunction: () => false
};
const userRole: Role = {
name: 'user',
validationFunction: () => true //boolean or Promise<boolean>
};
roleStoreService.defineRole(adminRole);
roleStoreService.defineRole(userRole);
// or roleStoreService.defineManyRoles([adminRole, userRole])
}
}
Now you can use onlyForRoles and exceptRoles directives in your components:
import { Component } from '@angular/core';
@Component({
template: `<div *onlyForRoles="['user']">user can see this</div>
<div *onlyForRoles="['admin']">user can't see this</div>`
})
export class MyComponent {}
set canActivate property RouterConnector class
{
path: 'about',
component: AboutComponent,
data: {
ngxPermissions: {
only: ['user']
}
},
canActivate: [RouterConnector]
},
{
path: 'secret',
component: SectetDataComponent,
data: {
ngxPermissions: {
exept: ['user'],
redirectTo: 'about'
}
},
canActivate: [RouterConnector]
}
<script src="node_modules/ngx-permission/bundles/ngx-permission.umd.js"></script>
<script>
// everything is exported ngxPermission namespace
</script>
All documentation is auto-generated from the source via compodoc and can be viewed here: https://TekVanDo.github.io/ngx-permission/docs/
npm install
while current directory is this repoRun npm start
to start a development server on port 8000 with auto reload + tests.
Run npm test
to run tests once or npm run test:watch
to continually run tests.
npm run release
MIT