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

139 lines
4.8 KiB
TypeScript
Raw Normal View History

import {
Component,
2018-07-09 22:27:54 +02:00
ComponentFactoryResolver,
OnInit,
2018-07-09 22:27:54 +02:00
ViewChild,
ViewContainerRef,
} from '@angular/core';
import {
ActivatedRoute,
Router,
} from '@angular/router';
2018-07-09 22:27:54 +02:00
import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2';
import { ApiService } from 'jslib/abstractions/api.service';
2018-07-06 20:22:20 +02:00
import { I18nService } from 'jslib/abstractions/i18n.service';
2018-07-09 22:27:54 +02:00
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
import { UserService } from 'jslib/abstractions/user.service';
2018-07-06 18:55:59 +02:00
import { GroupResponse } from 'jslib/models/response/groupResponse';
2018-07-06 16:21:08 +02:00
2018-07-06 21:01:23 +02:00
import { Utils } from 'jslib/misc/utils';
2018-07-09 22:27:54 +02:00
import { ModalComponent } from '../../modal.component';
2018-07-10 05:48:26 +02:00
import { EntityUsersComponent } from './entity-users.component';
2018-07-09 22:27:54 +02:00
import { GroupAddEditComponent } from './group-add-edit.component';
2018-07-06 16:21:08 +02:00
@Component({
selector: 'app-org-groups',
templateUrl: 'groups.component.html',
})
export class GroupsComponent implements OnInit {
2018-07-09 22:27:54 +02:00
@ViewChild('addEdit', { read: ViewContainerRef }) addEditModalRef: ViewContainerRef;
2018-07-10 05:48:26 +02:00
@ViewChild('usersTemplate', { read: ViewContainerRef }) usersModalRef: ViewContainerRef;
2018-07-09 22:27:54 +02:00
loading = true;
organizationId: string;
groups: GroupResponse[];
searchText: string;
2018-07-09 22:27:54 +02:00
private modal: ModalComponent = null;
2018-07-06 20:22:20 +02:00
constructor(private apiService: ApiService, private route: ActivatedRoute,
2018-07-09 22:27:54 +02:00
private i18nService: I18nService, private componentFactoryResolver: ComponentFactoryResolver,
private analytics: Angulartics2, private toasterService: ToasterService,
private platformUtilsService: PlatformUtilsService, private userService: UserService,
private router: Router) { }
async ngOnInit() {
this.route.parent.parent.params.subscribe(async (params) => {
this.organizationId = params.organizationId;
const organization = await this.userService.getOrganization(this.organizationId);
if (organization == null || !organization.useGroups) {
this.router.navigate(['/organizations', this.organizationId]);
return;
}
2018-07-07 05:08:10 +02:00
await this.load();
});
}
async load() {
const response = await this.apiService.getGroups(this.organizationId);
2018-07-06 20:22:20 +02:00
const groups = response.data != null && response.data.length > 0 ? response.data : [];
2018-07-06 21:01:23 +02:00
groups.sort(Utils.getSortFunction(this.i18nService, 'name'));
2018-07-06 20:22:20 +02:00
this.groups = groups;
this.loading = false;
}
2018-07-09 22:27:54 +02:00
edit(group: GroupResponse) {
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<GroupAddEditComponent>(
GroupAddEditComponent, this.addEditModalRef);
childComponent.organizationId = this.organizationId;
childComponent.groupId = group != null ? group.id : null;
childComponent.onSavedGroup.subscribe(() => {
this.modal.close();
this.load();
});
childComponent.onDeletedGroup.subscribe(() => {
this.modal.close();
this.load();
});
this.modal.onClosed.subscribe(() => {
this.modal = null;
});
}
add() {
this.edit(null);
}
async delete(group: GroupResponse) {
const confirmed = await this.platformUtilsService.showDialog(
this.i18nService.t('deleteGroupConfirmation'), group.name,
this.i18nService.t('yes'), this.i18nService.t('no'), 'warning');
if (!confirmed) {
return false;
}
try {
await this.apiService.deleteGroup(this.organizationId, group.id);
this.analytics.eventTrack.next({ action: 'Deleted Group' });
2018-07-10 14:39:05 +02:00
this.toasterService.popAsync('success', null, this.i18nService.t('deletedGroupId', group.name));
2018-07-10 05:48:26 +02:00
const index = this.groups.indexOf(group);
if (index > -1) {
this.groups.splice(index, 1);
}
2018-07-09 22:27:54 +02:00
} catch { }
}
2018-07-10 05:48:26 +02:00
users(group: GroupResponse) {
if (this.modal != null) {
this.modal.close();
}
const factory = this.componentFactoryResolver.resolveComponentFactory(ModalComponent);
this.modal = this.usersModalRef.createComponent(factory).instance;
const childComponent = this.modal.show<EntityUsersComponent>(
EntityUsersComponent, this.usersModalRef);
childComponent.organizationId = this.organizationId;
childComponent.entity = 'group';
childComponent.entityId = group.id;
childComponent.entityName = group.name;
this.modal.onClosed.subscribe(() => {
this.modal = null;
});
}
}