2020-08-06 14:31:07 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2021-02-25 10:25:46 +01:00
|
|
|
import { StorageService } from './storage.service';
|
2022-02-08 17:44:15 +01:00
|
|
|
import { HttpClient, HttpHeaders, HttpEventType } from '@angular/common/http';
|
2021-08-27 15:21:15 +01:00
|
|
|
import { LoginUserRespose, UserForm, UserSession } from '../models/user.model';
|
2020-08-21 00:22:51 +01:00
|
|
|
import { environment } from 'src/environments/environment';
|
2021-08-27 15:21:15 +01:00
|
|
|
import { BehaviorSubject } from 'rxjs';
|
2020-10-30 15:22:35 +01:00
|
|
|
import { AuthConnstants } from '../config/auth-constants';
|
2021-06-04 11:37:56 +01:00
|
|
|
import { AlertController } from '@ionic/angular';
|
2021-08-27 13:39:52 +01:00
|
|
|
import { SessionStore } from '../store/session.service';
|
2021-08-26 16:32:59 +01:00
|
|
|
import { AESEncrypt } from '../services/aesencrypt.service';
|
2022-09-30 15:13:36 +01:00
|
|
|
import { RochetChatConnectorService } from 'src/app/services/chat/rochet-chat-connector.service';
|
2022-02-02 11:57:11 +01:00
|
|
|
import { Router } from '@angular/router';
|
2022-10-04 16:04:23 +01:00
|
|
|
import { NfService } from 'src/app/services/chat/nf.service';
|
2022-02-07 17:55:00 +01:00
|
|
|
import { MessageService } from 'src/app/services/chat/message.service';
|
|
|
|
|
import { ProcessesService } from 'src/app/services/processes.service';
|
|
|
|
|
import { AttachmentsService } from 'src/app/services/attachments.service';
|
|
|
|
|
import { RoomService } from './chat/room.service';
|
|
|
|
|
import { Storage } from '@ionic/storage';
|
2022-03-28 13:34:01 +01:00
|
|
|
import { InitialsService } from './functions/initials.service';
|
2022-04-05 17:10:23 +01:00
|
|
|
import { PermissionService } from './permission.service';
|
2022-09-30 15:13:36 +01:00
|
|
|
import { ChatSystemService } from 'src/app/services/chat/chat-system.service';
|
2020-08-06 14:31:07 +01:00
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class AuthService {
|
2020-10-30 15:22:35 +01:00
|
|
|
userData$ = new BehaviorSubject<any>('');
|
2021-01-19 10:44:55 +01:00
|
|
|
userId$ = new BehaviorSubject<any>('');
|
|
|
|
|
headers: HttpHeaders;
|
2022-01-12 09:29:48 +01:00
|
|
|
public wsValidatedUserChat:any;
|
|
|
|
|
public isWsAuthenticated: boolean = false;
|
2021-01-19 10:44:55 +01:00
|
|
|
opts:any;
|
2021-05-10 14:51:51 +01:00
|
|
|
|
2020-08-06 14:31:07 +01:00
|
|
|
constructor(
|
2020-10-30 15:22:35 +01:00
|
|
|
private http: HttpClient,
|
2021-02-25 10:25:46 +01:00
|
|
|
private storageService:StorageService,
|
2021-06-04 11:37:56 +01:00
|
|
|
public alertController: AlertController,
|
2021-08-26 16:32:59 +01:00
|
|
|
private aesencrypt: AESEncrypt,
|
2022-09-30 15:13:36 +01:00
|
|
|
private RochetChatConnectorService: RochetChatConnectorService,
|
2022-02-07 17:55:00 +01:00
|
|
|
private router: Router,
|
|
|
|
|
private NfService:NfService,
|
|
|
|
|
private processesService: ProcessesService,
|
|
|
|
|
private AttachmentsService: AttachmentsService,
|
2022-03-28 13:34:01 +01:00
|
|
|
private storage: Storage,
|
2022-04-05 17:10:23 +01:00
|
|
|
private initialsService: InitialsService,
|
2022-07-21 18:05:29 +01:00
|
|
|
public p: PermissionService,
|
2022-09-30 15:13:36 +01:00
|
|
|
public ChatSystemService: ChatSystemService, ) {
|
2021-05-10 15:31:16 +01:00
|
|
|
|
2021-01-19 10:44:55 +01:00
|
|
|
this.headers = new HttpHeaders();
|
2020-08-06 14:31:07 +01:00
|
|
|
|
2021-08-27 13:39:52 +01:00
|
|
|
if (SessionStore.exist) {
|
2022-12-19 17:04:21 +01:00
|
|
|
|
|
|
|
|
SessionStore.setInativity(true)
|
2022-01-10 18:52:21 +01:00
|
|
|
|
2022-05-09 15:01:23 +01:00
|
|
|
if(this.p.userPermission(this.p.permissionList.Chat.access) == true ) {
|
2022-04-05 17:10:23 +01:00
|
|
|
this.loginToChatWs()
|
|
|
|
|
}
|
2022-12-19 17:04:21 +01:00
|
|
|
|
2021-05-10 14:51:51 +01:00
|
|
|
}
|
2022-05-09 15:01:23 +01:00
|
|
|
|
2021-02-18 12:44:35 +01:00
|
|
|
|
|
|
|
|
}
|
2021-07-22 17:07:04 +01:00
|
|
|
|
2021-09-02 12:17:14 +01:00
|
|
|
async login(user: UserForm, {saveSession = true}): Promise<LoginUserRespose> {
|
2021-08-27 10:13:35 +01:00
|
|
|
user.BasicAuthKey = 'Basic ' + btoa(user.username + ':' + this.aesencrypt.encrypt(user.password,user.username ));
|
2021-02-18 12:44:35 +01:00
|
|
|
|
2021-09-28 11:31:10 +01:00
|
|
|
this.headers = this.headers.set('Authorization', user.BasicAuthKey);
|
2021-01-19 10:44:55 +01:00
|
|
|
this.opts = {
|
|
|
|
|
headers: this.headers,
|
|
|
|
|
}
|
2021-07-22 17:07:04 +01:00
|
|
|
|
2021-01-19 10:44:55 +01:00
|
|
|
let response: any;
|
2020-08-21 00:22:51 +01:00
|
|
|
|
2021-06-09 14:00:14 +01:00
|
|
|
try {
|
2021-08-27 15:21:15 +01:00
|
|
|
response = await this.http.post<LoginUserRespose>(environment.apiURL + "UserAuthentication/Login", '', this.opts).toPromise();
|
2022-04-28 09:32:27 +01:00
|
|
|
|
2022-03-28 13:34:01 +01:00
|
|
|
|
2021-08-27 15:21:15 +01:00
|
|
|
if(saveSession) {
|
|
|
|
|
this.SetSession(response, user)
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2021-03-31 16:32:33 +01:00
|
|
|
|
2021-08-27 15:21:15 +01:00
|
|
|
} finally {
|
|
|
|
|
return response
|
|
|
|
|
}
|
2021-05-10 15:45:09 +01:00
|
|
|
|
2021-08-27 15:21:15 +01:00
|
|
|
}
|
2021-03-31 16:32:33 +01:00
|
|
|
|
2021-08-27 15:21:15 +01:00
|
|
|
SetSession(response: LoginUserRespose, user:UserForm) {
|
2021-10-05 10:18:38 +01:00
|
|
|
const session: UserSession = Object.assign(SessionStore.user, response)
|
2021-05-10 14:51:51 +01:00
|
|
|
|
2021-08-27 15:21:15 +01:00
|
|
|
if (response) {
|
|
|
|
|
if( session.RoleID == 100000014) {
|
|
|
|
|
session.Profile = 'PR'
|
|
|
|
|
} else if(session.RoleID == 100000011) {
|
|
|
|
|
session.Profile = 'MDGPR'
|
2022-04-02 09:40:09 +01:00
|
|
|
} else if(session.RoleID == 99999872) {
|
|
|
|
|
session.Profile = 'Consultant'
|
2022-12-13 17:21:48 +01:00
|
|
|
} else {
|
|
|
|
|
session.Profile = 'Unknown'
|
2021-06-09 14:00:14 +01:00
|
|
|
}
|
2022-01-10 18:52:21 +01:00
|
|
|
session.Password = user.password
|
|
|
|
|
|
2021-08-27 15:21:15 +01:00
|
|
|
session.BasicAuthKey = user.BasicAuthKey
|
2021-05-10 14:51:51 +01:00
|
|
|
|
2021-08-27 15:21:15 +01:00
|
|
|
SessionStore.reset(session)
|
2022-05-09 15:01:23 +01:00
|
|
|
|
2021-08-27 15:21:15 +01:00
|
|
|
this.storageService.store(AuthConnstants.USER, response);
|
2021-07-22 17:07:04 +01:00
|
|
|
|
2021-08-27 15:21:15 +01:00
|
|
|
return true;
|
2021-05-10 14:51:51 +01:00
|
|
|
}
|
2022-04-07 14:24:07 +01:00
|
|
|
|
|
|
|
|
this.initialsService.getInitials(session.FullName);
|
2020-08-21 00:22:51 +01:00
|
|
|
}
|
2020-08-06 14:31:07 +01:00
|
|
|
|
2022-02-10 18:07:06 +01:00
|
|
|
loginToChatWs() {
|
2022-10-04 16:04:23 +01:00
|
|
|
setTimeout(() => {
|
2022-04-28 09:32:27 +01:00
|
|
|
|
2022-09-30 15:13:36 +01:00
|
|
|
this.RochetChatConnectorService.connect();
|
|
|
|
|
this.RochetChatConnectorService.login().then((message: any) => {
|
2022-04-28 09:32:27 +01:00
|
|
|
|
2022-03-03 22:57:33 +01:00
|
|
|
SessionStore.user.RochetChatUserId = message.result.id
|
|
|
|
|
SessionStore.save()
|
2022-04-06 14:51:35 +01:00
|
|
|
|
2022-09-30 15:13:36 +01:00
|
|
|
this.RochetChatConnectorService.setStatus('online')
|
2022-07-21 18:05:29 +01:00
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2022-09-30 15:13:36 +01:00
|
|
|
this.ChatSystemService.getAllRooms();
|
2022-07-21 18:05:29 +01:00
|
|
|
}, 200);
|
|
|
|
|
|
2022-04-22 16:42:37 +01:00
|
|
|
// alert('wsLogin')
|
2022-03-03 22:57:33 +01:00
|
|
|
|
|
|
|
|
}).catch((message) => {
|
2022-04-22 16:42:37 +01:00
|
|
|
// alert('ws login failed')
|
2022-02-07 17:55:00 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
2022-03-03 22:57:33 +01:00
|
|
|
// before sending a message with a attachment
|
2022-02-08 15:33:44 +01:00
|
|
|
this.NfService.beforeSendAttachment = async (message: MessageService, room?: RoomService) => {
|
|
|
|
|
|
2022-02-07 17:55:00 +01:00
|
|
|
if(message.hasFile) {
|
|
|
|
|
if(message.file.type != 'application/webtrix') {
|
|
|
|
|
const formData = message.temporaryData
|
|
|
|
|
|
2022-02-07 20:18:48 +01:00
|
|
|
try {
|
2022-09-29 11:03:46 +01:00
|
|
|
// alert('upload try')
|
2022-02-07 20:18:48 +01:00
|
|
|
let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise()
|
|
|
|
|
message.file.guid = guid.path
|
2022-03-24 17:40:14 +01:00
|
|
|
|
2022-03-22 16:11:30 +01:00
|
|
|
message.downloadFileMsg()
|
|
|
|
|
|
2022-02-07 20:52:07 +01:00
|
|
|
return true
|
|
|
|
|
} catch(e) {
|
2022-04-28 09:32:27 +01:00
|
|
|
|
2022-02-07 20:52:07 +01:00
|
|
|
return false
|
|
|
|
|
}
|
2022-02-07 20:18:48 +01:00
|
|
|
|
|
|
|
|
} else {
|
2022-02-08 15:33:44 +01:00
|
|
|
try {
|
2022-02-07 20:18:48 +01:00
|
|
|
const res = message.temporaryData
|
2022-05-27 13:36:37 +01:00
|
|
|
|
2022-02-07 20:18:48 +01:00
|
|
|
let url = await this.processesService.GetDocumentUrl(res.data.selected.Id, res.data.selected.ApplicationType).toPromise();
|
2022-05-27 13:36:37 +01:00
|
|
|
|
2022-02-07 20:18:48 +01:00
|
|
|
let url_no_options: string = url.replace("webTRIX.Viewer","webTRIX.Viewer.Branch1");
|
|
|
|
|
message.attachments[0].title_link = url_no_options
|
|
|
|
|
message.attachments[0].message_link = url_no_options
|
2022-05-27 13:36:37 +01:00
|
|
|
|
2022-02-07 20:18:48 +01:00
|
|
|
return true
|
|
|
|
|
|
2022-02-07 20:52:07 +01:00
|
|
|
} catch(e) {
|
2022-05-27 13:36:37 +01:00
|
|
|
|
2022-02-07 20:52:07 +01:00
|
|
|
return false
|
|
|
|
|
}
|
2022-02-07 17:55:00 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
2022-02-08 17:44:15 +01:00
|
|
|
|
2022-02-09 15:27:10 +01:00
|
|
|
this.NfService.downloadFileMsg = async (message: MessageService, room?: RoomService) => {
|
2022-02-08 17:44:15 +01:00
|
|
|
|
2022-04-28 09:32:27 +01:00
|
|
|
//
|
2022-02-08 17:44:15 +01:00
|
|
|
let downloadFile = "";
|
|
|
|
|
if (message.file.type == "application/img") {
|
|
|
|
|
const event: any = await this.AttachmentsService.downloadFile(message.file.guid).toPromise();
|
|
|
|
|
|
|
|
|
|
if (event.type === HttpEventType.DownloadProgress) {
|
|
|
|
|
//this.downloadProgess = Math.round((100 * event.loaded) / event.total);
|
2022-04-28 09:32:27 +01:00
|
|
|
//
|
2022-02-08 17:44:15 +01:00
|
|
|
return true
|
|
|
|
|
} else if (event.type === HttpEventType.Response) {
|
|
|
|
|
downloadFile = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), ''));
|
2022-02-09 15:27:10 +01:00
|
|
|
|
2022-02-08 17:44:15 +01:00
|
|
|
message.file = {
|
|
|
|
|
guid: message.file.guid,
|
|
|
|
|
image_url: downloadFile,
|
|
|
|
|
type: message.file.type
|
|
|
|
|
}
|
2022-02-09 15:27:10 +01:00
|
|
|
|
2022-02-08 17:44:15 +01:00
|
|
|
await this.storage.set(message.file.guid, downloadFile).then(() => {
|
2022-04-28 09:32:27 +01:00
|
|
|
//
|
2022-02-08 17:44:15 +01:00
|
|
|
});
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-03-03 22:57:33 +01:00
|
|
|
|
2022-02-24 12:10:09 +01:00
|
|
|
}, 1)
|
2022-02-07 17:55:00 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-16 15:52:59 +01:00
|
|
|
autologout(expirationDate:number) {
|
2022-10-04 16:04:23 +01:00
|
|
|
setTimeout(() => {
|
2022-02-02 11:57:11 +01:00
|
|
|
this.logout();
|
|
|
|
|
}, expirationDate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logout() {
|
|
|
|
|
|
|
|
|
|
localStorage.removeItem('userChat');
|
|
|
|
|
|
|
|
|
|
SessionStore.setInativity(false)
|
2022-10-04 16:04:23 +01:00
|
|
|
SessionStore.setUrlBeforeInactivity(this.router.url);
|
2022-02-02 11:57:11 +01:00
|
|
|
setTimeout(() => {
|
2022-12-19 18:38:53 +01:00
|
|
|
// alert('logout')
|
2022-02-02 11:57:11 +01:00
|
|
|
this.router.navigateByUrl('/', { replaceUrl: true });
|
|
|
|
|
}, 100)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 16:04:23 +01:00
|
|
|
// Get user data from RocketChat | global object
|
2022-02-16 15:52:59 +01:00
|
|
|
getUserData() {
|
2021-02-25 12:41:29 +01:00
|
|
|
this.storageService.get(AuthConnstants.AUTH).then(res=>{
|
2020-10-30 15:22:35 +01:00
|
|
|
this.userData$.next(res);
|
2021-02-25 12:41:29 +01:00
|
|
|
});
|
2020-12-16 16:06:14 +01:00
|
|
|
}
|
2020-11-24 13:46:13 +01:00
|
|
|
|
2022-02-16 15:52:59 +01:00
|
|
|
logoutChat() {
|
2020-08-19 23:47:11 +01:00
|
|
|
}
|
2022-04-07 14:24:07 +01:00
|
|
|
|
2021-06-04 11:37:56 +01:00
|
|
|
async presentAlert(message: string) {
|
|
|
|
|
const alert = await this.alertController.create({
|
|
|
|
|
cssClass: 'my-custom-class',
|
|
|
|
|
header: 'Mensagem do sistema',
|
|
|
|
|
message: message,
|
|
|
|
|
buttons: ['OK']
|
|
|
|
|
});
|
2021-07-22 17:07:04 +01:00
|
|
|
|
2021-06-04 11:37:56 +01:00
|
|
|
await alert.present();
|
|
|
|
|
}
|
2020-08-06 14:31:07 +01:00
|
|
|
}
|