principal.service.ts 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { Injectable } from '@angular/core';
  2. import { JhiLanguageService } from 'ng-jhipster';
  3. import { SessionStorageService } from 'ngx-webstorage';
  4. import { Observable, Subject } from 'rxjs';
  5. import { AccountService } from './account.service';
  6. @Injectable({ providedIn: 'root' })
  7. export class Principal {
  8. private userIdentity: any;
  9. private authenticated = false;
  10. private authenticationState = new Subject<any>();
  11. constructor(
  12. private languageService: JhiLanguageService,
  13. private sessionStorage: SessionStorageService,
  14. private account: AccountService
  15. ) {}
  16. authenticate(identity) {
  17. this.userIdentity = identity;
  18. this.authenticated = identity !== null;
  19. this.authenticationState.next(this.userIdentity);
  20. }
  21. hasAnyAuthority(authorities: string[]): Promise<boolean> {
  22. return Promise.resolve(this.hasAnyAuthorityDirect(authorities));
  23. }
  24. hasAnyAuthorityDirect(authorities: string[]): boolean {
  25. if (!this.authenticated || !this.userIdentity || !this.userIdentity.authorities) {
  26. return false;
  27. }
  28. for (let i = 0; i < authorities.length; i++) {
  29. if (this.userIdentity.authorities.includes(authorities[i])) {
  30. return true;
  31. }
  32. }
  33. return false;
  34. }
  35. hasAuthority(authority: string): Promise<boolean> {
  36. if (!this.authenticated) {
  37. return Promise.resolve(false);
  38. }
  39. return this.identity().then(
  40. id => {
  41. return Promise.resolve(id.authorities && id.authorities.includes(authority));
  42. },
  43. () => {
  44. return Promise.resolve(false);
  45. }
  46. );
  47. }
  48. identity(force?: boolean): Promise<any> {
  49. if (force === true) {
  50. this.userIdentity = undefined;
  51. }
  52. // check and see if we have retrieved the userIdentity data from the server.
  53. // if we have, reuse it by immediately resolving
  54. if (this.userIdentity) {
  55. return Promise.resolve(this.userIdentity);
  56. }
  57. // retrieve the userIdentity data from the server, update the identity object, and then resolve.
  58. return this.account
  59. .get()
  60. .toPromise()
  61. .then(response => {
  62. const account = response.body;
  63. if (account) {
  64. this.userIdentity = account;
  65. this.authenticated = true;
  66. // After retrieve the account info, the language will be changed to
  67. // the user's preferred language configured in the account setting
  68. const langKey = this.sessionStorage.retrieve('locale') || this.userIdentity.langKey;
  69. this.languageService.changeLanguage(langKey);
  70. } else {
  71. this.userIdentity = null;
  72. this.authenticated = false;
  73. }
  74. this.authenticationState.next(this.userIdentity);
  75. return this.userIdentity;
  76. })
  77. .catch(err => {
  78. this.userIdentity = null;
  79. this.authenticated = false;
  80. this.authenticationState.next(this.userIdentity);
  81. return null;
  82. });
  83. }
  84. isAuthenticated(): boolean {
  85. return this.authenticated;
  86. }
  87. isIdentityResolved(): boolean {
  88. return this.userIdentity !== undefined;
  89. }
  90. getAuthenticationState(): Observable<any> {
  91. return this.authenticationState.asObservable();
  92. }
  93. getImageUrl(): string {
  94. return this.isIdentityResolved() ? this.userIdentity.imageUrl : null;
  95. }
  96. }