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

218 lines
8.6 KiB
TypeScript
Raw Normal View History

2018-07-06 21:01:23 +02:00
import {
Component,
2018-07-10 20:46:13 +02:00
ComponentFactoryResolver,
2018-07-06 21:01:23 +02:00
OnInit,
2018-07-10 20:46:13 +02:00
ViewChild,
ViewContainerRef,
2018-07-06 21:01:23 +02:00
} from '@angular/core';
import { ActivatedRoute } from '@angular/router';
2018-07-10 20:46:13 +02:00
import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2';
2018-07-06 21:01:23 +02:00
import { ApiService } from 'jslib/abstractions/api.service';
2018-07-11 19:30:17 +02:00
import { CryptoService } from 'jslib/abstractions/crypto.service';
2018-07-06 21:01:23 +02:00
import { I18nService } from 'jslib/abstractions/i18n.service';
2018-07-10 20:46:13 +02:00
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
2018-07-11 20:43:00 +02:00
import { UserService } from 'jslib/abstractions/user.service';
2018-07-06 21:01:23 +02:00
2018-07-11 19:30:17 +02:00
import { OrganizationUserConfirmRequest } from 'jslib/models/request/organizationUserConfirmRequest';
2018-07-06 21:01:23 +02:00
import { OrganizationUserUserDetailsResponse } from 'jslib/models/response/organizationUserResponse';
2018-07-06 21:45:35 +02:00
import { OrganizationUserStatusType } from 'jslib/enums/organizationUserStatusType';
import { OrganizationUserType } from 'jslib/enums/organizationUserType';
2018-07-06 21:01:23 +02:00
import { Utils } from 'jslib/misc/utils';
2018-07-06 16:21:08 +02:00
2018-07-10 20:46:13 +02:00
import { ModalComponent } from '../../modal.component';
import { EntityEventsComponent } from './entity-events.component';
2018-07-10 20:46:13 +02:00
import { UserAddEditComponent } from './user-add-edit.component';
2018-07-10 21:03:13 +02:00
import { UserGroupsComponent } from './user-groups.component';
2018-07-10 20:46:13 +02:00
2018-07-06 16:21:08 +02:00
@Component({
selector: 'app-org-people',
templateUrl: 'people.component.html',
})
2018-07-06 21:01:23 +02:00
export class PeopleComponent implements OnInit {
2018-07-10 20:46:13 +02:00
@ViewChild('addEdit', { read: ViewContainerRef }) addEditModalRef: ViewContainerRef;
@ViewChild('groupsTemplate', { read: ViewContainerRef }) groupsModalRef: ViewContainerRef;
2018-07-11 20:43:00 +02:00
@ViewChild('eventsTemplate', { read: ViewContainerRef }) eventsModalRef: ViewContainerRef;
2018-07-10 20:46:13 +02:00
2018-07-06 21:01:23 +02:00
loading = true;
organizationId: string;
users: OrganizationUserUserDetailsResponse[];
searchText: string;
2018-07-06 21:45:35 +02:00
organizationUserType = OrganizationUserType;
organizationUserStatusType = OrganizationUserStatusType;
2018-07-11 19:30:17 +02:00
actionPromise: Promise<any>;
2018-07-11 20:43:00 +02:00
accessEvents = false;
accessGroups = false;
2018-07-06 21:01:23 +02:00
2018-07-10 20:46:13 +02:00
private modal: ModalComponent = null;
2018-07-06 21:01:23 +02:00
constructor(private apiService: ApiService, private route: ActivatedRoute,
2018-07-10 20:46:13 +02:00
private i18nService: I18nService, private componentFactoryResolver: ComponentFactoryResolver,
private platformUtilsService: PlatformUtilsService, private analytics: Angulartics2,
2018-07-11 20:43:00 +02:00
private toasterService: ToasterService, private cryptoService: CryptoService,
private userService: UserService) { }
2018-07-06 21:01:23 +02:00
async ngOnInit() {
this.route.parent.parent.params.subscribe(async (params) => {
this.organizationId = params.organizationId;
2018-07-11 20:43:00 +02:00
const organization = await this.userService.getOrganization(this.organizationId);
this.accessEvents = organization.useEvents;
this.accessGroups = organization.useGroups;
2018-07-07 05:08:10 +02:00
await this.load();
this.route.queryParams.subscribe(async (qParams) => {
this.searchText = qParams.search;
if (qParams.viewEvents != null) {
const user = this.users.filter((u) => u.id === qParams.viewEvents);
if (user.length > 0) {
this.events(user[0]);
}
}
});
2018-07-06 21:01:23 +02:00
});
}
async load() {
const response = await this.apiService.getOrganizationUsers(this.organizationId);
const users = response.data != null && response.data.length > 0 ? response.data : [];
users.sort(Utils.getSortFunction(this.i18nService, 'email'));
this.users = users;
this.loading = false;
}
2018-07-09 23:07:13 +02:00
edit(user: OrganizationUserUserDetailsResponse) {
2018-07-10 20:46:13 +02:00
if (this.modal != null) {
this.modal.close();
}
const factory = this.componentFactoryResolver.resolveComponentFactory(ModalComponent);
this.modal = this.addEditModalRef.createComponent(factory).instance;
const childComponent = this.modal.show<UserAddEditComponent>(
UserAddEditComponent, this.addEditModalRef);
childComponent.name = user != null ? user.name || user.email : null;
childComponent.organizationId = this.organizationId;
childComponent.organizationUserId = user != null ? user.id : null;
childComponent.onSavedUser.subscribe(() => {
this.modal.close();
this.load();
});
childComponent.onDeletedUser.subscribe(() => {
this.modal.close();
this.removeUser(user);
});
this.modal.onClosed.subscribe(() => {
this.modal = null;
});
2018-07-09 23:07:13 +02:00
}
invite() {
2018-07-10 20:46:13 +02:00
this.edit(null);
2018-07-09 23:07:13 +02:00
}
2018-07-10 21:03:13 +02:00
groups(user: OrganizationUserUserDetailsResponse) {
if (this.modal != null) {
this.modal.close();
}
const factory = this.componentFactoryResolver.resolveComponentFactory(ModalComponent);
this.modal = this.groupsModalRef.createComponent(factory).instance;
const childComponent = this.modal.show<UserGroupsComponent>(
UserGroupsComponent, this.groupsModalRef);
childComponent.name = user != null ? user.name || user.email : null;
childComponent.organizationId = this.organizationId;
childComponent.organizationUserId = user != null ? user.id : null;
childComponent.onSavedUser.subscribe(() => {
this.modal.close();
});
this.modal.onClosed.subscribe(() => {
this.modal = null;
});
}
2018-07-09 23:07:13 +02:00
async remove(user: OrganizationUserUserDetailsResponse) {
2018-07-10 20:46:13 +02:00
const confirmed = await this.platformUtilsService.showDialog(
this.i18nService.t('removeUserConfirmation'), user.name || user.email,
this.i18nService.t('yes'), this.i18nService.t('no'), 'warning');
if (!confirmed) {
return false;
}
try {
await this.apiService.deleteOrganizationUser(this.organizationId, user.id);
this.analytics.eventTrack.next({ action: 'Deleted User' });
this.toasterService.popAsync('success', null, this.i18nService.t('removedUserId', user.name || user.email));
this.removeUser(user);
} catch { }
}
2018-07-11 19:30:17 +02:00
async reinvite(user: OrganizationUserUserDetailsResponse) {
if (this.actionPromise != null) {
return;
}
this.actionPromise = this.apiService.postOrganizationUserReinvite(this.organizationId, user.id);
await this.actionPromise;
this.analytics.eventTrack.next({ action: 'Reinvited User' });
this.toasterService.popAsync('success', null, this.i18nService.t('hasBeenReinvited', user.name || user.email));
this.actionPromise = null;
}
async confirm(user: OrganizationUserUserDetailsResponse) {
if (this.actionPromise != null) {
return;
}
this.actionPromise = this.doConfirmation(user);
await this.actionPromise;
user.status = OrganizationUserStatusType.Confirmed;
this.analytics.eventTrack.next({ action: 'Confirmed User' });
this.toasterService.popAsync('success', null, this.i18nService.t('hasBeenConfirmed', user.name || user.email));
this.actionPromise = null;
}
async events(user: OrganizationUserUserDetailsResponse) {
2018-07-11 20:43:00 +02:00
if (this.modal != null) {
this.modal.close();
}
const factory = this.componentFactoryResolver.resolveComponentFactory(ModalComponent);
this.modal = this.eventsModalRef.createComponent(factory).instance;
const childComponent = this.modal.show<EntityEventsComponent>(
EntityEventsComponent, this.eventsModalRef);
2018-07-11 20:43:00 +02:00
childComponent.name = user.name || user.email;
2018-07-11 20:43:00 +02:00
childComponent.organizationId = this.organizationId;
childComponent.entityId = user.id;
childComponent.showUser = false;
childComponent.entity = 'user';
2018-07-11 19:30:17 +02:00
2018-07-11 20:43:00 +02:00
this.modal.onClosed.subscribe(() => {
this.modal = null;
});
2018-07-11 19:30:17 +02:00
}
private async doConfirmation(user: OrganizationUserUserDetailsResponse) {
const orgKey = await this.cryptoService.getOrgKey(this.organizationId);
const publicKeyResponse = await this.apiService.getUserPublicKey(user.userId);
const publicKey = Utils.fromB64ToArray(publicKeyResponse.publicKey);
const key = await this.cryptoService.rsaEncrypt(orgKey.key, publicKey.buffer);
const request = new OrganizationUserConfirmRequest();
request.key = key.encryptedString;
await this.apiService.postOrganizationUserConfirm(this.organizationId, user.id, request);
}
2018-07-10 20:46:13 +02:00
private removeUser(user: OrganizationUserUserDetailsResponse) {
const index = this.users.indexOf(user);
if (index > -1) {
this.users.splice(index, 1);
}
2018-07-09 23:07:13 +02:00
}
2018-07-06 21:01:23 +02:00
}