Advertisement
Guest User

Untitled

a guest
Feb 5th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { UpdateUserRequest } from '../../models/update-user-request.model';
  2. import { UserRepresentation } from '../../models/user-representation.model';
  3. import { Injectable } from '@angular/core';
  4. import { User } from '../../models/user.model';
  5. import { LocalStorage } from 'ngx-webstorage-fix';
  6. import { HttpClient, HttpHeaders } from '@angular/common/http';
  7. import 'rxjs/add/operator/map';
  8. import 'rxjs/add/operator/toPromise';
  9. import * as _ from 'lodash';
  10. import { AuthResponse } from './auth-response';
  11.  
  12. @Injectable()
  13. export class AuthService {
  14.   @LocalStorage() private _user: User;
  15.   private set user(value: User) {
  16.     this._user = value;
  17.     this.fillAuthFlags();
  18.   }
  19.  
  20.   private get user() {
  21.     return this._user;
  22.   }
  23.  
  24.   public isModerator;
  25.   public isInvestor;
  26.   public isInitiator;
  27.   public isGISP;
  28.  
  29.   constructor(private http: HttpClient) {
  30.     this.fillAuthFlags();
  31.   }
  32.  
  33.   private fillAuthFlags() {
  34.     if (!this._user) {
  35.       this.isModerator = false;
  36.       this.isInvestor = false;
  37.       this.isInitiator = false;
  38.       this.isGISP = false;
  39.  
  40.       return;
  41.     }
  42.  
  43.     this.isModerator = this._user.groupTypes.includes('MODERATORS');
  44.     this.isInvestor = this._user.groupTypes.includes('INVESTORS');
  45.     this.isInitiator = this._user.groupTypes.includes('COMPANY_USERS');
  46.     this.isGISP = this._user.groupTypes.includes('GISP_USERS');
  47.   }
  48.  
  49.   public getUser(): Promise<User> {
  50.     return Promise.resolve(this.user);
  51.   }
  52.  
  53.   userHasSubscription(): boolean {
  54.     return this._user.hasSubscription || this.isInitiator || this.isModerator;
  55.   }
  56.  
  57.   public loadCurrentUser(): Promise<User> {
  58.     return this.http
  59.       .post<AuthResponse>('auth/status', {})
  60.       .toPromise()
  61.       .then((user: AuthResponse) => {
  62.         this.user = {
  63.           authenticated: user.authenticated,
  64.           groupTypes: user.groupTypes,
  65.           hasSubscription: user.accessType === 'EXTENDED',
  66.           name: user.name,
  67.           organizationId: user.organizationId,
  68.           userId: user.userId,
  69.           externalAuth: user.externalAuth
  70.         };
  71.         return user;
  72.       })
  73.       .catch(() => {
  74.         this._user = null;
  75.         return null;
  76.       });
  77.   }
  78.  
  79.   public login(usernameOrEmail: string, password: string, rememberMe: boolean) {
  80.     const body = {
  81.       usernameOrEmail,
  82.       password,
  83.       rememberMe
  84.     };
  85.     const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
  86.     const options = { headers: headers };
  87.     return this.http
  88.       .post<AuthResponse>('auth/login', body, options)
  89.       .toPromise()
  90.       .then((data: AuthResponse) => {
  91.         if (data['authenticated']) {
  92.           this.user = {
  93.             authenticated: data['authenticated'],
  94.             name: data['name'],
  95.             userId: +data['userId'],
  96.             organizationId: +data['organizationId'],
  97.             groupTypes: data['groupTypes'],
  98.             hasSubscription: data.accessType === 'EXTENDED',
  99.             externalAuth: data.externalAuth
  100.           };
  101.           return Promise.resolve(this.user);
  102.         }
  103.  
  104.         return Promise.reject('Unauthenticated');
  105.       });
  106.   }
  107.  
  108.   public loggedIn(): Promise<boolean> {
  109.     return this.getUser().then((user: User) => user != null);
  110.   }
  111.  
  112.   public logOut(): Promise<any> | void {
  113.  
  114.     if (this.user.externalAuth) {
  115.       location.href = `auth/logout`;
  116.     } else {
  117.       return this.http
  118.         .post(`auth/logout`, {})
  119.         .toPromise()
  120.         .then(() => {
  121.           this.user = null;
  122.         });
  123.     }
  124.   }
  125.  
  126.   public getCurrentUserInformation(): Promise<UserRepresentation> {
  127.     return this.http.get<UserRepresentation>('auth/user').toPromise();
  128.   }
  129.  
  130.   public updateUserInformation(userInfo: UserRepresentation, userRequest: UpdateUserRequest): Promise<any> {
  131.     const lengthPassword = _.get(userRequest, ['password', 'length']);
  132.     if (!userInfo.isAbleToChangePassword && _.isNumber(lengthPassword) && lengthPassword > 0) {
  133.       return Promise.reject('Нет прав на смену пароля');
  134.     }
  135.  
  136.     return this.http.post('auth/user', userRequest).toPromise();
  137.   }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement