项目作者: topaxi

项目描述 :
Access Control for Angular
高级语言: TypeScript
项目地址: git://github.com/topaxi/ng-ability.git
创建时间: 2018-11-05T21:05:18Z
项目社区:https://github.com/topaxi/ng-ability

开源协议:

下载


NgAbilityApp

Define access control lists in Angular.

Installation

  1. $ npm install --save ng-ability
  2. # or
  3. $ yarn add ng-ability

Usage

Define ability context, for example using the current user:

  1. import { Injectable } from '@angular/core';
  2. import { AbilityContext } from 'ng-ability';
  3. @Injectable({ providedIn: 'root' })
  4. export class AbilityUserContext {
  5. constructor(private readonly auth: AuthService) {}
  6. getAbilityContext(): User | null {
  7. return this.auth.getCurrentUser();
  8. }
  9. }

Define abilities for pages, models and other data:

  1. import { AbilityFor, Ability } from 'ng-ability';
  2. // Define ability for Article instance objects, the string 'Article'
  3. // and graphql like objects using a matching function
  4. @AbilityFor(Article, 'Article', article => article.__typename === 'Article')
  5. export class ArticleAbility implements Ability<User, Article> {
  6. can(currentUser: User | null, action: string, article: Article) {
  7. if (currentUser != null && currentUser.admin) {
  8. // Admins can do anything
  9. return true;
  10. }
  11. switch (action) {
  12. case 'view': // Everyone can view articles
  13. return true;
  14. case 'create': // Every user can create new articles
  15. return currentUser != null;
  16. case 'edit': // Users can only edit their own articles
  17. return currentUser != null && currentUser.id === article.authorId;
  18. default:
  19. return false;
  20. }
  21. }
  22. }
  23. @AbilityFor('AdminArea')
  24. export class AdminAreaAbility implements Ability<User> {
  25. can(currentUser: User | null, action: string) {
  26. switch (action) {
  27. case 'view': // Only admins can view the admin area
  28. return currentUser != null && currentUser.admin;
  29. default:
  30. return false;
  31. }
  32. }
  33. }

Import the NgAbilityModule into your application:

  1. import { NgModule } from '@angular/core';
  2. import { NgAbilityModule } from 'ng-ability';
  3. import { AbilityUserContext } from './ability-user-context';
  4. import { ArticleAbility } from './abilities/article.ability';
  5. import { AdminAreaAbility } from './abilities/admin-area.ability';
  6. @NgModule({
  7. imports: [
  8. NgAbilityModule.withAbilities(AbilityUserContext, [
  9. ArticleAbility,
  10. AdminAreaAbility
  11. ])
  12. ]
  13. })
  14. export class AppModule {}

Check for abilities in your application and template code:

  1. import { Component } from '@angular/core';
  2. import { NgAbilityService } from 'ng-ability';
  3. @Component({
  4. template: `
  5. <div *can="['create', 'Article']">
  6. I can create new articles!
  7. </div>
  8. <div *can="['edit', latestArticle]; else noteditable">
  9. <button (click)="editArticle(latestArticle)">Edit latest article</button>
  10. </div>
  11. <ng-template #noteditable>
  12. <div>Latest article is not editable :(</div>
  13. </ng-template>
  14. `
  15. })
  16. export class AppComponent {
  17. get latestArticle(): Article {
  18. return this.articleService.getLatestArticle();
  19. }
  20. constructor(
  21. private readonly ability: NgAbilityService,
  22. private readonly articleService: ArticleService
  23. ) {}
  24. editArticle(article: Article) {
  25. if (this.ability.can('edit', article)) {
  26. // edit article...
  27. }
  28. }
  29. }

Development server

Run ng serve for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.

Code scaffolding

Run ng generate component component-name to generate a new component. You can also use ng generate directive|pipe|service|class|guard|interface|enum|module.

Build

Run ng build to build the project. The build artifacts will be stored in the dist/ directory. Use the --prod flag for a production build.

Running unit tests

Run ng test to execute the unit tests via Karma.

Running end-to-end tests

Run ng e2e to execute the end-to-end tests via Protractor.

Further help

To get more help on the Angular CLI use ng help or go check out the Angular CLI README.