我需要设置一个条件,当用户没有权限(它没有登录)时可以看到,但我需要首先检查该权限,所以当用户登录时,我不会看到内容。
我使用的是JHipster项目中的"jhiHasAnyAuthority"指令。
<div *jhiHasAnyAuthority="''">This should appear.... when the VISITOR is NOT logged </div>
这在有权威机构的情况下有效(如果你想知道的话):
<div *jhiHasAnyAuthority="['ROLE_ADMIN', 'ROLE_USER']">This should appear.... when ADMIN or USER is logged </div>
<div *jhiHasAnyAuthority="'ROLE_ADMIN'">This should appear.... when ADMIN is logged </div>
所以我尝试了不同的替代方案,比如:
<div *jhiHasAnyAuthority="''">This should appear.... when Blank is NOT logged </div>
但它们不起作用!
注意:我不需要把文本放在一个普通的DIV中,这样如果用户没有登录,它就会出现,因为当它被登录时,它也会出现。
感谢
您正在使用的*jhiHasAnyAuthority指令是JHipseter项目的一部分。
它的源代码可以在https://github.com/jhipster/jhipster-sample-app-ng2/blob/master/src/main/webapp/app/shared/auth/has-any-authority.directive.ts
如果你查看源代码,你会发现它不支持阴性测试,我认为检查"HasAnyAuthority"是否有空或null的权限字符串与检查用户是否没有权限是不一样的。
因此,您似乎无法使用此指令来测试是否存在权威,该指令似乎也不支持像*ngIf这样的if/else语法。
我认为,做你想做的事情的唯一方法是写一个基于*jhiHasAnyAuthority的自定义指令,或者修改*jhiHasAnyAuthority以添加所需的行为。
在任何一种情况下,如果你能让它正常工作,那么向JHipster项目提交一个pull请求就很好了。
- 在
appsharedauth
中创建文件has-not-authority.directive.ts
:
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';
import { AccountService } from 'app/core/auth/account.service';
/**
* @whatItDoes Conditionally includes an HTML element if current user has not any
* of the authorities passed as the `expression`.
*
* @howToUse
* ```
* <some-element *jhiHasNotAuthority="'ROLE_ADMIN'">...</some-element>
*
* <some-element *jhiHasNotAuthority="['ROLE_ADMIN', 'ROLE_USER']">...</some-element>
* ```
*/
@Directive({
selector: '[jhiHasNotAuthority]'
})
export class HasNotAuthorityDirective {
private authorities: string[];
constructor(private accountService: AccountService, private templateRef: TemplateRef<any>, private viewContainerRef: ViewContainerRef) {}
@Input()
set jhiHasNotAuthority(value: string | string[]) {
this.authorities = typeof value === 'string' ? [value] : value;
this.updateView();
// Get notified each time authentication state changes.
this.accountService.getAuthenticationState().subscribe(() => this.updateView());
}
private updateView(): void {
const hasAnyAuthority = this.accountService.hasAnyAuthority(this.authorities);
this.viewContainerRef.clear();
if (!hasAnyAuthority) {
this.viewContainerRef.createEmbeddedView(this.templateRef);
}
}
}
- 更新
appshared
中的shared.module.ts
文件:
import { NgModule } from '@angular/core';
import { YourAppSharedLibsModule } from './shared-libs.module';
import { FindLanguageFromKeyPipe } from './language/find-language-from-key.pipe';
import { JhiAlertComponent } from './alert/alert.component';
import { JhiAlertErrorComponent } from './alert/alert-error.component';
import { JhiLoginModalComponent } from './login/login.component';
import { HasAnyAuthorityDirective } from './auth/has-any-authority.directive';
import { HasNotAuthorityDirective } from './auth/has-not-authority.directive';
@NgModule({
imports: [YourAppSharedLibsModule],
declarations: [FindLanguageFromKeyPipe, JhiAlertComponent, JhiAlertErrorComponent, JhiLoginModalComponent, HasAnyAuthorityDirective, HasNotAuthorityDirective],
entryComponents: [JhiLoginModalComponent],
exports: [
YourAppSharedLibsModule,
FindLanguageFromKeyPipe,
JhiAlertComponent,
JhiAlertErrorComponent,
JhiLoginModalComponent,
HasAnyAuthorityDirective,
HasNotAuthorityDirective
]
})
export class YourAppSharedModule {}
通过这种方式,我们有效地复制了HasAnyAuthorityDirective
的内容,但翻转了createEmbeddedView
方法调用的条件。
您本可以尝试这种方式!
<div *jhiHasAnyAuthority="[]">This should appear... for No_Authority! </div>
附言:我还没有测试过。