bitwarden-estensione-browser/src/app/layouts/organization-layout.compone...

128 lines
4.4 KiB
TypeScript
Raw Normal View History

2018-06-08 23:08:19 +02:00
import {
Component,
NgZone,
OnDestroy,
2018-07-03 21:11:58 +02:00
OnInit,
2018-06-08 23:08:19 +02:00
} from '@angular/core';
2018-07-03 21:11:58 +02:00
import { ActivatedRoute } from '@angular/router';
import { BroadcasterService } from 'jslib-angular/services/broadcaster.service';
import { ApiService } from 'jslib-common/abstractions/api.service';
import { EnvironmentService } from 'jslib-common/abstractions/environment.service';
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
import { UserService } from 'jslib-common/abstractions/user.service';
2018-07-03 21:11:58 +02:00
import { Organization } from 'jslib-common/models/domain/organization';
2018-06-08 23:08:19 +02:00
const BroadcasterSubscriptionId = 'OrganizationLayoutComponent';
2018-06-08 23:08:19 +02:00
@Component({
selector: 'app-organization-layout',
templateUrl: 'organization-layout.component.html',
})
export class OrganizationLayoutComponent implements OnInit, OnDestroy {
2018-07-03 21:11:58 +02:00
organization: Organization;
businessTokenPromise: Promise<any>;
2018-07-03 21:11:58 +02:00
private organizationId: string;
private businessUrl: string;
2018-07-03 21:11:58 +02:00
constructor(private route: ActivatedRoute, private userService: UserService,
private broadcasterService: BroadcasterService, private ngZone: NgZone,
private apiService: ApiService, private platformUtilsService: PlatformUtilsService,
private environmentService: EnvironmentService) { }
2018-07-03 21:11:58 +02:00
ngOnInit() {
this.businessUrl = this.environmentService.getEnterpriseUrl();
2018-07-06 22:36:51 +02:00
document.body.classList.remove('layout_frontend');
this.route.params.subscribe(async params => {
2018-07-03 21:11:58 +02:00
this.organizationId = params.organizationId;
await this.load();
});
this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => {
this.ngZone.run(async () => {
switch (message.command) {
case 'updatedOrgLicense':
await this.load();
break;
}
});
});
}
ngOnDestroy() {
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
2018-07-03 21:11:58 +02:00
}
async load() {
this.organization = await this.userService.getOrganization(this.organizationId);
}
async goToBusinessPortal() {
if (this.businessTokenPromise != null) {
return;
}
try {
this.businessTokenPromise = this.apiService.getEnterprisePortalSignInToken();
const token = await this.businessTokenPromise;
if (token != null) {
const userId = await this.userService.getUserId();
this.platformUtilsService.launchUri(this.businessUrl + '/login?userId=' + userId +
'&token=' + (window as any).encodeURIComponent(token) + '&organizationId=' + this.organization.id);
}
} catch { }
this.businessTokenPromise = null;
}
get showMenuBar() {
return this.showManageTab || this.showToolsTab || this.organization.isOwner;
}
get showManageTab(): boolean {
return this.organization.canManageUsers ||
this.organization.canViewAllCollections ||
this.organization.canViewAssignedCollections ||
this.organization.canManageGroups ||
this.organization.canManagePolicies ||
this.organization.canAccessEventLogs;
}
get showToolsTab(): boolean {
return this.organization.canAccessImportExport || this.organization.canAccessReports;
}
get showBusinessPortalButton(): boolean {
return this.organization.useBusinessPortal && this.organization.canAccessBusinessPortal;
}
get toolsRoute(): string {
return this.organization.canAccessImportExport ?
'tools/import' :
'tools/exposed-passwords-report';
}
get manageRoute(): string {
let route: string;
switch (true) {
case this.organization.canManageUsers:
route = 'manage/people';
break;
case this.organization.canViewAssignedCollections || this.organization.canViewAllCollections:
route = 'manage/collections';
break;
case this.organization.canManageGroups:
route = 'manage/groups';
break;
case this.organization.canManagePolicies:
route = 'manage/policies';
break;
case this.organization.canAccessEventLogs:
route = 'manage/events';
break;
}
return route;
}
2018-07-03 21:11:58 +02:00
}