Files
zitadel/console/src/app/pages/user-grant-create/user-grant-create.component.ts
T
Max PeintnerGitHubLivio SpringStefan BenzElio BischofFlorian Forsterdependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
a4ddedb5f4 fix(console): role table pagination, selection (#4611)
* fix: console role table pagination, selection

* remove changedetector

* chore(deps-dev): bump eslint from 8.24.0 to 8.26.0 in /console (#4601)

* feat: allow disabling the mfa setup prompt (#4575)

* feat: allow disabling the mfa setup prompt

* e2e: disable mfa prompt

Co-authored-by: Max Peintner <[email protected]>

* fix(machine): delete domain policy dependency and restructure functions (#4605)

* fix(machine): delete domain policy dependency and restructure functions

* fix(machine): delete domain policy dependency and restructure functions

* fix(machine): move check for username and name

* fix: correct unit test for machine

Co-authored-by: Livio Spring <[email protected]>

* docs(deploy): fix lb example (#4615)

* fix: instance remove (#4602)

* fix: join on instanceIDs in queries (#4612)

* fix(console): login text setters (#4600)

* fix: login text mapping

* fix mapping

Co-authored-by: Elio Bischof <[email protected]>
Co-authored-by: Livio Spring <[email protected]>

* fix: nil pointer on jwt idps in actions (#4619)

* chore: improve wording (#4545)

* chore(deps-dev): bump eslint from 8.24.0 to 8.26.0 in /console

Bumps [eslint](https://github.com/eslint/eslint) from 8.24.0 to 8.26.0.
- [Release notes](https://github.com/eslint/eslint/releases)
- [Changelog](https://github.com/eslint/eslint/blob/main/CHANGELOG.md)
- [Commits](https://github.com/eslint/eslint/compare/v8.24.0...v8.26.0)

---
updated-dependencies:
- dependency-name: eslint
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Livio Spring <[email protected]>
Co-authored-by: Max Peintner <[email protected]>
Co-authored-by: Stefan Benz <[email protected]>
Co-authored-by: Elio Bischof <[email protected]>
Co-authored-by: Florian Forster <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* chore(deps): bump google-protobuf from 3.21.0 to 3.21.2 in /console (#4538)

* feat: allow disabling the mfa setup prompt (#4575)

* feat: allow disabling the mfa setup prompt

* e2e: disable mfa prompt

Co-authored-by: Max Peintner <[email protected]>

* fix(machine): delete domain policy dependency and restructure functions (#4605)

* fix(machine): delete domain policy dependency and restructure functions

* fix(machine): delete domain policy dependency and restructure functions

* fix(machine): move check for username and name

* fix: correct unit test for machine

Co-authored-by: Livio Spring <[email protected]>

* docs(deploy): fix lb example (#4615)

* fix: instance remove (#4602)

* fix: join on instanceIDs in queries (#4612)

* fix(console): login text setters (#4600)

* fix: login text mapping

* fix mapping

Co-authored-by: Elio Bischof <[email protected]>
Co-authored-by: Livio Spring <[email protected]>

* fix: nil pointer on jwt idps in actions (#4619)

* chore: improve wording (#4545)

* chore(deps): bump google-protobuf from 3.21.0 to 3.21.2 in /console

Bumps [google-protobuf](https://github.com/protocolbuffers/protobuf-javascript) from 3.21.0 to 3.21.2.
- [Release notes](https://github.com/protocolbuffers/protobuf-javascript/releases)
- [Changelog](https://github.com/protocolbuffers/protobuf-javascript/blob/main/protobuf_javascript_release.bzl)
- [Commits](https://github.com/protocolbuffers/protobuf-javascript/compare/v3.21.0...v3.21.2)

---
updated-dependencies:
- dependency-name: google-protobuf
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Livio Spring <[email protected]>
Co-authored-by: Max Peintner <[email protected]>
Co-authored-by: Stefan Benz <[email protected]>
Co-authored-by: Elio Bischof <[email protected]>
Co-authored-by: Florian Forster <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* cli core

* material cdk

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Livio Spring <[email protected]>
Co-authored-by: Stefan Benz <[email protected]>
Co-authored-by: Elio Bischof <[email protected]>
Co-authored-by: Florian Forster <[email protected]>
2022-11-09 09:16:53 +01:00

228 lines
7.1 KiB
TypeScript

import { Location } from '@angular/common';
import { Component, OnDestroy } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { Subject, takeUntil } from 'rxjs';
import { ProjectType } from 'src/app/modules/project-members/project-members-datasource';
import { UserTarget } from 'src/app/modules/search-user-autocomplete/search-user-autocomplete.component';
import { UserGrantContext } from 'src/app/modules/user-grants/user-grants-datasource';
import { Org } from 'src/app/proto/generated/zitadel/org_pb';
import { GrantedProject, Project, Role } from 'src/app/proto/generated/zitadel/project_pb';
import { User } from 'src/app/proto/generated/zitadel/user_pb';
import { Breadcrumb, BreadcrumbService, BreadcrumbType } from 'src/app/services/breadcrumb.service';
import { ManagementService } from 'src/app/services/mgmt.service';
import { StorageKey, StorageLocation, StorageService } from 'src/app/services/storage.service';
import { ToastService } from 'src/app/services/toast.service';
@Component({
selector: 'cnsl-user-grant-create',
templateUrl: './user-grant-create.component.html',
styleUrls: ['./user-grant-create.component.scss'],
})
export class UserGrantCreateComponent implements OnDestroy {
public context!: UserGrantContext;
public org?: Org.AsObject;
public userIds: string[] = [];
public project?: Project.AsObject;
public grantedProject?: GrantedProject.AsObject;
public rolesList: string[] = [];
public createSteps: number = 2;
public currentCreateStep: number = 1;
public UserGrantContext: any = UserGrantContext;
public user?: User.AsObject;
public UserTarget: any = UserTarget;
public editState: boolean = false;
private destroy$: Subject<void> = new Subject();
constructor(
private userService: ManagementService,
private toast: ToastService,
private _location: Location,
private route: ActivatedRoute,
private mgmtService: ManagementService,
private storage: StorageService,
breadcrumbService: BreadcrumbService,
) {
breadcrumbService.setBreadcrumb([
new Breadcrumb({
type: BreadcrumbType.ORG,
routerLink: ['/org'],
}),
]);
this.route.params.pipe(takeUntil(this.destroy$)).subscribe((params: Params) => {
const { projectid, grantid, userid } = params;
this.context = UserGrantContext.NONE;
this.userIds = userid ? [userid] : [];
if (projectid && !grantid) {
this.context = UserGrantContext.OWNED_PROJECT;
this.mgmtService
.getProjectByID(projectid)
.then((resp) => {
if (resp.project) {
this.project = resp.project;
}
})
.catch((error: any) => {
this.toast.showError(error);
});
} else if (projectid && grantid) {
this.context = UserGrantContext.GRANTED_PROJECT;
this.mgmtService
.getGrantedProjectByID(projectid, grantid)
.then((resp) => {
if (resp.grantedProject) {
this.grantedProject = resp.grantedProject;
}
})
.catch((error: any) => {
this.toast.showError(error);
});
} else if (this.userIds && this.userIds.length === 1) {
this.context = UserGrantContext.USER;
this.mgmtService
.getUserByID(this.userIds[0])
.then((resp) => {
if (resp.user) {
this.user = resp.user;
}
})
.catch((error: any) => {
this.toast.showError(error);
});
}
});
const temporg = this.storage.getItem<Org.AsObject>(StorageKey.organization, StorageLocation.session);
if (temporg) {
this.org = temporg;
}
}
public close(): void {
this._location.back();
}
public addGrant(): void {
switch (this.context) {
case UserGrantContext.OWNED_PROJECT:
const prom = this.userIds.map((id) => this.userService.addUserGrant(id, this.rolesList, this.project?.id));
Promise.all(prom)
.then(() => {
this.toast.showInfo('GRANTS.TOAST.UPDATED', true);
this.close();
})
.catch((error: any) => {
this.toast.showError(error);
this.close();
});
break;
case UserGrantContext.GRANTED_PROJECT:
const promp = this.userIds.map((id) =>
this.userService.addUserGrant(id, this.rolesList, this.grantedProject?.projectId, this.grantedProject?.grantId),
);
Promise.all(promp)
.then(() => {
this.toast.showInfo('GRANTS.TOAST.UPDATED', true);
this.close();
})
.catch((error: any) => {
this.toast.showError(error);
this.close();
});
break;
case UserGrantContext.USER:
let grantId: string = '';
let grantedProjectId: string = '';
if (this.grantedProject?.grantId) {
grantId = this.grantedProject.grantId;
grantedProjectId = this.grantedProject.projectId;
}
const promu = this.userIds.map((id) =>
this.userService.addUserGrant(
id,
this.rolesList,
this.project?.id ? this.project.id : grantedProjectId ? grantedProjectId : '',
grantId,
),
);
Promise.all(promu)
.then(() => {
this.toast.showInfo('GRANTS.TOAST.UPDATED', true);
this.close();
})
.catch((error: any) => {
this.toast.showError(error);
this.close();
});
break;
case UserGrantContext.NONE:
let tempGrantId: string = '';
if (this.grantedProject?.grantId) {
tempGrantId = this.grantedProject.grantId;
}
const promn = this.userIds.map((id) =>
this.userService.addUserGrant(
id,
this.rolesList,
this.project ? this.project.id : this.grantedProject ? this.grantedProject.projectId : '',
tempGrantId,
),
);
Promise.all(promn)
.then(() => {
this.toast.showInfo('GRANTS.TOAST.UPDATED', true);
this.close();
})
.catch((error: any) => {
this.toast.showError(error);
this.close();
});
break;
}
}
public selectProject(project: Project.AsObject | GrantedProject.AsObject, type: ProjectType): void {
if (type === ProjectType.PROJECTTYPE_OWNED) {
this.project = project as Project.AsObject;
} else if (type === ProjectType.PROJECTTYPE_GRANTED) {
this.grantedProject = project as GrantedProject.AsObject;
}
}
public selectUsers(user: User.AsObject[]): void {
if (user && user.length) {
this.userIds = (user as User.AsObject[]).map((u) => u.id);
}
}
public selectRoles(roleKeys: string[]): void {
this.rolesList = roleKeys;
}
public next(): void {
this.currentCreateStep++;
}
public previous(): void {
this.currentCreateStep--;
}
public ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
}