Broker System for Supercredit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
5.2KB

  1. import {EventEmitter, Injectable} from '@angular/core';
  2. import {HttpClient} from '@angular/common/http';
  3. import {ApiV1LoginResponse} from '../models/api-v1-login-response';
  4. import {Router} from '@angular/router';
  5. import {PeopleModel} from '../models/people.model';
  6. import {UserExtraModel} from '../models/user-extra.model';
  7. import {Observable} from 'rxjs';
  8. import {AppConfig} from '../app.config';
  9. @Injectable()
  10. export class AuthService {
  11. public apiUrl = '';
  12. public apiWsUrl = '';
  13. public loggedIn = ApiV1LoginResponse.EmptyNew();
  14. loginSuccess = new EventEmitter <ApiV1LoginResponse>();
  15. constructor( private http: HttpClient ,
  16. private router: Router,
  17. private config: AppConfig) {
  18. this.apiUrl = config.apiUrl;
  19. this.apiWsUrl = config.apiWsUrl;
  20. }
  21. public AutoLogin(): void {
  22. const sfm: ApiV1LoginResponse = JSON.parse(localStorage.getItem('sfm'));
  23. if (!sfm) {
  24. console.log('no auto login');
  25. return;
  26. }
  27. this.loggedIn = new ApiV1LoginResponse(
  28. sfm.login,
  29. sfm.machineId,
  30. sfm.session,
  31. sfm.sessionExpire,
  32. sfm.role,
  33. new PeopleModel(
  34. sfm.User.Id, sfm.User.First, sfm.User.Last, sfm.User.Middle,
  35. sfm.User.Title, sfm.User.Display, sfm.User.Nick, sfm.User.Enabled)
  36. );
  37. if ( sfm.UserExtra !== undefined ) {
  38. this.loggedIn.UserExtra = new UserExtraModel(sfm.UserExtra);
  39. }
  40. this.loginSuccess.emit(this.loggedIn);
  41. // console.log ( 'auto login emit events', this.loggedIn);
  42. }
  43. isAuthenticated(): boolean {
  44. return this.loggedIn.login;
  45. }
  46. allowEditLoan(): boolean{
  47. return true;
  48. }
  49. saveSessionInfo(): void {
  50. localStorage.setItem('sfm', JSON.stringify(this.loggedIn));
  51. localStorage.setItem('mid', this.loggedIn.machineId);
  52. }
  53. login(email: string, password: string): void {
  54. this.http.post<ApiV1LoginResponse>(this.apiUrl + 'login', {u: email, p: password}).subscribe(
  55. responseData => {
  56. this.loggedIn.session = responseData['Biukop-Session'];
  57. this.loggedIn.login = responseData.login;
  58. this.loggedIn.machineId = responseData['Biukop-Mid'];
  59. this.loggedIn.sessionExpire = responseData.sessionExpire;
  60. this.loggedIn.role = responseData.role;
  61. if ( responseData.User !== undefined) {
  62. this.loggedIn.User = new PeopleModel(
  63. responseData.User.Id,
  64. responseData.User.First,
  65. responseData.User.Last,
  66. responseData.User.Middle,
  67. responseData.User.Title,
  68. responseData.User.Display,
  69. responseData.User.Nick,
  70. responseData.User.Enabled,
  71. );
  72. if (responseData.UserExtra !== undefined ) {
  73. this.loggedIn.UserExtra = new UserExtraModel(responseData.UserExtra);
  74. }else{
  75. this.loggedIn.UserExtra = UserExtraModel.EmptyNew();
  76. }
  77. }else{
  78. this.loggedIn.User = PeopleModel.EmptyNew();
  79. }
  80. this.saveSessionInfo();
  81. this.loginSuccess.emit(responseData);
  82. },
  83. error => {
  84. this.loggedIn = ApiV1LoginResponse.EmptyNew();
  85. console.log('login error', error);
  86. this.loginSuccess.emit(this.loggedIn);
  87. }
  88. );
  89. }
  90. logout(): void {
  91. this.loggedIn = ApiV1LoginResponse.EmptyNew();
  92. localStorage.removeItem('sfm');
  93. this.loginSuccess.emit(this.loggedIn);
  94. this.router.navigate(['/login']).then(r => {
  95. console.log('prepare to log back in');
  96. } );
  97. this.http.post(`${this.apiUrl}logout`, '').subscribe(
  98. resp => {
  99. console.log('successfully logout from server');
  100. },
  101. event => {
  102. console.log('error logout from server', event);
  103. }
  104. );
  105. }
  106. public getUrl(key: string): string{
  107. return this.config.getUrl(key);
  108. // const s = this.apiUrl + key;
  109. // const kvPair: {key: string, value: string}[] = [
  110. // {key: 'login', value: this.apiUrl + 'login'},
  111. // {key: 'logout', value: this.apiUrl + 'logout'}
  112. // ];
  113. //
  114. // kvPair.forEach( item => {
  115. // if ( item.key === key) {
  116. // return item.value;
  117. // }
  118. // });
  119. //
  120. // // not found if arrive here
  121. // return s;
  122. }
  123. public UpdatePeopleInfo(people: PeopleModel): void{
  124. this.loggedIn.User.Display = people.Display;
  125. this.loggedIn.User.First = people.First;
  126. this.loggedIn.User.Last = people.Last;
  127. this.saveSessionInfo();
  128. }
  129. public LoginAvailable(v: string): Observable<boolean> {
  130. return this.http.get<boolean>(this.getUrl('login-available/' + v));
  131. }
  132. public isAdmin(): boolean {
  133. return this.loggedIn.role === 'admin';
  134. }
  135. public isBroker(): boolean {
  136. return this.loggedIn.role === 'broker';
  137. }
  138. public isUser(): boolean {
  139. return this.loggedIn.login === true;
  140. }
  141. public isCurrentUser(id: string): boolean {
  142. return this.loggedIn.login === true && this.loggedIn.User !== undefined && this.loggedIn.User.Id === id;
  143. }
  144. }