2018-07-10 20:46:13 +02:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
EventEmitter,
|
|
|
|
Input,
|
|
|
|
OnInit,
|
|
|
|
Output,
|
|
|
|
} from '@angular/core';
|
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { ApiService } from 'jslib-common/abstractions/api.service';
|
|
|
|
import { CollectionService } from 'jslib-common/abstractions/collection.service';
|
|
|
|
import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
2021-10-20 18:30:04 +02:00
|
|
|
import { LogService } from 'jslib-common/abstractions/log.service';
|
2021-06-07 20:13:58 +02:00
|
|
|
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
|
2018-07-10 20:46:13 +02:00
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { CollectionData } from 'jslib-common/models/data/collectionData';
|
|
|
|
import { Collection } from 'jslib-common/models/domain/collection';
|
|
|
|
import { OrganizationUserInviteRequest } from 'jslib-common/models/request/organizationUserInviteRequest';
|
|
|
|
import { OrganizationUserUpdateRequest } from 'jslib-common/models/request/organizationUserUpdateRequest';
|
|
|
|
import { SelectionReadOnlyRequest } from 'jslib-common/models/request/selectionReadOnlyRequest';
|
|
|
|
import { CollectionDetailsResponse } from 'jslib-common/models/response/collectionResponse';
|
|
|
|
import { CollectionView } from 'jslib-common/models/view/collectionView';
|
2018-07-10 20:46:13 +02:00
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { OrganizationUserType } from 'jslib-common/enums/organizationUserType';
|
|
|
|
import { PermissionsApi } from 'jslib-common/models/api/permissionsApi';
|
2018-07-10 20:46:13 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-user-add-edit',
|
|
|
|
templateUrl: 'user-add-edit.component.html',
|
|
|
|
})
|
|
|
|
export class UserAddEditComponent implements OnInit {
|
|
|
|
@Input() name: string;
|
|
|
|
@Input() organizationUserId: string;
|
|
|
|
@Input() organizationId: string;
|
2021-11-22 18:39:20 +01:00
|
|
|
@Input() usesKeyConnector: boolean = false;
|
2018-07-10 20:46:13 +02:00
|
|
|
@Output() onSavedUser = new EventEmitter();
|
|
|
|
@Output() onDeletedUser = new EventEmitter();
|
|
|
|
|
|
|
|
loading = true;
|
|
|
|
editMode: boolean = false;
|
|
|
|
title: string;
|
|
|
|
emails: string;
|
|
|
|
type: OrganizationUserType = OrganizationUserType.User;
|
2021-01-12 21:31:22 +01:00
|
|
|
permissions = new PermissionsApi();
|
|
|
|
showCustom = false;
|
2018-07-10 20:46:13 +02:00
|
|
|
access: 'all' | 'selected' = 'selected';
|
|
|
|
collections: CollectionView[] = [];
|
|
|
|
formPromise: Promise<any>;
|
|
|
|
deletePromise: Promise<any>;
|
|
|
|
organizationUserType = OrganizationUserType;
|
|
|
|
|
2021-10-05 18:12:44 +02:00
|
|
|
manageAllCollectionsCheckboxes = [
|
|
|
|
{
|
|
|
|
id: 'createNewCollections',
|
|
|
|
get: () => this.permissions.createNewCollections,
|
|
|
|
set: (v: boolean) => this.permissions.createNewCollections = v,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'editAnyCollection',
|
|
|
|
get: () => this.permissions.editAnyCollection,
|
|
|
|
set: (v: boolean) => this.permissions.editAnyCollection = v,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'deleteAnyCollection',
|
|
|
|
get: () => this.permissions.deleteAnyCollection,
|
|
|
|
set: (v: boolean) => this.permissions.deleteAnyCollection = v,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
manageAssignedCollectionsCheckboxes = [
|
|
|
|
{
|
|
|
|
id: 'editAssignedCollections',
|
|
|
|
get: () => this.permissions.editAssignedCollections,
|
|
|
|
set: (v: boolean) => this.permissions.editAssignedCollections = v,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'deleteAssignedCollections',
|
|
|
|
get: () => this.permissions.deleteAssignedCollections,
|
|
|
|
set: (v: boolean) => this.permissions.deleteAssignedCollections = v,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-01-12 21:31:22 +01:00
|
|
|
get customUserTypeSelected(): boolean {
|
|
|
|
return this.type === OrganizationUserType.Custom;
|
|
|
|
}
|
|
|
|
|
2018-07-10 20:46:13 +02:00
|
|
|
constructor(private apiService: ApiService, private i18nService: I18nService,
|
2021-12-07 20:41:45 +01:00
|
|
|
private collectionService: CollectionService,
|
2021-10-20 18:30:04 +02:00
|
|
|
private platformUtilsService: PlatformUtilsService, private logService: LogService) { }
|
2018-07-10 20:46:13 +02:00
|
|
|
|
|
|
|
async ngOnInit() {
|
|
|
|
this.editMode = this.loading = this.organizationUserId != null;
|
|
|
|
await this.loadCollections();
|
|
|
|
|
|
|
|
if (this.editMode) {
|
|
|
|
this.editMode = true;
|
|
|
|
this.title = this.i18nService.t('editUser');
|
|
|
|
try {
|
|
|
|
const user = await this.apiService.getOrganizationUser(this.organizationId, this.organizationUserId);
|
|
|
|
this.access = user.accessAll ? 'all' : 'selected';
|
|
|
|
this.type = user.type;
|
2021-01-12 21:31:22 +01:00
|
|
|
if (user.type === OrganizationUserType.Custom) {
|
|
|
|
this.permissions = user.permissions;
|
|
|
|
}
|
2018-07-10 20:46:13 +02:00
|
|
|
if (user.collections != null && this.collections != null) {
|
2021-02-03 18:41:33 +01:00
|
|
|
user.collections.forEach(s => {
|
|
|
|
const collection = this.collections.filter(c => c.id === s.id);
|
2018-07-10 20:46:13 +02:00
|
|
|
if (collection != null && collection.length > 0) {
|
|
|
|
(collection[0] as any).checked = true;
|
|
|
|
collection[0].readOnly = s.readOnly;
|
2020-05-21 15:55:58 +02:00
|
|
|
collection[0].hidePasswords = s.hidePasswords;
|
2018-07-10 20:46:13 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
|
|
|
}
|
2018-07-10 20:46:13 +02:00
|
|
|
} else {
|
|
|
|
this.title = this.i18nService.t('inviteUser');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadCollections() {
|
|
|
|
const response = await this.apiService.getCollections(this.organizationId);
|
2021-02-03 18:41:33 +01:00
|
|
|
const collections = response.data.map(r =>
|
2018-07-10 20:46:13 +02:00
|
|
|
new Collection(new CollectionData(r as CollectionDetailsResponse)));
|
|
|
|
this.collections = await this.collectionService.decryptMany(collections);
|
|
|
|
}
|
|
|
|
|
|
|
|
check(c: CollectionView, select?: boolean) {
|
|
|
|
(c as any).checked = select == null ? !(c as any).checked : select;
|
|
|
|
if (!(c as any).checked) {
|
|
|
|
c.readOnly = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
selectAll(select: boolean) {
|
2021-02-03 18:41:33 +01:00
|
|
|
this.collections.forEach(c => this.check(c, select));
|
2018-07-10 20:46:13 +02:00
|
|
|
}
|
|
|
|
|
2021-01-12 21:31:22 +01:00
|
|
|
setRequestPermissions(p: PermissionsApi, clearPermissions: boolean) {
|
2021-10-05 18:12:44 +02:00
|
|
|
Object.assign(p, clearPermissions ? new PermissionsApi() : this.permissions);
|
2021-01-12 21:31:22 +01:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2021-06-14 20:12:15 +02:00
|
|
|
handleDependentPermissions() {
|
|
|
|
// Manage Password Reset must have Manage Users enabled
|
|
|
|
if (this.permissions.manageResetPassword && !this.permissions.manageUsers) {
|
|
|
|
this.permissions.manageUsers = true;
|
|
|
|
(document.getElementById('manageUsers') as HTMLInputElement).checked = true;
|
|
|
|
this.platformUtilsService.showToast('info', null, this.i18nService.t('resetPasswordManageUsers'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 20:46:13 +02:00
|
|
|
async submit() {
|
|
|
|
let collections: SelectionReadOnlyRequest[] = null;
|
|
|
|
if (this.access !== 'all') {
|
2021-02-03 18:41:33 +01:00
|
|
|
collections = this.collections.filter(c => (c as any).checked)
|
|
|
|
.map(c => new SelectionReadOnlyRequest(c.id, !!c.readOnly, !!c.hidePasswords));
|
2018-07-10 20:46:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (this.editMode) {
|
|
|
|
const request = new OrganizationUserUpdateRequest();
|
|
|
|
request.accessAll = this.access === 'all';
|
|
|
|
request.type = this.type;
|
|
|
|
request.collections = collections;
|
2021-01-12 21:31:22 +01:00
|
|
|
request.permissions = this.setRequestPermissions(request.permissions ?? new PermissionsApi(), request.type !== OrganizationUserType.Custom);
|
2018-07-10 20:46:13 +02:00
|
|
|
this.formPromise = this.apiService.putOrganizationUser(this.organizationId, this.organizationUserId,
|
|
|
|
request);
|
|
|
|
} else {
|
|
|
|
const request = new OrganizationUserInviteRequest();
|
|
|
|
request.emails = this.emails.trim().split(/\s*,\s*/);
|
|
|
|
request.accessAll = this.access === 'all';
|
|
|
|
request.type = this.type;
|
2021-01-12 21:31:22 +01:00
|
|
|
request.permissions = this.setRequestPermissions(request.permissions ?? new PermissionsApi(), request.type !== OrganizationUserType.Custom);
|
2018-07-10 20:46:13 +02:00
|
|
|
request.collections = collections;
|
|
|
|
this.formPromise = this.apiService.postOrganizationUserInvite(this.organizationId, request);
|
|
|
|
}
|
|
|
|
await this.formPromise;
|
2021-12-07 20:41:45 +01:00
|
|
|
this.platformUtilsService.showToast('success', null,
|
2018-07-10 20:46:13 +02:00
|
|
|
this.i18nService.t(this.editMode ? 'editedUserId' : 'invitedUsers', this.name));
|
|
|
|
this.onSavedUser.emit();
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
|
|
|
}
|
2018-07-10 20:46:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async delete() {
|
|
|
|
if (!this.editMode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-22 18:39:20 +01:00
|
|
|
const message = this.usesKeyConnector ? 'removeUserConfirmationKeyConnector' : 'removeUserConfirmation';
|
2018-07-10 20:46:13 +02:00
|
|
|
const confirmed = await this.platformUtilsService.showDialog(
|
2021-11-22 18:39:20 +01:00
|
|
|
this.i18nService.t(message), this.name, this.i18nService.t('yes'), this.i18nService.t('no'), 'warning'
|
|
|
|
);
|
2018-07-10 20:46:13 +02:00
|
|
|
if (!confirmed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.deletePromise = this.apiService.deleteOrganizationUser(this.organizationId, this.organizationUserId);
|
|
|
|
await this.deletePromise;
|
2021-12-07 20:41:45 +01:00
|
|
|
this.platformUtilsService.showToast('success', null, this.i18nService.t('removedUserId', this.name));
|
2018-07-10 20:46:13 +02:00
|
|
|
this.onDeletedUser.emit();
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
|
|
|
}
|
2018-07-10 20:46:13 +02:00
|
|
|
}
|
|
|
|
}
|