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';
|
2020-10-30 15:22:35 +01:00
|
|
|
import { HttpService } from './http.service';
|
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';
|
2021-10-15 15:21:26 +01:00
|
|
|
import { CookieService } from 'ngx-cookie-service';
|
2022-01-12 12:44:51 +01:00
|
|
|
import { WsChatService } from 'src/app/services/chat/ws-chat.service';
|
2022-02-02 11:57:11 +01:00
|
|
|
import { Router } from '@angular/router';
|
2022-02-07 17:55:00 +01:00
|
|
|
import { NfService } from 'src/app/services/chat/nf.service'
|
|
|
|
|
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-02-08 17:44:15 +01:00
|
|
|
|
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;
|
2021-08-27 15:21:15 +01:00
|
|
|
public ValidatedUser: UserSession;
|
2022-01-12 09:29:48 +01:00
|
|
|
public wsValidatedUserChat:any;
|
2022-01-14 09:58:18 +01:00
|
|
|
public ValidatedUserChat:any = {}
|
2022-01-12 09:29:48 +01:00
|
|
|
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,
|
|
|
|
|
private httpService: HttpService,
|
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,
|
2021-10-15 15:21:26 +01:00
|
|
|
private cookieService: CookieService,
|
2022-02-02 11:57:11 +01:00
|
|
|
private WsChatService: WsChatService,
|
2022-02-07 17:55:00 +01:00
|
|
|
private router: Router,
|
|
|
|
|
private NfService:NfService,
|
|
|
|
|
private processesService: ProcessesService,
|
|
|
|
|
private AttachmentsService: AttachmentsService,
|
|
|
|
|
private storage: Storage ) {
|
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) {
|
|
|
|
|
this.ValidatedUser = SessionStore.user
|
2022-01-10 18:52:21 +01:00
|
|
|
|
2022-03-15 15:49:59 +01:00
|
|
|
// console.log('login', SessionStore.user.RochetChatUser, SessionStore.user.Password)
|
2022-02-07 17:55:00 +01:00
|
|
|
this.loginToChatWs()
|
2022-01-10 18:52:21 +01:00
|
|
|
|
2021-05-10 14:51:51 +01:00
|
|
|
}
|
2021-07-18 20:20:30 +01:00
|
|
|
|
2022-02-24 12:10:09 +01:00
|
|
|
if (localStorage.getItem("userChat") != null) {
|
2021-06-04 11:37:56 +01:00
|
|
|
this.ValidatedUserChat = JSON.parse(localStorage.getItem('userChat'));
|
2022-02-24 12:10:09 +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();
|
|
|
|
|
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'
|
2021-06-09 14:00:14 +01:00
|
|
|
}
|
2021-07-22 17:07:04 +01:00
|
|
|
|
2022-01-10 18:52:21 +01:00
|
|
|
session.Password = user.password
|
2022-01-10 22:04:04 +01:00
|
|
|
session.RochetChatUser = user.username.split('@')[0]
|
2022-01-10 18:52:21 +01:00
|
|
|
|
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)
|
|
|
|
|
this.ValidatedUser = SessionStore.user;
|
|
|
|
|
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
|
|
|
}
|
2020-08-21 00:22:51 +01:00
|
|
|
}
|
2020-08-06 14:31:07 +01:00
|
|
|
|
2022-02-08 14:22:32 +01:00
|
|
|
//Login to rocketChat server2
|
|
|
|
|
//user: UserForm
|
2022-02-08 16:37:32 +01:00
|
|
|
async loginChat() {
|
2022-02-08 14:22:32 +01:00
|
|
|
|
2022-02-24 12:10:09 +01:00
|
|
|
const expirationMinutes = 30;
|
2022-02-08 14:22:32 +01:00
|
|
|
let date = new Date().getTime();
|
|
|
|
|
let expirationDate = new Date(new Date().getTime() + expirationMinutes*60*1000);
|
|
|
|
|
|
2021-06-04 11:37:56 +01:00
|
|
|
let postData = {
|
2022-02-08 16:23:30 +01:00
|
|
|
"user": SessionStore.user.RochetChatUser,
|
|
|
|
|
"password": SessionStore.user.Password,
|
2021-06-04 11:37:56 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-17 11:27:25 +01:00
|
|
|
let responseChat = await this.httpService.post('login', postData).toPromise();
|
2022-01-10 18:52:21 +01:00
|
|
|
|
2022-01-17 11:27:25 +01:00
|
|
|
if(responseChat) {
|
2022-01-14 09:03:31 +01:00
|
|
|
|
2022-02-09 11:34:21 +01:00
|
|
|
console.log('Login to Rocket chat OK', responseChat);
|
2022-01-14 09:03:31 +01:00
|
|
|
this.ValidatedUserChat = responseChat;
|
|
|
|
|
localStorage.setItem('userChat', JSON.stringify(responseChat));
|
|
|
|
|
this.storageService.store(AuthConnstants.AUTH, responseChat);
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
console.log('Network error');
|
|
|
|
|
this.presentAlert('Network error');
|
|
|
|
|
}
|
2022-02-08 14:22:32 +01:00
|
|
|
|
2022-02-24 12:10:09 +01:00
|
|
|
this.autoLoginChat(expirationDate.getTime() - date);
|
2022-02-08 14:22:32 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-03 22:57:33 +01:00
|
|
|
async autoLoginChat(expirationDate:number) {
|
2022-02-08 14:22:32 +01:00
|
|
|
setTimeout(()=>{
|
2022-02-08 16:37:32 +01:00
|
|
|
this.loginChat();
|
2022-02-08 14:22:32 +01:00
|
|
|
}, expirationDate)
|
2020-10-30 15:22:35 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-10 18:07:06 +01:00
|
|
|
loginToChatWs() {
|
2022-02-24 12:10:09 +01:00
|
|
|
setTimeout(()=>{
|
2022-02-07 17:55:00 +01:00
|
|
|
|
|
|
|
|
this.WsChatService.connect();
|
2022-03-03 22:57:33 +01:00
|
|
|
this.WsChatService.login().then((message: any) => {
|
|
|
|
|
|
|
|
|
|
SessionStore.user.RochetChatUserId = message.result.id
|
|
|
|
|
SessionStore.save()
|
2022-02-07 17:55:00 +01:00
|
|
|
this.WsChatService.setStatus('online')
|
2022-03-03 22:57:33 +01:00
|
|
|
|
|
|
|
|
}).catch((message) => {
|
2022-02-07 17:55:00 +01:00
|
|
|
console.log('rocket chat login failed', message)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise()
|
|
|
|
|
message.file.guid = guid.path
|
2022-03-24 17:40:14 +01:00
|
|
|
|
|
|
|
|
console.log('========================================',guid)
|
2022-03-15 16:28:31 +01:00
|
|
|
// await this.storage.set(guid.path, message.file.image_url).then(() => {
|
|
|
|
|
// console.log('add picture to chat IMAGE SAVED')
|
|
|
|
|
// // message.getFileFromDb()
|
|
|
|
|
// });
|
2022-02-08 15:33:44 +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-03-15 15:49:59 +01:00
|
|
|
console.log('failed to upload to server', e)
|
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
|
|
|
|
|
let url = await this.processesService.GetDocumentUrl(res.data.selected.Id, res.data.selected.ApplicationType).toPromise();
|
|
|
|
|
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
|
|
|
|
|
return true
|
|
|
|
|
|
2022-02-07 20:52:07 +01:00
|
|
|
} catch(e) {
|
2022-02-08 19:17:44 +01:00
|
|
|
console.log(e)
|
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
|
|
|
|
|
|
|
|
console.log('FILE TYPE', message.file.type)
|
|
|
|
|
let downloadFile = "";
|
|
|
|
|
if (message.file.type == "application/img") {
|
|
|
|
|
const event: any = await this.AttachmentsService.downloadFile(message.file.guid).toPromise();
|
|
|
|
|
|
|
|
|
|
console.log('FILE TYPE 22', message.file.guid)
|
2022-02-09 15:27:10 +01:00
|
|
|
|
2022-02-08 17:44:15 +01:00
|
|
|
if (event.type === HttpEventType.DownloadProgress) {
|
|
|
|
|
//this.downloadProgess = Math.round((100 * event.loaded) / event.total);
|
|
|
|
|
console.log('FILE TYPE 33', message.file.type)
|
|
|
|
|
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(() => {
|
|
|
|
|
console.log('IMAGE SAVED')
|
|
|
|
|
});
|
|
|
|
|
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-02-02 11:57:11 +01:00
|
|
|
setTimeout(()=>{
|
|
|
|
|
this.logout();
|
|
|
|
|
}, expirationDate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logout() {
|
|
|
|
|
|
|
|
|
|
this.ValidatedUser = null;
|
|
|
|
|
localStorage.removeItem('userChat');
|
|
|
|
|
|
|
|
|
|
SessionStore.setInativity(false)
|
|
|
|
|
SessionStore.setUrlBeforeInactivity(this.router.url)
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.router.navigateByUrl('/', { replaceUrl: true });
|
|
|
|
|
}, 100)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 10:02:30 +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-10-30 15:22:35 +01:00
|
|
|
//this.storageService.clear();
|
2021-02-23 16:10:14 +01:00
|
|
|
/* this.storageService.removeStorageItem(AuthConnstants.AUTH).then(res =>{
|
2020-10-30 15:22:35 +01:00
|
|
|
this.userData$.next('');
|
|
|
|
|
this.router.navigate(['']);
|
2021-02-23 16:10:14 +01:00
|
|
|
}) */
|
2020-10-30 15:22:35 +01:00
|
|
|
|
2020-08-19 23:47:11 +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
|
|
|
}
|