在檢查ACL訪問之後,需要基於「routerLink」隱藏菜單中的鏈接。我不想用角指令「* ngIf」在應用程序的每個元素鏈接(需要做的globaly在routerConfig定義)Angular2。如何在檢查訪問後隱藏(無呈現)菜單中的鏈接?
內容可以在組件使用註釋@CanActivate CONTROLL但需要隱藏菜單鏈接
我試圖做到這一點與覆蓋「routerLink」指令,但在這個指令後覆蓋不能routerConfig
讓我的擴展PARAM定義(資源的privilages)訪問例子:
@Component({})
@RouteConfig([{
path: '/dashboard',
name: 'Dashboard',
component: DashboardComponent,
data: {'resource':'account', 'privilage':'show'}
}])
,但不能得到在 「routerLink」 上網本配置數據(routerData)。
有些想法該怎麼做?
第二個版本
多級菜單和stiil有來自routerConfig定義可以訪問到(擴展數據配置)問題。
主要成分
@RouteConfig([
{ path:'/dashboard', name: 'DashboardLink', component: DashboardComponent, data: { res: 'dasboard', priv: 'show'}, useAsDefault: true },
{ path:'/user/...', name: 'UserLink', component: UserComponent, data: { res: 'user', priv: 'show'} },
])
@Component({
selector: 'main-app',
template: `
<ul class="left-menu">
<li><a secured [routerLink]="['UserLink']">User</a>
<ul>
<li><a secured [routerLink]="['ProfileLink']">Profile</a></li>
</ul>
</li>
<li><a secured [routerLink]="['HomeLink']">Home</a></li>
<li><a secured [routerLink]="['DashboardLink']">Dashboard</a></li>
</ul>
<router-outlet></router-outlet>
`
})
export class MainComponent {
}
用戶組件
@RouteConfig([
{ path: '/profile', name: 'ProfileLink', component: ProfileComponent, data: {res: 'user', priv: 'details'} },
])
@Component({ ... })
export class UserComponent {
}
輪廓組件
@Component({ ... })
export class ProfileComponent {
}
我的安全指令
@Directive({
selector: '[secured]'
})
export class SecuredDirective {
@Input('routerLink')
routeParams: any[];
/**
*
*/
constructor(private _viewContainer: ViewContainerRef, private _elementRef: ElementRef, private router:Router) {
}
ngAfterViewInit(){
//Get access to the directives element router instructions (with parent instructions)
let instruction = this.router.generate(this.routeParams);
//Find last child element od instruction - I thing thats my component but I am not sure (work good with two levels of menu)
this.getRouterChild(instruction);
}
private getRouterChild(obj: any){
var obj1 = obj;
while(true) {
if(typeof obj1.child !== 'undefined' && obj1.child !== null){
obj1 = obj1.child;
} else {
break;
}
}
this.checkResPrivAcl(obj1.component.routeData.data)
}
private checkResPrivAcl(aclResAndPriv: any){
let hasAccess = CommonAclService.getInstance().hasAccess(aclResAndPriv['res'], aclResAndPriv['priv']);
if (!hasAccess) {
let el : HTMLElement = this._elementRef.nativeElement;
el.parentNode.removeChild(el);
}
console.log("CHECK ACL: " + aclResAndPriv['res'] + " | " + aclResAndPriv['priv']);
}
}
此解決方案僅適用於菜單的子項目不適用於菜單的主級別,並且不確定此功能是否在多級菜單上正常工作。
我嘗試解決這個問題,我試圖讓訪問RouterConfig定義不同勢的方式(和我routerData擴展配置)的指令,並通過使用@input(「routerLink」),但鏈接的別名檢查元素不知道如何才能訪問RouterConfig。
我不明白什麼是'檢查access'? – micronyks
我的意思是「檢查訪問」訪問者是否有權訪問鏈接 - 數據中定義的資源:{'resource':'account','privilage':'show'} –