Files
zitadel/console/src/app/pages/user-grant-create/user-grant-create.component.ts
T
Max PeintnerandGitHub 06e3330d2e feat: console flat navigation, settings (#3581)
* instance routing

* instance naming

* org list

* rm isonsystem

* breadcrumb  type

* routing

* instance members

* fragment refresh org

* settings pages

* settings list, sidenav grouping, i18n

* org-settings, policy changes

* lint

* grid

* rename grid

* fallback to general

* cleanup

* general settings, remove cards

* sidenav for settings, label policy

* i18n

* header, nav backbuild

* general, project nav rehaul

* login text background adapt

* org nav anim

* org, instance settings, fix policy layout, roles

* i18n, active route for project

* lint
2022-05-09 15:01:36 +02:00

221 lines
6.9 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 STEPS: number = 2; // project, roles
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 = '';
if (this.grantedProject?.grantId) {
grantId = this.grantedProject.grantId;
}
const promu = this.userIds.map((id) =>
this.userService.addUserGrant(id, this.rolesList, (this.project as Project.AsObject).id, 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(roles: Role.AsObject[]): void {
this.rolesList = roles.map((role) => role.key);
}
public next(): void {
this.currentCreateStep++;
}
public previous(): void {
this.currentCreateStep--;
}
public ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
}