From 99cecdb3cac6ca9ef7a9bc79bf9c34d2fa336ab6 Mon Sep 17 00:00:00 2001 From: Jens L Date: Wed, 15 Nov 2023 23:14:30 +0100 Subject: [PATCH] web/admin: contextually add user to group when creating user from group page (#7586) * move related user list to group folder Signed-off-by: Jens Langhammer * contextually add user to group when created from group view page Signed-off-by: Jens Langhammer * add banner Signed-off-by: Jens Langhammer * format Signed-off-by: Jens Langhammer --------- Signed-off-by: Jens Langhammer --- web/src/admin/groups/GroupViewPage.ts | 2 +- .../{users => groups}/RelatedUserList.ts | 25 +++++++++++++++-- web/src/admin/users/ServiceAccountForm.ts | 25 +++++++++++++++-- web/src/admin/users/UserForm.ts | 28 +++++++++++++++---- web/src/admin/users/UserListPage.ts | 2 +- web/xliff/de.xlf | 3 ++ web/xliff/en.xlf | 3 ++ web/xliff/es.xlf | 3 ++ web/xliff/fr.xlf | 3 ++ web/xliff/pl.xlf | 3 ++ web/xliff/pseudo-LOCALE.xlf | 3 ++ web/xliff/tr.xlf | 3 ++ web/xliff/zh-Hans.xlf | 3 ++ web/xliff/zh-Hant.xlf | 3 ++ web/xliff/zh_TW.xlf | 3 ++ 15 files changed, 98 insertions(+), 14 deletions(-) rename web/src/admin/{users => groups}/RelatedUserList.ts (94%) diff --git a/web/src/admin/groups/GroupViewPage.ts b/web/src/admin/groups/GroupViewPage.ts index e3a35663c..3e6cbddcb 100644 --- a/web/src/admin/groups/GroupViewPage.ts +++ b/web/src/admin/groups/GroupViewPage.ts @@ -1,5 +1,5 @@ import "@goauthentik/admin/groups/GroupForm"; -import "@goauthentik/admin/users/RelatedUserList"; +import "@goauthentik/app/admin/groups/RelatedUserList"; import "@goauthentik/app/elements/rbac/ObjectPermissionsPage"; import { DEFAULT_CONFIG } from "@goauthentik/common/api/config"; import { EVENT_REFRESH } from "@goauthentik/common/constants"; diff --git a/web/src/admin/users/RelatedUserList.ts b/web/src/admin/groups/RelatedUserList.ts similarity index 94% rename from web/src/admin/users/RelatedUserList.ts rename to web/src/admin/groups/RelatedUserList.ts index 276b172f8..0023c5515 100644 --- a/web/src/admin/users/RelatedUserList.ts +++ b/web/src/admin/groups/RelatedUserList.ts @@ -24,7 +24,7 @@ import { UserOption } from "@goauthentik/elements/user/utils"; import "@patternfly/elements/pf-tooltip/pf-tooltip.js"; import { msg, str } from "@lit/localize"; -import { CSSResult, TemplateResult, html } from "lit"; +import { CSSResult, TemplateResult, html, nothing } from "lit"; import { customElement, property, state } from "lit/decorators.js"; import { ifDefined } from "lit/directives/if-defined.js"; @@ -402,7 +402,16 @@ export class RelatedUserList extends Table { ${msg("Create")} ${msg("Create User")} - + ${this.targetGroup + ? html` +
+ ${msg( + str`This user will be added to the group "${this.targetGroup.name}".`, + )} +
+ ` + : nothing} + ${msg("Create user")} @@ -415,7 +424,17 @@ export class RelatedUserList extends Table { > ${msg("Create")} ${msg("Create Service account")} - + ${this.targetGroup + ? html` +
+ ${msg( + str`This user will be added to the group "${this.targetGroup.name}".`, + )} +
+ ` + : nothing} + + ${msg("Create Service account")} diff --git a/web/src/admin/users/ServiceAccountForm.ts b/web/src/admin/users/ServiceAccountForm.ts index 96dbb89be..914e5fd51 100644 --- a/web/src/admin/users/ServiceAccountForm.ts +++ b/web/src/admin/users/ServiceAccountForm.ts @@ -4,19 +4,30 @@ import { Form } from "@goauthentik/elements/forms/Form"; import "@goauthentik/elements/forms/HorizontalFormElement"; import { ModalForm } from "@goauthentik/elements/forms/ModalForm"; -import { msg } from "@lit/localize"; +import { msg, str } from "@lit/localize"; import { TemplateResult, html } from "lit"; import { customElement, property } from "lit/decorators.js"; import { ifDefined } from "lit/directives/if-defined.js"; -import { CoreApi, UserServiceAccountRequest, UserServiceAccountResponse } from "@goauthentik/api"; +import { + CoreApi, + Group, + UserServiceAccountRequest, + UserServiceAccountResponse, +} from "@goauthentik/api"; -@customElement("ak-user-service-account") +@customElement("ak-user-service-account-form") export class ServiceAccountForm extends Form { @property({ attribute: false }) result?: UserServiceAccountResponse; + @property({ attribute: false }) + group?: Group; + getSuccessMessage(): string { + if (this.group) { + return msg(str`Successfully created user and added to group ${this.group.name}`); + } return msg("Successfully created user."); } @@ -26,6 +37,14 @@ export class ServiceAccountForm extends Form { }); this.result = result; (this.parentElement as ModalForm).showSubmitButton = false; + if (this.group) { + await new CoreApi(DEFAULT_CONFIG).coreGroupsAddUserCreate({ + groupUuid: this.group.pk, + userAccountRequest: { + pk: this.result.userPk, + }, + }); + } return result; } diff --git a/web/src/admin/users/UserForm.ts b/web/src/admin/users/UserForm.ts index 13d2ac141..061fd6f56 100644 --- a/web/src/admin/users/UserForm.ts +++ b/web/src/admin/users/UserForm.ts @@ -8,15 +8,18 @@ import { ModelForm } from "@goauthentik/elements/forms/ModelForm"; import "@goauthentik/elements/forms/Radio"; import YAML from "yaml"; -import { msg } from "@lit/localize"; +import { msg, str } from "@lit/localize"; import { CSSResult, TemplateResult, css, html } from "lit"; -import { customElement } from "lit/decorators.js"; +import { customElement, property } from "lit/decorators.js"; import { ifDefined } from "lit/directives/if-defined.js"; -import { CoreApi, User, UserTypeEnum } from "@goauthentik/api"; +import { CoreApi, Group, User, UserTypeEnum } from "@goauthentik/api"; @customElement("ak-user-form") export class UserForm extends ModelForm { + @property({ attribute: false }) + group?: Group; + static get defaultUserAttributes(): { [key: string]: unknown } { return {}; } @@ -42,6 +45,9 @@ export class UserForm extends ModelForm { if (this.instance) { return msg("Successfully updated user."); } else { + if (this.group) { + return msg(str`Successfully created user and added to group ${this.group.name}`); + } return msg("Successfully created user."); } } @@ -50,21 +56,31 @@ export class UserForm extends ModelForm { if (data.attributes === null) { data.attributes = UserForm.defaultUserAttributes; } + let user; if (this.instance?.pk) { - return new CoreApi(DEFAULT_CONFIG).coreUsersPartialUpdate({ + user = await new CoreApi(DEFAULT_CONFIG).coreUsersPartialUpdate({ id: this.instance.pk, patchedUserRequest: data, }); } else { data.groups = []; - return new CoreApi(DEFAULT_CONFIG).coreUsersCreate({ + user = await new CoreApi(DEFAULT_CONFIG).coreUsersCreate({ userRequest: data, }); } + if (this.group) { + await new CoreApi(DEFAULT_CONFIG).coreGroupsAddUserCreate({ + groupUuid: this.group.pk, + userAccountRequest: { + pk: user.pk, + }, + }); + } + return user; } renderForm(): TemplateResult { - return html` { ${msg("Create")} ${msg("Create Service account")} - + diff --git a/web/xliff/de.xlf b/web/xliff/de.xlf index aa0b11fcd..81b1fa4de 100644 --- a/web/xliff/de.xlf +++ b/web/xliff/de.xlf @@ -6063,6 +6063,9 @@ Bindings to groups/users are checked against the user of the event. 4: Very unguessable: strong protection from offline slow-hash scenario. (guesses >= 10^10) + + + Successfully created user and added to group diff --git a/web/xliff/en.xlf b/web/xliff/en.xlf index 5dc9ddbc7..25b7648bc 100644 --- a/web/xliff/en.xlf +++ b/web/xliff/en.xlf @@ -6340,6 +6340,9 @@ Bindings to groups/users are checked against the user of the event. 4: Very unguessable: strong protection from offline slow-hash scenario. (guesses >= 10^10) + + + Successfully created user and added to group diff --git a/web/xliff/es.xlf b/web/xliff/es.xlf index 69575c1bb..2b1b72ed4 100644 --- a/web/xliff/es.xlf +++ b/web/xliff/es.xlf @@ -5979,6 +5979,9 @@ Bindings to groups/users are checked against the user of the event. 4: Very unguessable: strong protection from offline slow-hash scenario. (guesses >= 10^10) + + + Successfully created user and added to group diff --git a/web/xliff/fr.xlf b/web/xliff/fr.xlf index fe09b02df..5e6ab9a15 100644 --- a/web/xliff/fr.xlf +++ b/web/xliff/fr.xlf @@ -7965,6 +7965,9 @@ Les liaisons avec les groupes/utilisateurs sont vérifiées par rapport à l'uti 4: Very unguessable: strong protection from offline slow-hash scenario. (guesses >= 10^10) + + + Successfully created user and added to group diff --git a/web/xliff/pl.xlf b/web/xliff/pl.xlf index f83eefb02..aa0459d08 100644 --- a/web/xliff/pl.xlf +++ b/web/xliff/pl.xlf @@ -6187,6 +6187,9 @@ Bindings to groups/users are checked against the user of the event. 4: Very unguessable: strong protection from offline slow-hash scenario. (guesses >= 10^10) + + + Successfully created user and added to group diff --git a/web/xliff/pseudo-LOCALE.xlf b/web/xliff/pseudo-LOCALE.xlf index 9824b2123..b7f8bfef4 100644 --- a/web/xliff/pseudo-LOCALE.xlf +++ b/web/xliff/pseudo-LOCALE.xlf @@ -7865,4 +7865,7 @@ Bindings to groups/users are checked against the user of the event. 4: Very unguessable: strong protection from offline slow-hash scenario. (guesses >= 10^10) + + Successfully created user and added to group + diff --git a/web/xliff/tr.xlf b/web/xliff/tr.xlf index f53087166..1398f0387 100644 --- a/web/xliff/tr.xlf +++ b/web/xliff/tr.xlf @@ -5972,6 +5972,9 @@ Bindings to groups/users are checked against the user of the event. 4: Very unguessable: strong protection from offline slow-hash scenario. (guesses >= 10^10) + + + Successfully created user and added to group diff --git a/web/xliff/zh-Hans.xlf b/web/xliff/zh-Hans.xlf index f729ad3ce..c90510329 100644 --- a/web/xliff/zh-Hans.xlf +++ b/web/xliff/zh-Hans.xlf @@ -7967,6 +7967,9 @@ Bindings to groups/users are checked against the user of the event. 4: Very unguessable: strong protection from offline slow-hash scenario. (guesses >= 10^10) + + + Successfully created user and added to group diff --git a/web/xliff/zh-Hant.xlf b/web/xliff/zh-Hant.xlf index 6ae3a27a2..21433dc1b 100644 --- a/web/xliff/zh-Hant.xlf +++ b/web/xliff/zh-Hant.xlf @@ -6020,6 +6020,9 @@ Bindings to groups/users are checked against the user of the event. 4: Very unguessable: strong protection from offline slow-hash scenario. (guesses >= 10^10) + + + Successfully created user and added to group diff --git a/web/xliff/zh_TW.xlf b/web/xliff/zh_TW.xlf index 4f86131ad..3b96980ee 100644 --- a/web/xliff/zh_TW.xlf +++ b/web/xliff/zh_TW.xlf @@ -6019,6 +6019,9 @@ Bindings to groups/users are checked against the user of the event. 4: Very unguessable: strong protection from offline slow-hash scenario. (guesses >= 10^10) + + + Successfully created user and added to group