2018-07-04 15:55:52 +02:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
Input,
|
|
|
|
OnInit,
|
|
|
|
} from '@angular/core';
|
|
|
|
|
|
|
|
import { ToasterService } from 'angular2-toaster';
|
|
|
|
|
|
|
|
import { ApiService } from 'jslib/abstractions/api.service';
|
2021-04-05 16:48:46 +02:00
|
|
|
import { CryptoService } from 'jslib/abstractions/crypto.service';
|
2018-07-04 15:55:52 +02:00
|
|
|
import { I18nService } from 'jslib/abstractions/i18n.service';
|
|
|
|
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
|
|
|
import { SyncService } from 'jslib/abstractions/sync.service';
|
|
|
|
import { UserService } from 'jslib/abstractions/user.service';
|
|
|
|
|
|
|
|
import { Organization } from 'jslib/models/domain/organization';
|
|
|
|
|
2018-07-06 21:01:23 +02:00
|
|
|
import { Utils } from 'jslib/misc/utils';
|
|
|
|
|
2021-04-05 16:48:46 +02:00
|
|
|
import { OrganizationUserResetPasswordEnrollmentRequest } from 'jslib/models/request/organizationUserResetPasswordEnrollmentRequest';
|
|
|
|
|
2018-07-04 15:55:52 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'app-organizations',
|
|
|
|
templateUrl: 'organizations.component.html',
|
|
|
|
})
|
|
|
|
export class OrganizationsComponent implements OnInit {
|
|
|
|
@Input() vault = false;
|
|
|
|
|
|
|
|
organizations: Organization[];
|
|
|
|
loaded: boolean = false;
|
|
|
|
actionPromise: Promise<any>;
|
2021-04-22 16:43:51 +02:00
|
|
|
// TODO Remove feature flag once ready for general release
|
|
|
|
resetPasswordFeatureFlag = false;
|
2018-07-04 15:55:52 +02:00
|
|
|
|
|
|
|
constructor(private userService: UserService, private platformUtilsService: PlatformUtilsService,
|
|
|
|
private i18nService: I18nService, private apiService: ApiService,
|
2021-04-14 23:43:40 +02:00
|
|
|
private toasterService: ToasterService, private syncService: SyncService,
|
|
|
|
private cryptoService: CryptoService) { }
|
2018-07-04 15:55:52 +02:00
|
|
|
|
|
|
|
async ngOnInit() {
|
2018-07-12 15:14:20 +02:00
|
|
|
if (!this.vault) {
|
2020-08-27 17:44:04 +02:00
|
|
|
await this.syncService.fullSync(true);
|
2018-07-11 17:39:48 +02:00
|
|
|
await this.load();
|
|
|
|
}
|
2018-07-04 15:55:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
2018-07-06 20:19:49 +02:00
|
|
|
const orgs = await this.userService.getAllOrganizations();
|
2018-07-06 21:01:23 +02:00
|
|
|
orgs.sort(Utils.getSortFunction(this.i18nService, 'name'));
|
2018-07-06 20:19:49 +02:00
|
|
|
this.organizations = orgs;
|
2018-07-04 15:55:52 +02:00
|
|
|
this.loaded = true;
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:44:04 +02:00
|
|
|
async unlinkSso(org: Organization) {
|
|
|
|
const confirmed = await this.platformUtilsService.showDialog(
|
|
|
|
'Are you sure you want to unlink SSO for this organization?', org.name,
|
|
|
|
this.i18nService.t('yes'), this.i18nService.t('no'), 'warning');
|
|
|
|
if (!confirmed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.actionPromise = this.apiService.deleteSsoUser(org.id).then(() => {
|
|
|
|
return this.syncService.fullSync(true);
|
|
|
|
});
|
|
|
|
await this.actionPromise;
|
|
|
|
this.toasterService.popAsync('success', null, 'Unlinked SSO');
|
|
|
|
await this.load();
|
|
|
|
} catch { }
|
|
|
|
}
|
|
|
|
|
2018-07-04 15:55:52 +02:00
|
|
|
async leave(org: Organization) {
|
|
|
|
const confirmed = await this.platformUtilsService.showDialog(
|
|
|
|
this.i18nService.t('leaveOrganizationConfirmation'), org.name,
|
|
|
|
this.i18nService.t('yes'), this.i18nService.t('no'), 'warning');
|
|
|
|
if (!confirmed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.actionPromise = this.apiService.postLeaveOrganization(org.id).then(() => {
|
|
|
|
return this.syncService.fullSync(true);
|
|
|
|
});
|
|
|
|
await this.actionPromise;
|
|
|
|
this.toasterService.popAsync('success', null, this.i18nService.t('leftOrganization'));
|
|
|
|
await this.load();
|
|
|
|
} catch { }
|
|
|
|
}
|
2021-04-05 16:48:46 +02:00
|
|
|
|
|
|
|
async toggleResetPasswordEnrollment(org: Organization) {
|
2021-04-22 16:43:51 +02:00
|
|
|
// Feature Flag
|
|
|
|
if (!this.resetPasswordFeatureFlag) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-05 16:48:46 +02:00
|
|
|
// Set variables
|
|
|
|
let keyString: string = null;
|
|
|
|
let toastStringRef = 'withdrawPasswordResetSuccess';
|
|
|
|
|
|
|
|
// Enroll - encrpyt user's encKey.key with organization key
|
2021-05-25 19:24:09 +02:00
|
|
|
if (!org.resetPasswordEnrolled) {
|
2021-04-05 16:48:46 +02:00
|
|
|
const encKey = await this.cryptoService.getEncKey();
|
|
|
|
const orgSymKey = await this.cryptoService.getOrgKey(org.id);
|
|
|
|
const encryptedKey = await this.cryptoService.encrypt(encKey.key, orgSymKey);
|
|
|
|
keyString = encryptedKey.encryptedString;
|
|
|
|
toastStringRef = 'enrollPasswordResetSuccess';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create/Execute request
|
|
|
|
try {
|
|
|
|
const request = new OrganizationUserResetPasswordEnrollmentRequest();
|
|
|
|
request.resetPasswordKey = keyString;
|
|
|
|
this.actionPromise = this.apiService.putOrganizationUserResetPasswordEnrollment(org.id, org.userId, request)
|
|
|
|
.then(() => {
|
|
|
|
return this.syncService.fullSync(true);
|
|
|
|
});
|
|
|
|
await this.actionPromise;
|
|
|
|
this.platformUtilsService.showToast('success', null, this.i18nService.t(toastStringRef));
|
|
|
|
await this.load();
|
|
|
|
} catch { }
|
|
|
|
}
|
2018-07-04 15:55:52 +02:00
|
|
|
}
|