Skip to content

Commit

Permalink
feat(acl): support function of guard (#1365)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk authored Oct 27, 2021
1 parent 68b5c5b commit bf2ff5d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 12 deletions.
16 changes: 13 additions & 3 deletions packages/acl/docs/guard.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,30 @@ const routes: Routes = [
{
path: 'auth',
canActivate: [ ACLGuard ],
data: { guard: 'user1' }
data: { guard: 'user1' as ACLGuardType }
},
{
path: 'auth',
canActivate: [ ACLGuard ],
data: {
guard: <ACLType>{
guard: {
role: [ 'user1' ],
ability: [ 10, 'USER-EDIT' ],
mode: 'allOf'
},
} as ACLGuardType,
guard_url: '/no-permisseion'
}
},
{
path: 'obs',
canActivate: [ ACLGuard ],
data: {
guard: ((_srv, _injector) => {
return of('user');
}) as ACLGuardFunctionType,
guard_url: '/no-permisseion'
}
}
]
```

Expand Down
16 changes: 13 additions & 3 deletions packages/acl/docs/guard.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,30 @@ const routes: Routes = [
{
path: 'auth',
canActivate: [ ACLGuard ],
data: { guard: 'user1' }
data: { guard: 'user1' as ACLGuardType }
},
{
path: 'auth',
canActivate: [ ACLGuard ],
data: {
guard: <ACLType>{
guard: {
role: [ 'user1' ],
ability: [ 10, 'USER-EDIT' ],
mode: 'allOf'
},
} as ACLGuardType,
guard_url: '/no-permisseion'
}
},
{
path: 'obs',
canActivate: [ ACLGuard ],
data: {
guard: ((_srv, _injector) => {
return of('user');
}) as ACLGuardFunctionType,
guard_url: '/no-permisseion'
}
}
]
```

Expand Down
22 changes: 20 additions & 2 deletions packages/acl/src/acl-guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { of } from 'rxjs';
import { ACLGuard } from './acl-guard';
import { DelonACLModule } from './acl.module';
import { ACLService } from './acl.service';
import { ACLType } from './acl.type';
import { ACLGuardFunctionType, ACLGuardType, ACLType } from './acl.type';

describe('acl: guard', () => {
let srv: ACLGuard;
Expand Down Expand Up @@ -66,12 +66,30 @@ describe('acl: guard', () => {
});
});

it(`should load route via function`, (done: () => void) => {
srv
.canActivate(
{
data: {
guard: ((_srv, _injector) => {
return of('user');
}) as ACLGuardFunctionType
}
} as any,
null
)
.subscribe(res => {
expect(res).toBeTruthy();
done();
});
});

it(`should load route via Observable`, (done: () => void) => {
srv
.canActivate(
{
data: {
guard: of('user')
guard: of('user') as ACLGuardType
}
} as any,
null
Expand Down
9 changes: 5 additions & 4 deletions packages/acl/src/acl-guard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@angular/core';
import { Injectable, Injector } from '@angular/core';
import {
ActivatedRouteSnapshot,
CanActivate,
Expand All @@ -13,7 +13,7 @@ import { Observable, of } from 'rxjs';
import { map, tap } from 'rxjs/operators';

import { ACLService } from './acl.service';
import { ACLCanType } from './acl.type';
import { ACLCanType, ACLGuardType } from './acl.type';

/**
* Routing guard prevent unauthorized users visit the page, [ACL Document](https://ng-alain.com/acl).
Expand All @@ -28,15 +28,16 @@ import { ACLCanType } from './acl.type';
*/
@Injectable({ providedIn: 'root' })
export class ACLGuard implements CanActivate, CanActivateChild, CanLoad {
constructor(private srv: ACLService, private router: Router) {}
constructor(private srv: ACLService, private router: Router, private injector: Injector) {}

private process(data: Data): Observable<boolean> {
data = {
guard: null,
guard_url: this.srv.guard_url,
...data
};
const guard: ACLCanType | Observable<ACLCanType> = data.guard;
let guard: ACLGuardType = data.guard;
if (typeof guard === 'function') guard = guard(this.srv, this.injector);
return (guard && guard instanceof Observable ? guard : of(guard != null ? (guard as ACLCanType) : null)).pipe(
map(v => this.srv.can(v)),
tap(v => {
Expand Down
8 changes: 8 additions & 0 deletions packages/acl/src/acl.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
* TODO: 尝试增加 `@delon/core` 类库用于处理这种通用型
*/

import { Injector } from '@angular/core';
import { Observable } from 'rxjs';

import type { NzSafeAny } from 'ng-zorro-antd/core/types';

import type { ACLService } from './acl.service';

export interface ACLType {
/**
* 角色
Expand All @@ -32,3 +37,6 @@ export interface ACLType {
}

export type ACLCanType = number | number[] | string | string[] | ACLType;

export type ACLGuardFunctionType = (srv: ACLService, injector: Injector) => Observable<ACLCanType>;
export type ACLGuardType = ACLCanType | Observable<ACLCanType> | ACLGuardFunctionType;

0 comments on commit bf2ff5d

Please sign in to comment.