Files
doneit-web/src/app/services/auth.service.ts
T

333 lines
9.8 KiB
TypeScript
Raw Normal View History

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';
import { environment } from 'src/environments/environment';
import { HttpService } from './http.service';
2021-08-27 15:21:15 +01:00
import { BehaviorSubject } from 'rxjs';
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';
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-03-28 13:34:01 +01:00
import { InitialsService } from './functions/initials.service';
import { PermissionService } from './permission.service';
2022-02-08 17:44:15 +01:00
2020-08-06 14:31:07 +01:00
@Injectable({
providedIn: 'root'
})
export class AuthService {
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(
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,
private WsChatService: WsChatService,
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,
private initialsService: InitialsService,
public p: PermissionService, ) {
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-05-09 15:01:23 +01:00
if(this.p.userPermission(this.p.permissionList.Chat.access) == true ) {
this.loginToChatWs()
}
2021-05-10 14:51:51 +01:00
}
2021-07-18 20:20:30 +01:00
2022-05-09 15:01:23 +01:00
if(SessionStore?.user?.ChatData) {
this.ValidatedUserChat = SessionStore.user.ChatData
}
2022-05-06 15:56:18 +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;
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
2022-04-28 09:32:27 +01:00
2022-04-16 19:23:44 +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'
2021-06-09 14:00:14 +01:00
}
2022-04-02 09:40:09 +01:00
else if(session.RoleID == 99999873) {
session.Profile = 'Assistant'
}
else if(session.RoleID == 99999874) {
session.Profile = 'Department boss'
}
else if(session.RoleID == 99999875) {
session.Profile = 'Director'
}
else if(session.RoleID == 99999876) {
session.Profile = 'Deputy Director'
}
else if(session.RoleID == 99999885) {
session.Profile = 'Secretariat'
}
else if(session.RoleID == 99999886) {
session.Profile = 'General secretary'
2022-03-28 13:34:01 +01:00
}
2021-07-22 17:07:04 +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
2022-04-12 15:48:32 +01:00
2022-04-28 09:32:27 +01:00
2022-04-12 15:48:32 +01:00
2021-08-27 15:21:15 +01:00
SessionStore.reset(session)
this.ValidatedUser = SessionStore.user;
2022-05-09 15:01:23 +01:00
console.log('!!!::!!===', this.ValidatedUser)
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-06 14:31:07 +01:00
2022-02-08 14:22:32 +01:00
//Login to rocketChat server2
//user: UserForm
2022-04-16 19:23:44 +01:00
async loginChat(responseChat = this.ValidatedUserChat) {
2022-02-08 14:22:32 +01:00
2022-05-09 15:01:23 +01:00
if(SessionStore?.user?.ChatData) {
this.ValidatedUserChat = SessionStore.user.ChatData
}
2022-02-08 14:22:32 +01:00
2022-04-16 19:23:44 +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-04-26 14:34:52 +01:00
"user": SessionStore.user.UserName,
2022-02-08 16:23:30 +01:00
"password": SessionStore.user.Password,
2021-06-04 11:37:56 +01:00
}
let responseChat = await this.httpService.post('login', postData).toPromise();
if(responseChat) {
2022-04-28 09:32:27 +01:00
2022-01-14 09:03:31 +01:00
this.ValidatedUserChat = responseChat;
localStorage.setItem('userChat', JSON.stringify(responseChat));
this.storageService.store(AuthConnstants.AUTH, responseChat);
}
else{
2022-04-28 09:32:27 +01:00
2022-01-14 09:03:31 +01:00
this.presentAlert('Network error');
}
2022-02-08 14:22:32 +01:00
2022-04-16 19:23:44 +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-04-16 19:23:44 +01:00
/* setTimeout(()=>{
this.loginChat(this.ValidatedUserChat);
}, expirationDate) */
}
2022-02-10 18:07:06 +01:00
loginToChatWs() {
2022-02-24 12:10:09 +01:00
setTimeout(()=>{
2022-04-28 09:32:27 +01:00
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) => {
2022-04-28 09:32:27 +01:00
2022-03-28 13:34:01 +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-04-28 09:32:27 +01:00
//
2022-04-06 14:51:35 +01:00
2022-02-07 17:55:00 +01:00
this.WsChatService.setStatus('online')
2022-04-22 16:42:37 +01:00
// alert('wsLogin')
2022-03-03 22:57:33 +01:00
}).catch((message) => {
2022-04-28 09:32:27 +01:00
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
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
2022-04-28 09:32:27 +01:00
2022-03-15 16:28:31 +01:00
// await this.storage.set(guid.path, message.file.image_url).then(() => {
2022-04-28 09:32:27 +01:00
//
2022-03-15 16:28:31 +01:00
// // message.getFileFromDb()
// });
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 {
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-04-28 09:32:27 +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();
2022-04-28 09:32:27 +01:00
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);
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) {
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=>{
this.userData$.next(res);
2021-02-25 12:41:29 +01:00
});
}
2020-11-24 13:46:13 +01:00
2022-02-16 15:52:59 +01:00
logoutChat() {
//this.storageService.clear();
2021-02-23 16:10:14 +01:00
/* this.storageService.removeStorageItem(AuthConnstants.AUTH).then(res =>{
this.userData$.next('');
this.router.navigate(['']);
2021-02-23 16:10:14 +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
}