From 6246a3d915c9c1e7812977a19562dc6eb2dad3b4 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Thu, 2 May 2024 17:43:31 +0200 Subject: [PATCH] fix(core): update --- ts/00_commitinfo_data.ts | 2 +- ts/classes.zitadelclient.ts | 20 +++++++++++++++- ts/classes.zitadelproject.ts | 33 +++++++++++++++++++++++++++ ts/classes.zitadelprojectrole.ts | 19 +++++++++++++++ ts/classes.zitadeluser.ts | 15 +++++++++--- ts/{zitadel.plugins.ts => plugins.ts} | 0 6 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 ts/classes.zitadelproject.ts create mode 100644 ts/classes.zitadelprojectrole.ts rename ts/{zitadel.plugins.ts => plugins.ts} (100%) diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index ea3cf8b..4a2ed0d 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@apiclient.xyz/zitadel', - version: '1.0.4', + version: '1.0.5', description: 'An unofficial client for interacting with Zitadel API.' } diff --git a/ts/classes.zitadelclient.ts b/ts/classes.zitadelclient.ts index 0f3bedd..ce6b554 100644 --- a/ts/classes.zitadelclient.ts +++ b/ts/classes.zitadelclient.ts @@ -1,5 +1,6 @@ +import { ZitadelProject } from './classes.zitadelproject.js'; import { ZitaldelUser } from './classes.zitadeluser.js'; -import * as plugins from './zitadel.plugins.js'; +import * as plugins from './plugins.js'; export interface IZitadelContructorOptions { url: string; @@ -10,6 +11,7 @@ export class ZitaldelClient { private options: IZitadelContructorOptions; public authClient: plugins.zitadel.AuthServiceClient; public userClient: plugins.zitadel.UserServiceClient; + public managementClient: plugins.zitadel.ManagementServiceClient; constructor(optionsArg: IZitadelContructorOptions) { this.options = optionsArg; @@ -22,6 +24,10 @@ export class ZitaldelClient { this.options.url, plugins.zitadel.createAccessTokenInterceptor(this.options.accessToken) ); + this.managementClient = plugins.zitadel.createManagementClient( + this.options.url, + plugins.zitadel.createAccessTokenInterceptor(this.options.accessToken) + ); } public async listOwnUser() { @@ -34,6 +40,18 @@ export class ZitaldelClient { return zitadelUser; } + public async listProjects() { + const returnProjects: ZitadelProject[] = []; + const response = await this.managementClient.listProjects({}); + for (const projectObject of response.result) { + returnProjects.push(new ZitadelProject(this, { + id: projectObject.id, + name: projectObject.name, + })); + } + return returnProjects; + } + public async listUsers() { const response = await this.userClient.listUsers({}); const returnArray: ZitaldelUser[] = []; diff --git a/ts/classes.zitadelproject.ts b/ts/classes.zitadelproject.ts new file mode 100644 index 0000000..418449b --- /dev/null +++ b/ts/classes.zitadelproject.ts @@ -0,0 +1,33 @@ +import type { ZitaldelClient } from './classes.zitadelclient.js'; +import { ZitadelProjectRole } from './classes.zitadelprojectrole.js'; +import * as plugins from './plugins.js'; + +export class IZitadelProjectData { + id: string; + name: string; +} + +export class ZitadelProject { + ziadelclientRef: ZitaldelClient; + public data: IZitadelProjectData; + + constructor(zitadelclientRefArg: ZitaldelClient, dataArg: IZitadelProjectData) { + this.ziadelclientRef = zitadelclientRefArg; + this.data = dataArg; + } + + public async listProjectRoles() { + const returnRoles: ZitadelProjectRole[] = []; + const response = await this.ziadelclientRef.managementClient.listProjectRoles({ + projectId: this.data.id, + }); + for (const roleObject of response.result) { + returnRoles.push(new ZitadelProjectRole(this.ziadelclientRef, { + project: this, + name: roleObject.displayName, + key: roleObject.key, + })); + } + return returnRoles; + } +} \ No newline at end of file diff --git a/ts/classes.zitadelprojectrole.ts b/ts/classes.zitadelprojectrole.ts new file mode 100644 index 0000000..12ec0a4 --- /dev/null +++ b/ts/classes.zitadelprojectrole.ts @@ -0,0 +1,19 @@ +import type { ZitaldelClient } from './classes.zitadelclient.js'; +import type { ZitadelProject } from './classes.zitadelproject.js'; +import * as plugins from './plugins.js'; + +export interface IZitadelProjectRoleData { + project: ZitadelProject; + key: string; + name: string; +} + +export class ZitadelProjectRole { + ziadelclientRef: ZitaldelClient; + public data: IZitadelProjectRoleData; + + constructor(zitadelclientRefArg: ZitaldelClient, dataArg: IZitadelProjectRoleData) { + this.ziadelclientRef = zitadelclientRefArg; + this.data = dataArg; + } +} \ No newline at end of file diff --git a/ts/classes.zitadeluser.ts b/ts/classes.zitadeluser.ts index 5b937eb..b821da2 100644 --- a/ts/classes.zitadeluser.ts +++ b/ts/classes.zitadeluser.ts @@ -1,5 +1,6 @@ import type { ZitaldelClient } from './classes.zitadelclient.js'; -import * as plugins from './zitadel.plugins.js'; +import type { ZitadelProjectRole } from './classes.zitadelprojectrole.js'; +import * as plugins from './plugins.js'; export interface IZitadelUserData { id: string; @@ -8,13 +9,21 @@ export interface IZitadelUserData { }; export class ZitaldelUser { + // INSTANCE zitadelclientRef: ZitaldelClient; + data: IZitadelUserData; constructor(zitadelclientRefArg: ZitaldelClient, dataArg: IZitadelUserData) { this.zitadelclientRef = zitadelclientRefArg; this.data = dataArg; } - // INSTANCE - data: IZitadelUserData; + public async addRole(projectRole: ZitadelProjectRole) { + this.zitadelclientRef.managementClient.addUserGrant({ + userId: this.data.id, + roleKeys: [projectRole.data.key], + projectId: projectRole.data.project.data.id, + }); + } + } diff --git a/ts/zitadel.plugins.ts b/ts/plugins.ts similarity index 100% rename from ts/zitadel.plugins.ts rename to ts/plugins.ts