2021-03-25 20:39:49 +00:00
|
|
|
import { CoreApi, Group } from "authentik-api";
|
|
|
|
import { gettext } from "django";
|
|
|
|
import { customElement, property } from "lit-element";
|
|
|
|
import { html, TemplateResult } from "lit-html";
|
|
|
|
import { DEFAULT_CONFIG } from "../../api/Config";
|
|
|
|
import { Form } from "../../elements/forms/Form";
|
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";
|
|
|
|
import YAML from "yaml";
|
2021-03-25 20:39:49 +00:00
|
|
|
|
|
|
|
@customElement("ak-group-form")
|
|
|
|
export class GroupForm extends Form<Group> {
|
|
|
|
|
|
|
|
@property({attribute: false})
|
|
|
|
group?: Group;
|
|
|
|
|
2021-03-29 13:57:38 +00:00
|
|
|
getSuccessMessage(): string {
|
|
|
|
if (this.group) {
|
2021-03-29 14:16:27 +00:00
|
|
|
return gettext("Successfully updated group.");
|
2021-03-29 13:57:38 +00:00
|
|
|
} else {
|
2021-03-29 14:16:27 +00:00
|
|
|
return gettext("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> => {
|
|
|
|
if (this.group) {
|
|
|
|
return new CoreApi(DEFAULT_CONFIG).coreGroupsUpdate({
|
|
|
|
groupUuid: this.group.pk || "",
|
|
|
|
data: data
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return new CoreApi(DEFAULT_CONFIG).coreGroupsCreate({
|
|
|
|
data: data
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
renderForm(): TemplateResult {
|
|
|
|
return html`<form class="pf-c-form pf-m-horizontal">
|
2021-03-29 16:22:15 +00:00
|
|
|
<ak-form-element-horizontal
|
|
|
|
label=${gettext("Name")}
|
|
|
|
?required=${true}
|
|
|
|
name="name">
|
2021-03-29 16:25:59 +00:00
|
|
|
<input type="text" value="${ifDefined(this.group?.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-03-29 16:22:15 +00:00
|
|
|
<input type="checkbox" class="pf-c-check__input" ?checked=${this.group?.isSuperuser || false}>
|
2021-03-29 08:09:43 +00:00
|
|
|
<label class="pf-c-check__label">
|
|
|
|
${gettext("Is superuser")}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
<p class="pf-c-form__helper-text">${gettext("Users added to this group will be superusers.")}</p>
|
|
|
|
</ak-form-element-horizontal>
|
2021-03-29 16:22:15 +00:00
|
|
|
<ak-form-element-horizontal
|
|
|
|
label=${gettext("Parent")}
|
|
|
|
?required=${true}
|
|
|
|
name="parent">
|
|
|
|
<select class="pf-c-form-control">
|
2021-03-29 08:09:43 +00:00
|
|
|
<option value="" ?selected=${this.group?.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.group?.parent === group.pk}>${group.name}</option>`;
|
|
|
|
});
|
|
|
|
}), html``)}
|
|
|
|
</select>
|
|
|
|
</ak-form-element-horizontal>
|
2021-03-29 16:22:15 +00:00
|
|
|
<ak-form-element-horizontal
|
|
|
|
label=${gettext("Members")}
|
|
|
|
?required=${true}
|
|
|
|
name="users">
|
2021-03-29 20:52:08 +00:00
|
|
|
<select class="pf-c-form-control" multiple>
|
2021-04-01 13:39:59 +00:00
|
|
|
${until(new CoreApi(DEFAULT_CONFIG).coreUsersList({
|
|
|
|
ordering: "username",
|
|
|
|
}).then(users => {
|
2021-03-29 08:09:43 +00:00
|
|
|
return users.results.map(user => {
|
|
|
|
const selected = Array.from(this.group?.users || []).some(su => {
|
|
|
|
return su == user.pk;
|
2021-03-28 20:07:11 +00:00
|
|
|
});
|
2021-04-01 13:39:59 +00:00
|
|
|
return html`<option value=${ifDefined(user.pk)} ?selected=${selected}>${user.username} (${user.name})</option>`;
|
2021-03-29 08:09:43 +00:00
|
|
|
});
|
|
|
|
}))}
|
|
|
|
</select>
|
|
|
|
<p class="pf-c-form__helper-text">${gettext("Hold control/command to select multiple items.")}</p>
|
|
|
|
</ak-form-element-horizontal>
|
2021-03-29 16:22:15 +00:00
|
|
|
<ak-form-element-horizontal
|
|
|
|
label=${gettext("Attributes")}
|
|
|
|
name="attributes">
|
|
|
|
<ak-codemirror mode="yaml" value="${YAML.stringify(this.group?.attributes)}">
|
2021-03-29 10:16:32 +00:00
|
|
|
</ak-codemirror>
|
|
|
|
</ak-form-element-horizontal>
|
2021-03-29 08:09:43 +00:00
|
|
|
</form>`;
|
2021-03-25 20:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|