bitwarden-estensione-browser/src/app/organizations/manage/events.component.ts

186 lines
6.6 KiB
TypeScript
Raw Normal View History

2018-07-07 05:08:10 +02:00
import {
Component,
OnInit,
} from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
2018-07-07 05:08:10 +02:00
2018-07-09 18:22:32 +02:00
import { ToasterService } from 'angular2-toaster';
import { ApiService } from 'jslib-common/abstractions/api.service';
import { ExportService } from 'jslib-common/abstractions/export.service';
import { I18nService } from 'jslib-common/abstractions/i18n.service';
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
import { UserService } from 'jslib-common/abstractions/user.service';
2018-07-07 05:08:10 +02:00
import { EventResponse } from 'jslib-common/models/response/eventResponse';
import { ListResponse } from 'jslib-common/models/response/listResponse';
import { EventView } from 'jslib-common/models/view/eventView';
2018-07-06 16:21:08 +02:00
import { EventService } from '../../services/event.service';
2018-07-09 17:47:57 +02:00
2018-07-06 16:21:08 +02:00
@Component({
selector: 'app-org-events',
templateUrl: 'events.component.html',
})
2018-07-07 05:08:10 +02:00
export class EventsComponent implements OnInit {
loading = true;
loaded = false;
organizationId: string;
events: EventView[];
2018-07-07 05:08:10 +02:00
start: string;
end: string;
dirtyDates: boolean = true;
2018-07-07 05:08:10 +02:00
continuationToken: string;
refreshPromise: Promise<any>;
exportPromise: Promise<any>;
2018-07-07 05:08:10 +02:00
morePromise: Promise<any>;
2018-07-09 18:22:32 +02:00
private orgUsersUserIdMap = new Map<string, any>();
private orgUsersIdMap = new Map<string, any>();
constructor(private apiService: ApiService, private route: ActivatedRoute, private eventService: EventService,
private i18nService: I18nService, private toasterService: ToasterService, private userService: UserService,
private exportService: ExportService, private platformUtilsService: PlatformUtilsService,
private router: Router) { }
2018-07-07 05:08:10 +02:00
async ngOnInit() {
this.route.parent.parent.params.subscribe(async params => {
2018-07-07 05:08:10 +02:00
this.organizationId = params.organizationId;
const organization = await this.userService.getOrganization(this.organizationId);
if (organization == null || !organization.useEvents) {
this.router.navigate(['/organizations', this.organizationId]);
return;
}
2018-07-07 05:08:10 +02:00
const defaultDates = this.eventService.getDefaultDateFilters();
this.start = defaultDates[0];
this.end = defaultDates[1];
await this.load();
});
}
async load() {
2018-07-09 18:22:32 +02:00
const response = await this.apiService.getOrganizationUsers(this.organizationId);
response.data.forEach(u => {
2018-07-09 18:22:32 +02:00
const name = u.name == null || u.name.trim() === '' ? u.email : u.name;
this.orgUsersIdMap.set(u.id, { name: name, email: u.email });
this.orgUsersUserIdMap.set(u.userId, { name: name, email: u.email });
});
2018-07-07 05:08:10 +02:00
await this.loadEvents(true);
this.loaded = true;
}
async exportEvents() {
if (this.appApiPromiseUnfulfilled() || this.dirtyDates) {
return;
}
this.loading = true;
const dates = this.parseDates();
if (dates == null) {
return;
}
try {
this.exportPromise = this.export(dates[0], dates[1]);
await this.exportPromise;
} catch { }
this.exportPromise = null;
this.loading = false;
}
2018-07-07 05:08:10 +02:00
async loadEvents(clearExisting: boolean) {
if (this.appApiPromiseUnfulfilled()) {
2018-07-07 05:08:10 +02:00
return;
}
const dates = this.parseDates();
if (dates == null) {
2018-07-07 05:08:10 +02:00
return;
}
this.loading = true;
let events: EventView[] = [];
2018-07-07 05:08:10 +02:00
try {
const promise = this.loadAndParseEvents(dates[0], dates[1], clearExisting ? null : this.continuationToken);
2018-07-07 05:08:10 +02:00
if (clearExisting) {
this.refreshPromise = promise;
} else {
this.morePromise = promise;
}
const result = await promise;
this.continuationToken = result.continuationToken;
events = result.events;
2018-07-07 05:08:10 +02:00
} catch { }
2018-07-09 17:47:57 +02:00
if (!clearExisting && this.events != null && this.events.length > 0) {
this.events = this.events.concat(events);
} else {
this.events = events;
}
this.dirtyDates = false;
this.loading = false;
this.morePromise = null;
this.refreshPromise = null;
}
private async export(start: string, end: string) {
let continuationToken = this.continuationToken;
let events = [].concat(this.events);
while (continuationToken != null) {
const result = await this.loadAndParseEvents(start, end, continuationToken);
continuationToken = result.continuationToken;
events = events.concat(result.events);
}
const data = await this.exportService.getEventExport(events);
const fileName = this.exportService.getFileName('org-events', 'csv');
this.platformUtilsService.saveFile(window, data, { type: 'text/plain' }, fileName);
}
private async loadAndParseEvents(startDate: string, endDate: string, continuationToken: string) {
const response = await this.apiService.getEventsOrganization(this.organizationId, startDate, endDate,
continuationToken);
const events = await Promise.all(response.data.map(async r => {
2018-07-09 17:47:57 +02:00
const userId = r.actingUserId == null ? r.userId : r.actingUserId;
const eventInfo = await this.eventService.getEventInfo(r);
2018-07-09 18:22:32 +02:00
const user = userId != null && this.orgUsersUserIdMap.has(userId) ?
this.orgUsersUserIdMap.get(userId) : null;
return new EventView({
2018-07-09 17:47:57 +02:00
message: eventInfo.message,
humanReadableMessage: eventInfo.humanReadableMessage,
2018-07-09 17:47:57 +02:00
appIcon: eventInfo.appIcon,
appName: eventInfo.appName,
userId: userId,
2018-07-09 18:22:32 +02:00
userName: user != null ? user.name : this.i18nService.t('unknown'),
userEmail: user != null ? user.email : '',
2018-07-09 17:47:57 +02:00
date: r.date,
ip: r.ipAddress,
2018-07-10 14:39:05 +02:00
type: r.type,
});
}));
return { continuationToken: response.continuationToken, events: events };
}
2018-07-09 17:47:57 +02:00
private parseDates() {
let dates: string[] = null;
try {
dates = this.eventService.formatDateFilters(this.start, this.end);
} catch (e) {
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('invalidDateRange'));
return null;
2018-07-09 17:47:57 +02:00
}
return dates;
2018-07-07 05:08:10 +02:00
}
private appApiPromiseUnfulfilled() {
return this.refreshPromise != null || this.morePromise != null || this.exportPromise != null;
}
2018-07-07 05:08:10 +02:00
}