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

378 lines
15 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,
Router,
} from '@angular/router';
2018-07-06 21:01:23 +02:00
2018-07-10 20:46:13 +02:00
import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2';
2018-11-15 05:13:50 +01:00
import { ConstantsService } from 'jslib/services/constants.service';
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';
import { SearchService } from 'jslib/abstractions/search.service';
2018-11-15 05:13:50 +01:00
import { StorageService } from 'jslib/abstractions/storage.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-11-15 05:13:50 +01:00
import { UserConfirmComponent } from './user-confirm.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-11-15 05:13:50 +01:00
@ViewChild('confirmTemplate', { read: ViewContainerRef }) confirmModalRef: ViewContainerRef;
2018-07-10 20:46:13 +02:00
2018-07-06 21:01:23 +02:00
loading = true;
organizationId: string;
users: OrganizationUserUserDetailsResponse[];
pagedUsers: OrganizationUserUserDetailsResponse[];
2018-07-06 21:01:23 +02:00
searchText: string;
2018-07-11 22:40:32 +02:00
status: OrganizationUserStatusType = null;
statusMap = new Map<OrganizationUserStatusType, OrganizationUserUserDetailsResponse[]>();
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
protected didScroll = false;
protected pageSize = 100;
private pagedUsersCount = 0;
2018-07-10 20:46:13 +02:00
private modal: ModalComponent = null;
2018-07-11 22:40:32 +02:00
private allUsers: OrganizationUserUserDetailsResponse[];
2018-07-10 20:46:13 +02:00
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,
2018-11-15 05:13:50 +01:00
private userService: UserService, private router: Router,
private storageService: StorageService, private searchService: SearchService) { }
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);
if (!organization.isAdmin) {
this.router.navigate(['../collections'], { relativeTo: this.route });
return;
}
2018-07-11 20:43:00 +02:00
this.accessEvents = organization.useEvents;
this.accessGroups = organization.useGroups;
2018-07-07 05:08:10 +02:00
await this.load();
const queryParamsSub = 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 && user[0].status === OrganizationUserStatusType.Confirmed) {
this.events(user[0]);
}
}
2019-01-17 05:30:32 +01:00
if (queryParamsSub != null) {
queryParamsSub.unsubscribe();
}
});
2018-07-06 21:01:23 +02:00
});
}
async load() {
const response = await this.apiService.getOrganizationUsers(this.organizationId);
2018-07-11 22:40:32 +02:00
this.statusMap.clear();
this.allUsers = response.data != null && response.data.length > 0 ? response.data : [];
this.allUsers.sort(Utils.getSortFunction(this.i18nService, 'email'));
this.allUsers.forEach((u) => {
if (!this.statusMap.has(u.status)) {
this.statusMap.set(u.status, [u]);
} else {
this.statusMap.get(u.status).push(u);
}
});
this.filter(this.status);
2018-07-06 21:01:23 +02:00
this.loading = false;
}
2018-07-09 23:07:13 +02:00
2018-07-11 22:40:32 +02:00
filter(status: OrganizationUserStatusType) {
this.status = status;
if (this.status != null) {
this.users = this.statusMap.get(this.status);
} else {
this.users = this.allUsers;
}
this.resetPaging();
}
loadMore() {
if (!this.users || this.users.length <= this.pageSize) {
return;
}
const pagedLength = this.pagedUsers.length;
let pagedSize = this.pageSize;
if (pagedLength === 0 && this.pagedUsersCount > this.pageSize) {
pagedSize = this.pagedUsersCount;
}
if (this.users.length > pagedLength) {
this.pagedUsers = this.pagedUsers.concat(this.users.slice(pagedLength, pagedLength + pagedSize));
}
this.pagedUsersCount = this.pagedUsers.length;
this.didScroll = this.pagedUsers.length > this.pageSize;
2018-07-11 22:40:32 +02:00
}
get allCount() {
2020-04-20 06:17:06 +02:00
return this.allUsers != null ? this.allUsers.length : 0;
}
2018-07-11 22:40:32 +02:00
get invitedCount() {
return this.statusMap.has(OrganizationUserStatusType.Invited) ?
this.statusMap.get(OrganizationUserStatusType.Invited).length : 0;
}
get acceptedCount() {
return this.statusMap.has(OrganizationUserStatusType.Accepted) ?
this.statusMap.get(OrganizationUserStatusType.Accepted).length : 0;
}
get confirmedCount() {
return this.statusMap.has(OrganizationUserStatusType.Confirmed) ?
this.statusMap.get(OrganizationUserStatusType.Confirmed).length : 0;
}
get showConfirmUsers(): boolean {
return this.allUsers != null && this.statusMap != null && this.allUsers.length > 1 &&
this.confirmedCount > 0 && this.confirmedCount < 3 && this.acceptedCount > 0;
}
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) {
2018-11-15 05:13:50 +01:00
function updateUser(self: PeopleComponent) {
user.status = OrganizationUserStatusType.Confirmed;
const mapIndex = self.statusMap.get(OrganizationUserStatusType.Accepted).indexOf(user);
if (mapIndex > -1) {
self.statusMap.get(OrganizationUserStatusType.Accepted).splice(mapIndex, 1);
self.statusMap.get(OrganizationUserStatusType.Confirmed).push(user);
}
}
2018-07-11 19:30:17 +02:00
if (this.actionPromise != null) {
return;
}
2018-11-15 05:13:50 +01:00
const autoConfirm = await this.storageService.get<boolean>(ConstantsService.autoConfirmFingerprints);
if (autoConfirm == null || !autoConfirm) {
if (this.modal != null) {
this.modal.close();
}
const factory = this.componentFactoryResolver.resolveComponentFactory(ModalComponent);
2019-03-16 02:04:57 +01:00
this.modal = this.confirmModalRef.createComponent(factory).instance;
2018-11-15 05:13:50 +01:00
const childComponent = this.modal.show<UserConfirmComponent>(
UserConfirmComponent, this.confirmModalRef);
childComponent.name = user != null ? user.name || user.email : null;
childComponent.organizationId = this.organizationId;
childComponent.organizationUserId = user != null ? user.id : null;
childComponent.userId = user != null ? user.userId : null;
childComponent.onConfirmedUser.subscribe(() => {
this.modal.close();
updateUser(this);
});
this.modal.onClosed.subscribe(() => {
this.modal = null;
});
return;
}
2018-07-11 19:30:17 +02:00
this.actionPromise = this.doConfirmation(user);
await this.actionPromise;
2018-11-15 05:13:50 +01:00
updateUser(this);
2018-07-11 19:30:17 +02:00
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
}
async resetPaging() {
this.pagedUsers = [];
this.loadMore();
}
isSearching() {
return this.searchService.isSearchable(this.searchText);
}
isPaging() {
const searching = this.isSearching();
if (searching && this.didScroll) {
this.resetPaging();
}
return !searching && this.users && this.users.length > this.pageSize;
}
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);
try {
// tslint:disable-next-line
console.log('User\'s fingerprint: ' +
(await this.cryptoService.getFingerprint(user.userId, publicKey.buffer)).join('-'));
} catch { }
2018-07-11 19:30:17 +02:00
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) {
let index = this.users.indexOf(user);
2018-07-10 20:46:13 +02:00
if (index > -1) {
this.users.splice(index, 1);
this.resetPaging();
2018-07-10 20:46:13 +02:00
}
if (this.statusMap.has(OrganizationUserStatusType.Accepted)) {
index = this.statusMap.get(OrganizationUserStatusType.Accepted).indexOf(user);
if (index > -1) {
this.statusMap.get(OrganizationUserStatusType.Accepted).splice(index, 1);
}
}
if (this.statusMap.has(OrganizationUserStatusType.Invited)) {
index = this.statusMap.get(OrganizationUserStatusType.Invited).indexOf(user);
if (index > -1) {
this.statusMap.get(OrganizationUserStatusType.Invited).splice(index, 1);
}
}
if (this.statusMap.has(OrganizationUserStatusType.Confirmed)) {
index = this.statusMap.get(OrganizationUserStatusType.Confirmed).indexOf(user);
if (index > -1) {
this.statusMap.get(OrganizationUserStatusType.Confirmed).splice(index, 1);
}
}
2018-07-09 23:07:13 +02:00
}
2018-07-06 21:01:23 +02:00
}