2021-09-21 09:31:37 +00:00
|
|
|
import YAML from "yaml";
|
|
|
|
|
2021-04-03 17:26:43 +00:00
|
|
|
import { t } from "@lingui/macro";
|
2021-09-21 09:31:37 +00:00
|
|
|
|
2021-09-21 09:19:26 +00:00
|
|
|
import { html, TemplateResult } from "lit";
|
2021-09-21 09:31:37 +00:00
|
|
|
import { customElement } from "lit/decorators";
|
2021-09-21 09:19:26 +00:00
|
|
|
import { ifDefined } from "lit/directives/if-defined";
|
2021-09-21 09:31:37 +00:00
|
|
|
import { until } from "lit/directives/until";
|
|
|
|
|
|
|
|
import { CoreApi, Group, User } from "@goauthentik/api";
|
|
|
|
|
|
|
|
import { DEFAULT_CONFIG } from "../../api/Config";
|
2021-03-29 10:16:32 +00:00
|
|
|
import "../../elements/CodeMirror";
|
2021-04-10 18:19:23 +00:00
|
|
|
import "../../elements/chips/Chip";
|
2021-09-21 09:31:37 +00:00
|
|
|
import "../../elements/chips/ChipGroup";
|
|
|
|
import "../../elements/forms/HorizontalFormElement";
|
2021-05-11 10:05:30 +00:00
|
|
|
import { ModelForm } from "../../elements/forms/ModelForm";
|
2021-09-21 09:31:37 +00:00
|
|
|
import { first } from "../../utils";
|
|
|
|
import "./MemberSelectModal";
|
2021-03-25 20:39:49 +00:00
|
|
|
|
|
|
|
@customElement("ak-group-form")
|
2021-05-11 10:05:30 +00:00
|
|
|
export class GroupForm extends ModelForm<Group, string> {
|
|
|
|
loadInstance(pk: string): Promise<Group> {
|
2021-05-16 12:43:42 +00:00
|
|
|
return new CoreApi(DEFAULT_CONFIG).coreGroupsRetrieve({
|
2021-08-03 15:52:21 +00:00
|
|
|
groupUuid: pk,
|
2021-05-11 10:05:30 +00:00
|
|
|
});
|
|
|
|
}
|
2021-03-25 20:39:49 +00:00
|
|
|
|
2021-03-29 13:57:38 +00:00
|
|
|
getSuccessMessage(): string {
|
2021-05-11 10:05:30 +00:00
|
|
|
if (this.instance) {
|
2021-04-03 17:26:43 +00:00
|
|
|
return t`Successfully updated group.`;
|
2021-03-29 13:57:38 +00:00
|
|
|
} else {
|
2021-04-03 17:26:43 +00:00
|
|
|
return t`Successfully created group.`;
|
2021-03-29 13:57:38 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-28 20:07:11 +00:00
|
|
|
|
2021-03-25 20:39:49 +00:00
|
|
|
send = (data: Group): Promise<Group> => {
|
2021-05-11 10:05:30 +00:00
|
|
|
if (this.instance?.pk) {
|
2021-03-25 20:39:49 +00:00
|
|
|
return new CoreApi(DEFAULT_CONFIG).coreGroupsUpdate({
|
2021-08-10 19:27:41 +00:00
|
|
|
groupUuid: this.instance.pk,
|
2021-08-03 15:52:21 +00:00
|
|
|
groupRequest: data,
|
2021-03-25 20:39:49 +00:00
|
|
|
});
|
|
|
|
} else {
|
2021-05-16 16:38:19 +00:00
|
|
|
data.users = Array.from(this.instance?.users || []);
|
2021-03-25 20:39:49 +00:00
|
|
|
return new CoreApi(DEFAULT_CONFIG).coreGroupsCreate({
|
2021-08-03 15:52:21 +00:00
|
|
|
groupRequest: data,
|
2021-03-25 20:39:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
renderForm(): TemplateResult {
|
|
|
|
return html`<form class="pf-c-form pf-m-horizontal">
|
2021-08-03 15:52:21 +00:00
|
|
|
<ak-form-element-horizontal label=${t`Name`} ?required=${true} name="name">
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
value="${ifDefined(this.instance?.name)}"
|
|
|
|
class="pf-c-form-control"
|
|
|
|
required
|
|
|
|
/>
|
2021-03-29 08:09:43 +00:00
|
|
|
</ak-form-element-horizontal>
|
2021-03-29 16:22:15 +00:00
|
|
|
<ak-form-element-horizontal name="isSuperuser">
|
2021-03-29 08:09:43 +00:00
|
|
|
<div class="pf-c-check">
|
2021-08-03 15:52:21 +00:00
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
class="pf-c-check__input"
|
|
|
|
?checked=${first(this.instance?.isSuperuser, false)}
|
|
|
|
/>
|
|
|
|
<label class="pf-c-check__label"> ${t`Is superuser`} </label>
|
2021-03-29 08:09:43 +00:00
|
|
|
</div>
|
2021-08-03 15:52:21 +00:00
|
|
|
<p class="pf-c-form__helper-text">
|
|
|
|
${t`Users added to this group will be superusers.`}
|
|
|
|
</p>
|
2021-03-29 08:09:43 +00:00
|
|
|
</ak-form-element-horizontal>
|
2021-08-03 15:52:21 +00:00
|
|
|
<ak-form-element-horizontal label=${t`Parent`} name="parent">
|
2021-03-29 16:22:15 +00:00
|
|
|
<select class="pf-c-form-control">
|
2021-08-03 15:52:21 +00:00
|
|
|
<option value="" ?selected=${this.instance?.parent === undefined}>
|
|
|
|
---------
|
|
|
|
</option>
|
|
|
|
${until(
|
|
|
|
new CoreApi(DEFAULT_CONFIG).coreGroupsList({}).then((groups) => {
|
|
|
|
return groups.results.map((group) => {
|
|
|
|
return html`<option
|
|
|
|
value=${ifDefined(group.pk)}
|
|
|
|
?selected=${this.instance?.parent === group.pk}
|
|
|
|
>
|
|
|
|
${group.name}
|
|
|
|
</option>`;
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
html`<option>${t`Loading...`}</option>`,
|
|
|
|
)}
|
2021-03-29 08:09:43 +00:00
|
|
|
</select>
|
|
|
|
</ak-form-element-horizontal>
|
2021-08-03 15:52:21 +00:00
|
|
|
<ak-form-element-horizontal label=${t`Members`} name="users">
|
2021-04-10 18:19:23 +00:00
|
|
|
<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
|
2021-08-03 15:52:21 +00:00
|
|
|
const ids = items.map((u) => u.pk || 0);
|
2021-05-11 10:05:30 +00:00
|
|
|
if (!this.instance) this.instance = {} as Group;
|
2021-08-03 15:52:21 +00:00
|
|
|
this.instance.users = Array.from(this.instance?.users || []).concat(
|
|
|
|
ids,
|
|
|
|
);
|
2021-04-10 18:19:23 +00:00
|
|
|
this.requestUpdate();
|
|
|
|
return Promise.resolve();
|
2021-08-03 15:52:21 +00:00
|
|
|
}}
|
|
|
|
>
|
2021-04-10 18:19:23 +00:00
|
|
|
<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>
|
2021-08-03 15:52:21 +00:00
|
|
|
${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();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
${user.username}
|
|
|
|
</ak-chip>`;
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
html`<option>${t`Loading...`}</option>`,
|
|
|
|
)}
|
2021-04-10 18:19:23 +00:00
|
|
|
</ak-chip-group>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-03-29 08:09:43 +00:00
|
|
|
</ak-form-element-horizontal>
|
2021-08-03 15:52:21 +00:00
|
|
|
<ak-form-element-horizontal label=${t`Attributes`} ?required=${true} name="attributes">
|
|
|
|
<ak-codemirror
|
|
|
|
mode="yaml"
|
|
|
|
value="${YAML.stringify(first(this.instance?.attributes, {}))}"
|
|
|
|
>
|
2021-03-29 10:16:32 +00:00
|
|
|
</ak-codemirror>
|
2021-08-03 15:52:21 +00:00
|
|
|
<p class="pf-c-form__helper-text">
|
|
|
|
${t`Set custom attributes using YAML or JSON.`}
|
|
|
|
</p>
|
2021-03-29 10:16:32 +00:00
|
|
|
</ak-form-element-horizontal>
|
2021-03-29 08:09:43 +00:00
|
|
|
</form>`;
|
2021-03-25 20:39:49 +00:00
|
|
|
}
|
|
|
|
}
|