2021-04-10 18:19:23 +00:00
|
|
|
import { CoreApi, Group, User } from "authentik-api";
|
2021-04-03 17:26:43 +00:00
|
|
|
import { t } from "@lingui/macro";
|
2021-05-11 10:12:31 +00:00
|
|
|
import { customElement } from "lit-element";
|
2021-03-25 20:39:49 +00:00
|
|
|
import { html, TemplateResult } from "lit-html";
|
|
|
|
import { DEFAULT_CONFIG } from "../../api/Config";
|
2021-03-27 22:38:53 +00:00
|
|
|
import { until } from "lit-html/directives/until";
|
2021-03-28 20:07:11 +00:00
|
|
|
import { ifDefined } from "lit-html/directives/if-defined";
|
2021-03-29 08:09:43 +00:00
|
|
|
import "../../elements/forms/HorizontalFormElement";
|
2021-03-29 10:16:32 +00:00
|
|
|
import "../../elements/CodeMirror";
|
2021-04-10 18:19:23 +00:00
|
|
|
import "../../elements/chips/ChipGroup";
|
|
|
|
import "../../elements/chips/Chip";
|
|
|
|
import "./MemberSelectModal";
|
2021-03-29 10:16:32 +00:00
|
|
|
import YAML from "yaml";
|
2021-04-03 22:36:53 +00:00
|
|
|
import { first } from "../../utils";
|
2021-05-11 10:05:30 +00:00
|
|
|
import { ModelForm } from "../../elements/forms/ModelForm";
|
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> {
|
2021-03-25 20:39:49 +00:00
|
|
|
|
2021-05-11 10:05:30 +00:00
|
|
|
loadInstance(pk: string): Promise<Group> {
|
2021-05-16 12:43:42 +00:00
|
|
|
return new CoreApi(DEFAULT_CONFIG).coreGroupsRetrieve({
|
2021-05-11 10:05:30 +00:00
|
|
|
groupUuid: pk
|
|
|
|
});
|
|
|
|
}
|
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-05-11 10:05:30 +00:00
|
|
|
groupUuid: this.instance.pk || "",
|
2021-05-16 16:24:15 +00:00
|
|
|
groupRequest: data
|
2021-03-25 20:39:49 +00:00
|
|
|
});
|
|
|
|
} else {
|
2021-05-11 10:05:30 +00:00
|
|
|
data.users = Array.from(this.instance?.users || []) as unknown as Set<number>;
|
2021-03-25 20:39:49 +00:00
|
|
|
return new CoreApi(DEFAULT_CONFIG).coreGroupsCreate({
|
2021-05-16 16:24:15 +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-03-29 16:22:15 +00:00
|
|
|
<ak-form-element-horizontal
|
2021-04-03 17:26:43 +00:00
|
|
|
label=${t`Name`}
|
2021-03-29 16:22:15 +00:00
|
|
|
?required=${true}
|
|
|
|
name="name">
|
2021-05-11 10:05:30 +00:00
|
|
|
<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-05-11 10:05:30 +00:00
|
|
|
<input type="checkbox" class="pf-c-check__input" ?checked=${first(this.instance?.isSuperuser, false)}>
|
2021-03-29 08:09:43 +00:00
|
|
|
<label class="pf-c-check__label">
|
2021-04-03 17:26:43 +00:00
|
|
|
${t`Is superuser`}
|
2021-03-29 08:09:43 +00:00
|
|
|
</label>
|
|
|
|
</div>
|
2021-04-03 17:26:43 +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-03-29 16:22:15 +00:00
|
|
|
<ak-form-element-horizontal
|
2021-04-03 17:26:43 +00:00
|
|
|
label=${t`Parent`}
|
2021-03-29 16:22:15 +00:00
|
|
|
name="parent">
|
|
|
|
<select class="pf-c-form-control">
|
2021-05-11 10:05:30 +00:00
|
|
|
<option value="" ?selected=${this.instance?.parent === undefined}>---------</option>
|
2021-03-29 08:09:43 +00:00
|
|
|
${until(new CoreApi(DEFAULT_CONFIG).coreGroupsList({}).then(groups => {
|
|
|
|
return groups.results.map(group => {
|
2021-05-11 10:05:30 +00:00
|
|
|
return html`<option value=${ifDefined(group.pk)} ?selected=${this.instance?.parent === group.pk}>${group.name}</option>`;
|
2021-03-29 08:09:43 +00:00
|
|
|
});
|
2021-04-03 22:24:06 +00:00
|
|
|
}), html`<option>${t`Loading...`}</option>`)}
|
2021-03-29 08:09:43 +00:00
|
|
|
</select>
|
|
|
|
</ak-form-element-horizontal>
|
2021-03-29 16:22:15 +00:00
|
|
|
<ak-form-element-horizontal
|
2021-04-03 17:26:43 +00:00
|
|
|
label=${t`Members`}
|
2021-03-29 16:22:15 +00:00
|
|
|
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
|
|
|
|
const ids = items.map(u => u.pk || 0);
|
2021-05-11 10:05:30 +00:00
|
|
|
if (!this.instance) this.instance = {} as Group;
|
|
|
|
this.instance.users = new Set(Array.from(this.instance?.users || []).concat(ids));
|
2021-04-10 18:19:23 +00:00
|
|
|
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 => {
|
2021-05-11 10:05:30 +00:00
|
|
|
const selected = Array.from(this.instance?.users || []).some(su => {
|
2021-04-10 18:19:23 +00:00
|
|
|
return su == user.pk;
|
|
|
|
});
|
|
|
|
if (!selected) return;
|
|
|
|
return html`<ak-chip
|
|
|
|
.removable=${true}
|
|
|
|
value=${ifDefined(user.pk)}
|
|
|
|
@remove=${() => {
|
2021-05-11 10:05:30 +00:00
|
|
|
if (!this.instance) return;
|
|
|
|
const users = Array.from(this.instance?.users || []);
|
2021-04-10 18:19:23 +00:00
|
|
|
const idx = users.indexOf(user.pk || 0);
|
|
|
|
users.splice(idx, 1);
|
2021-05-11 10:05:30 +00:00
|
|
|
this.instance.users = new Set(users);
|
2021-04-10 18:19:23 +00:00
|
|
|
this.requestUpdate();
|
|
|
|
}}>
|
|
|
|
${user.username}
|
|
|
|
</ak-chip>`;
|
|
|
|
});
|
|
|
|
}), html`<option>${t`Loading...`}</option>`)}
|
|
|
|
</ak-chip-group>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-04-03 17:26:43 +00:00
|
|
|
<p class="pf-c-form__helper-text">${t`Hold control/command to select multiple items.`}</p>
|
2021-03-29 08:09:43 +00:00
|
|
|
</ak-form-element-horizontal>
|
2021-03-29 16:22:15 +00:00
|
|
|
<ak-form-element-horizontal
|
2021-04-03 17:26:43 +00:00
|
|
|
label=${t`Attributes`}
|
2021-04-22 08:33:36 +00:00
|
|
|
?required=${true}
|
2021-03-29 16:22:15 +00:00
|
|
|
name="attributes">
|
2021-05-11 10:05:30 +00:00
|
|
|
<ak-codemirror mode="yaml" value="${YAML.stringify(first(this.instance?.attributes, {}))}">
|
2021-03-29 10:16:32 +00:00
|
|
|
</ak-codemirror>
|
2021-04-09 12:30:49 +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
|
|
|
}
|
|
|
|
|
|
|
|
}
|