2021-08-10 19:27:41 +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:44:50 +00:00
|
|
|
import { customElement } from "lit-element";
|
2021-03-29 14:16:27 +00:00
|
|
|
import { html, TemplateResult } from "lit-html";
|
|
|
|
import { DEFAULT_CONFIG } from "../../api/Config";
|
|
|
|
import { ifDefined } from "lit-html/directives/if-defined";
|
|
|
|
import "../../elements/forms/HorizontalFormElement";
|
|
|
|
import "../../elements/CodeMirror";
|
2021-08-10 19:27:41 +00:00
|
|
|
import "./GroupSelectModal";
|
2021-03-29 14:16:27 +00:00
|
|
|
import YAML from "yaml";
|
2021-04-03 22:36:53 +00:00
|
|
|
import { first } from "../../utils";
|
2021-05-11 10:44:50 +00:00
|
|
|
import { ModelForm } from "../../elements/forms/ModelForm";
|
2021-08-10 19:27:41 +00:00
|
|
|
import { until } from "lit-html/directives/until";
|
2021-03-29 14:16:27 +00:00
|
|
|
|
|
|
|
@customElement("ak-user-form")
|
2021-05-11 10:44:50 +00:00
|
|
|
export class UserForm extends ModelForm<User, number> {
|
|
|
|
loadInstance(pk: number): Promise<User> {
|
2021-05-16 12:43:42 +00:00
|
|
|
return new CoreApi(DEFAULT_CONFIG).coreUsersRetrieve({
|
2021-08-03 15:52:21 +00:00
|
|
|
id: pk,
|
2021-05-11 10:44:50 +00:00
|
|
|
});
|
|
|
|
}
|
2021-03-29 14:16:27 +00:00
|
|
|
|
|
|
|
getSuccessMessage(): string {
|
2021-05-11 10:44:50 +00:00
|
|
|
if (this.instance) {
|
2021-04-03 17:26:43 +00:00
|
|
|
return t`Successfully updated user.`;
|
2021-03-29 14:16:27 +00:00
|
|
|
} else {
|
2021-04-03 17:26:43 +00:00
|
|
|
return t`Successfully created user.`;
|
2021-03-29 14:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
send = (data: User): Promise<User> => {
|
2021-08-10 19:27:41 +00:00
|
|
|
if (this.instance?.pk) {
|
2021-03-29 14:16:27 +00:00
|
|
|
return new CoreApi(DEFAULT_CONFIG).coreUsersUpdate({
|
2021-08-10 19:27:41 +00:00
|
|
|
id: this.instance.pk,
|
2021-08-03 15:52:21 +00:00
|
|
|
userRequest: data,
|
2021-03-29 14:16:27 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return new CoreApi(DEFAULT_CONFIG).coreUsersCreate({
|
2021-08-03 15:52:21 +00:00
|
|
|
userRequest: data,
|
2021-03-29 14:16:27 +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`Username`} ?required=${true} name="username">
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
value="${ifDefined(this.instance?.username)}"
|
|
|
|
class="pf-c-form-control"
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
<p class="pf-c-form__helper-text">
|
|
|
|
${t`Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.`}
|
|
|
|
</p>
|
2021-03-29 14:16:27 +00:00
|
|
|
</ak-form-element-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-04-03 17:26:43 +00:00
|
|
|
<p class="pf-c-form__helper-text">${t`User's display name.`}</p>
|
2021-03-29 14:16:27 +00:00
|
|
|
</ak-form-element-horizontal>
|
2021-08-05 12:43:16 +00:00
|
|
|
<ak-form-element-horizontal label=${t`Email`} name="email">
|
2021-08-03 15:52:21 +00:00
|
|
|
<input
|
|
|
|
type="email"
|
|
|
|
autocomplete="off"
|
|
|
|
value="${ifDefined(this.instance?.email)}"
|
|
|
|
class="pf-c-form-control"
|
|
|
|
/>
|
2021-03-29 14:16:27 +00:00
|
|
|
</ak-form-element-horizontal>
|
2021-08-03 15:52:21 +00:00
|
|
|
<ak-form-element-horizontal name="isActive">
|
2021-03-29 14:16:27 +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?.isActive, true)}
|
|
|
|
/>
|
|
|
|
<label class="pf-c-check__label"> ${t`Is active`} </label>
|
2021-03-29 14:16:27 +00:00
|
|
|
</div>
|
2021-08-03 15:52:21 +00:00
|
|
|
<p class="pf-c-form__helper-text">
|
|
|
|
${t`Designates whether this user should be treated as active. Unselect this instead of deleting accounts.`}
|
|
|
|
</p>
|
2021-03-29 14:16:27 +00:00
|
|
|
</ak-form-element-horizontal>
|
2021-08-10 19:27:41 +00:00
|
|
|
<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>
|
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 14:16:27 +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 14:16:27 +00:00
|
|
|
</ak-form-element-horizontal>
|
|
|
|
</form>`;
|
|
|
|
}
|
|
|
|
}
|