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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

91 lines
2.8 KiB
TypeScript
Raw Normal View History

2018-07-10 21:03:13 +02:00
import { Component, EventEmitter, Input, OnInit, Output } from "@angular/core";
2022-06-14 17:10:53 +02:00
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/abstractions/log.service";
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
import { Utils } from "@bitwarden/common/misc/utils";
import { OrganizationUserUpdateGroupsRequest } from "@bitwarden/common/models/request/organizationUserUpdateGroupsRequest";
import { GroupResponse } from "@bitwarden/common/models/response/groupResponse";
2018-07-10 21:03:13 +02:00
@Component({
selector: "app-user-groups",
templateUrl: "user-groups.component.html",
})
export class UserGroupsComponent implements OnInit {
@Input() name: string;
@Input() organizationUserId: string;
@Input() organizationId: string;
@Output() onSavedUser = new EventEmitter();
loading = true;
groups: GroupResponse[] = [];
formPromise: Promise<any>;
constructor(
private apiService: ApiService,
private i18nService: I18nService,
2021-12-07 20:41:45 +01:00
private platformUtilsService: PlatformUtilsService,
private logService: LogService
) {}
2018-07-10 21:03:13 +02:00
async ngOnInit() {
const groupsResponse = await this.apiService.getGroups(this.organizationId);
const groups = groupsResponse.data.map((r) => r);
2018-07-23 16:57:25 +02:00
groups.sort(Utils.getSortFunction(this.i18nService, "name"));
this.groups = groups;
2018-07-10 21:03:13 +02:00
try {
const userGroups = await this.apiService.getOrganizationUserGroups(
this.organizationId,
this.organizationUserId
);
if (userGroups != null && this.groups != null) {
userGroups.forEach((ug) => {
const group = this.groups.filter((g) => g.id === ug);
2018-07-10 21:03:13 +02:00
if (group != null && group.length > 0) {
(group[0] as any).checked = true;
}
});
}
} catch (e) {
this.logService.error(e);
2018-07-10 21:03:13 +02:00
}
this.loading = false;
}
check(g: GroupResponse, select?: boolean) {
(g as any).checked = select == null ? !(g as any).checked : select;
2018-07-10 21:03:13 +02:00
if (!(g as any).checked) {
(g as any).readOnly = false;
2018-07-10 21:03:13 +02:00
}
2021-12-17 15:57:11 +01:00
}
2018-07-10 21:03:13 +02:00
selectAll(select: boolean) {
this.groups.forEach((g) => this.check(g, select));
2021-12-17 15:57:11 +01:00
}
2018-07-10 21:03:13 +02:00
async submit() {
const request = new OrganizationUserUpdateGroupsRequest();
request.groupIds = this.groups.filter((g) => (g as any).checked).map((g) => g.id);
2018-07-10 21:03:13 +02:00
try {
this.formPromise = this.apiService.putOrganizationUserGroups(
this.organizationId,
this.organizationUserId,
request
);
await this.formPromise;
2021-12-07 20:41:45 +01:00
this.platformUtilsService.showToast(
"success",
null,
this.i18nService.t("editedGroupsForUser", this.name)
);
2018-07-10 21:03:13 +02:00
this.onSavedUser.emit();
} catch (e) {
this.logService.error(e);
2018-07-10 21:03:13 +02:00
}
2021-12-17 15:57:11 +01:00
}
2018-07-10 21:03:13 +02:00
}