web/admin: remove groups and users from users and group form to prevent accidental removal when updating
Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
		@ -7,7 +7,6 @@ import "@goauthentik/elements/chips/ChipGroup";
 | 
			
		||||
import "@goauthentik/elements/forms/HorizontalFormElement";
 | 
			
		||||
import { ModelForm } from "@goauthentik/elements/forms/ModelForm";
 | 
			
		||||
import "@goauthentik/elements/forms/SearchSelect";
 | 
			
		||||
import { UserOption } from "@goauthentik/elements/user/utils";
 | 
			
		||||
import YAML from "yaml";
 | 
			
		||||
 | 
			
		||||
import { t } from "@lingui/macro";
 | 
			
		||||
@ -15,9 +14,8 @@ import { t } from "@lingui/macro";
 | 
			
		||||
import { CSSResult, TemplateResult, css, html } from "lit";
 | 
			
		||||
import { customElement } from "lit/decorators.js";
 | 
			
		||||
import { ifDefined } from "lit/directives/if-defined.js";
 | 
			
		||||
import { until } from "lit/directives/until.js";
 | 
			
		||||
 | 
			
		||||
import { CoreApi, CoreGroupsListRequest, Group, User } from "@goauthentik/api";
 | 
			
		||||
import { CoreApi, CoreGroupsListRequest, Group } from "@goauthentik/api";
 | 
			
		||||
 | 
			
		||||
@customElement("ak-group-form")
 | 
			
		||||
export class GroupForm extends ModelForm<Group, string> {
 | 
			
		||||
@ -48,9 +46,9 @@ export class GroupForm extends ModelForm<Group, string> {
 | 
			
		||||
 | 
			
		||||
    send = (data: Group): Promise<Group> => {
 | 
			
		||||
        if (this.instance?.pk) {
 | 
			
		||||
            return new CoreApi(DEFAULT_CONFIG).coreGroupsUpdate({
 | 
			
		||||
            return new CoreApi(DEFAULT_CONFIG).coreGroupsPartialUpdate({
 | 
			
		||||
                groupUuid: this.instance.pk,
 | 
			
		||||
                groupRequest: data,
 | 
			
		||||
                patchedGroupRequest: data,
 | 
			
		||||
            });
 | 
			
		||||
        } else {
 | 
			
		||||
            data.users = Array.from(this.instance?.users || []);
 | 
			
		||||
@ -113,63 +111,6 @@ export class GroupForm extends ModelForm<Group, string> {
 | 
			
		||||
                >
 | 
			
		||||
                </ak-search-select>
 | 
			
		||||
            </ak-form-element-horizontal>
 | 
			
		||||
            <ak-form-element-horizontal label=${t`Members`} name="users">
 | 
			
		||||
                <div class="pf-c-input-group">
 | 
			
		||||
                    <ak-group-member-select-table
 | 
			
		||||
                        .confirm=${(items: User[]) => {
 | 
			
		||||
                            // Because the model only has the IDs, map the user list to IDs
 | 
			
		||||
                            const ids = items.map((u) => u.pk || 0);
 | 
			
		||||
                            if (!this.instance) this.instance = {} as Group;
 | 
			
		||||
                            this.instance.users = Array.from(this.instance?.users || []).concat(
 | 
			
		||||
                                ids,
 | 
			
		||||
                            );
 | 
			
		||||
                            this.requestUpdate();
 | 
			
		||||
                            return Promise.resolve();
 | 
			
		||||
                        }}
 | 
			
		||||
                    >
 | 
			
		||||
                        <button slot="trigger" class="pf-c-button pf-m-control" type="button">
 | 
			
		||||
                            <i class="fas fa-plus" aria-hidden="true"></i>
 | 
			
		||||
                        </button>
 | 
			
		||||
                    </ak-group-member-select-table>
 | 
			
		||||
                    <div class="pf-c-form-control">
 | 
			
		||||
                        <ak-chip-group>
 | 
			
		||||
                            ${until(
 | 
			
		||||
                                new CoreApi(DEFAULT_CONFIG)
 | 
			
		||||
                                    .coreUsersList({
 | 
			
		||||
                                        ordering: "username",
 | 
			
		||||
                                    })
 | 
			
		||||
                                    .then((users) => {
 | 
			
		||||
                                        return users.results.map((user) => {
 | 
			
		||||
                                            const selected = Array.from(
 | 
			
		||||
                                                this.instance?.users || [],
 | 
			
		||||
                                            ).some((su) => {
 | 
			
		||||
                                                return su == user.pk;
 | 
			
		||||
                                            });
 | 
			
		||||
                                            if (!selected) return;
 | 
			
		||||
                                            return html`<ak-chip
 | 
			
		||||
                                                .removable=${true}
 | 
			
		||||
                                                value=${ifDefined(user.pk)}
 | 
			
		||||
                                                @remove=${() => {
 | 
			
		||||
                                                    if (!this.instance) return;
 | 
			
		||||
                                                    const users = Array.from(
 | 
			
		||||
                                                        this.instance?.users || [],
 | 
			
		||||
                                                    );
 | 
			
		||||
                                                    const idx = users.indexOf(user.pk || 0);
 | 
			
		||||
                                                    users.splice(idx, 1);
 | 
			
		||||
                                                    this.instance.users = users;
 | 
			
		||||
                                                    this.requestUpdate();
 | 
			
		||||
                                                }}
 | 
			
		||||
                                            >
 | 
			
		||||
                                                ${UserOption(user)}
 | 
			
		||||
                                            </ak-chip>`;
 | 
			
		||||
                                        });
 | 
			
		||||
                                    }),
 | 
			
		||||
                                html`<option>${t`Loading...`}</option>`,
 | 
			
		||||
                            )}
 | 
			
		||||
                        </ak-chip-group>
 | 
			
		||||
                    </div>
 | 
			
		||||
                </div>
 | 
			
		||||
            </ak-form-element-horizontal>
 | 
			
		||||
            <ak-form-element-horizontal label=${t`Attributes`} ?required=${true} name="attributes">
 | 
			
		||||
                <ak-codemirror
 | 
			
		||||
                    mode="yaml"
 | 
			
		||||
 | 
			
		||||
@ -11,9 +11,8 @@ import { t } from "@lingui/macro";
 | 
			
		||||
import { CSSResult, TemplateResult, css, html } from "lit";
 | 
			
		||||
import { customElement } from "lit/decorators.js";
 | 
			
		||||
import { ifDefined } from "lit/directives/if-defined.js";
 | 
			
		||||
import { until } from "lit/directives/until.js";
 | 
			
		||||
 | 
			
		||||
import { CoreApi, Group, User } from "@goauthentik/api";
 | 
			
		||||
import { CoreApi, User } from "@goauthentik/api";
 | 
			
		||||
 | 
			
		||||
@customElement("ak-user-form")
 | 
			
		||||
export class UserForm extends ModelForm<User, number> {
 | 
			
		||||
@ -44,9 +43,9 @@ export class UserForm extends ModelForm<User, number> {
 | 
			
		||||
 | 
			
		||||
    send = (data: User): Promise<User> => {
 | 
			
		||||
        if (this.instance?.pk) {
 | 
			
		||||
            return new CoreApi(DEFAULT_CONFIG).coreUsersUpdate({
 | 
			
		||||
            return new CoreApi(DEFAULT_CONFIG).coreUsersPartialUpdate({
 | 
			
		||||
                id: this.instance.pk,
 | 
			
		||||
                userRequest: data,
 | 
			
		||||
                patchedUserRequest: data,
 | 
			
		||||
            });
 | 
			
		||||
        } else {
 | 
			
		||||
            return new CoreApi(DEFAULT_CONFIG).coreUsersCreate({
 | 
			
		||||
@ -110,63 +109,6 @@ export class UserForm extends ModelForm<User, number> {
 | 
			
		||||
                    ${t`Designates whether this user should be treated as active. Unselect this instead of deleting accounts.`}
 | 
			
		||||
                </p>
 | 
			
		||||
            </ak-form-element-horizontal>
 | 
			
		||||
            <ak-form-element-horizontal label=${t`Groups`} name="groups">
 | 
			
		||||
                <div class="pf-c-input-group">
 | 
			
		||||
                    <ak-user-group-select-table
 | 
			
		||||
                        .confirm=${(items: Group[]) => {
 | 
			
		||||
                            // Because the model only has the IDs, map the group list to IDs
 | 
			
		||||
                            const ids = items.map((g) => g.pk);
 | 
			
		||||
                            if (!this.instance) this.instance = {} as User;
 | 
			
		||||
                            this.instance.groups = Array.from(this.instance?.groups || []).concat(
 | 
			
		||||
                                ids,
 | 
			
		||||
                            );
 | 
			
		||||
                            this.requestUpdate();
 | 
			
		||||
                            return Promise.resolve();
 | 
			
		||||
                        }}
 | 
			
		||||
                    >
 | 
			
		||||
                        <button slot="trigger" class="pf-c-button pf-m-control" type="button">
 | 
			
		||||
                            <i class="fas fa-plus" aria-hidden="true"></i>
 | 
			
		||||
                        </button>
 | 
			
		||||
                    </ak-user-group-select-table>
 | 
			
		||||
                    <div class="pf-c-form-control">
 | 
			
		||||
                        <ak-chip-group>
 | 
			
		||||
                            ${until(
 | 
			
		||||
                                new CoreApi(DEFAULT_CONFIG)
 | 
			
		||||
                                    .coreGroupsList({
 | 
			
		||||
                                        ordering: "name",
 | 
			
		||||
                                    })
 | 
			
		||||
                                    .then((groups) => {
 | 
			
		||||
                                        return groups.results.map((group) => {
 | 
			
		||||
                                            const selected = Array.from(
 | 
			
		||||
                                                this.instance?.groups || [],
 | 
			
		||||
                                            ).some((sg) => {
 | 
			
		||||
                                                return sg == group.pk;
 | 
			
		||||
                                            });
 | 
			
		||||
                                            if (!selected) return;
 | 
			
		||||
                                            return html`<ak-chip
 | 
			
		||||
                                                .removable=${true}
 | 
			
		||||
                                                value=${ifDefined(group.pk)}
 | 
			
		||||
                                                @remove=${() => {
 | 
			
		||||
                                                    if (!this.instance) return;
 | 
			
		||||
                                                    const groups = Array.from(
 | 
			
		||||
                                                        this.instance?.groups || [],
 | 
			
		||||
                                                    );
 | 
			
		||||
                                                    const idx = groups.indexOf(group.pk);
 | 
			
		||||
                                                    groups.splice(idx, 1);
 | 
			
		||||
                                                    this.instance.groups = groups;
 | 
			
		||||
                                                    this.requestUpdate();
 | 
			
		||||
                                                }}
 | 
			
		||||
                                            >
 | 
			
		||||
                                                ${group.name}
 | 
			
		||||
                                            </ak-chip>`;
 | 
			
		||||
                                        });
 | 
			
		||||
                                    }),
 | 
			
		||||
                                html`<option>${t`Loading...`}</option>`,
 | 
			
		||||
                            )}
 | 
			
		||||
                        </ak-chip-group>
 | 
			
		||||
                    </div>
 | 
			
		||||
                </div>
 | 
			
		||||
            </ak-form-element-horizontal>
 | 
			
		||||
            <ak-form-element-horizontal label=${t`Attributes`} ?required=${true} name="attributes">
 | 
			
		||||
                <ak-codemirror
 | 
			
		||||
                    mode="yaml"
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user