2021-03-31 17:15:28 +00:00
|
|
|
import { CoreApi, PoliciesApi, Policy, PolicyBinding } from "authentik-api";
|
2021-04-03 17:26:43 +00:00
|
|
|
import { t } from "@lingui/macro";
|
2021-03-31 17:15:28 +00:00
|
|
|
import { customElement, property } from "lit-element";
|
|
|
|
import { html, TemplateResult } from "lit-html";
|
|
|
|
import { DEFAULT_CONFIG } from "../../api/Config";
|
|
|
|
import { Form } from "../../elements/forms/Form";
|
|
|
|
import { until } from "lit-html/directives/until";
|
|
|
|
import { ifDefined } from "lit-html/directives/if-defined";
|
2021-04-03 09:41:11 +00:00
|
|
|
import { first, groupBy } from "../../utils";
|
2021-03-31 17:15:28 +00:00
|
|
|
import "../../elements/forms/HorizontalFormElement";
|
|
|
|
|
|
|
|
@customElement("ak-policy-binding-form")
|
|
|
|
export class PolicyBindingForm extends Form<PolicyBinding> {
|
|
|
|
|
|
|
|
@property({attribute: false})
|
|
|
|
binding?: PolicyBinding;
|
|
|
|
|
|
|
|
@property()
|
|
|
|
targetPk?: string;
|
|
|
|
|
|
|
|
getSuccessMessage(): string {
|
|
|
|
if (this.binding) {
|
2021-04-03 17:26:43 +00:00
|
|
|
return t`Successfully updated binding.`;
|
2021-03-31 17:15:28 +00:00
|
|
|
} else {
|
2021-04-03 17:26:43 +00:00
|
|
|
return t`Successfully created binding.`;
|
2021-03-31 17:15:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async customValidate(form: PolicyBinding): Promise<PolicyBinding> {
|
|
|
|
return form;
|
|
|
|
}
|
|
|
|
|
|
|
|
send = (data: PolicyBinding): Promise<PolicyBinding> => {
|
|
|
|
if (this.binding) {
|
|
|
|
return new PoliciesApi(DEFAULT_CONFIG).policiesBindingsUpdate({
|
|
|
|
policyBindingUuid: this.binding.pk || "",
|
|
|
|
data: data
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return new PoliciesApi(DEFAULT_CONFIG).policiesBindingsCreate({
|
|
|
|
data: data
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
groupPolicies(policies: Policy[]): TemplateResult {
|
|
|
|
return html`
|
|
|
|
${groupBy<Policy>(policies, (p => p.verboseName || "")).map(([group, policies]) => {
|
|
|
|
return html`<optgroup label=${group}>
|
|
|
|
${policies.map(p => {
|
|
|
|
const selected = (this.binding?.policy === p.pk);
|
|
|
|
return html`<option ?selected=${selected} value=${ifDefined(p.pk)}>${p.name}</option>`;
|
|
|
|
})}
|
|
|
|
</optgroup>`;
|
|
|
|
})}
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
getOrder(): Promise<number> {
|
|
|
|
if (this.binding) {
|
|
|
|
return Promise.resolve(this.binding.order);
|
|
|
|
}
|
|
|
|
return new PoliciesApi(DEFAULT_CONFIG).policiesBindingsList({
|
|
|
|
target: this.targetPk || "",
|
|
|
|
}).then(bindings => {
|
|
|
|
const orders = bindings.results.map(binding => binding.order);
|
2021-04-04 11:41:18 +00:00
|
|
|
if (orders.length < 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-03-31 17:15:28 +00:00
|
|
|
return Math.max(...orders) + 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
renderForm(): TemplateResult {
|
|
|
|
return html`<form class="pf-c-form pf-m-horizontal">
|
|
|
|
<ak-form-element-horizontal
|
2021-04-03 17:26:43 +00:00
|
|
|
label=${t`Policy`}
|
2021-03-31 17:15:28 +00:00
|
|
|
name="policy">
|
|
|
|
<select class="pf-c-form-control">
|
|
|
|
<option value="" ?selected=${this.binding?.policy === undefined}>---------</option>
|
|
|
|
${until(new PoliciesApi(DEFAULT_CONFIG).policiesAllList({
|
|
|
|
ordering: "pk"
|
|
|
|
}).then(policies => {
|
|
|
|
return this.groupPolicies(policies.results);
|
2021-04-03 22:24:06 +00:00
|
|
|
}), html`<option>${t`Loading...`}</option>`)}
|
2021-03-31 17:15:28 +00:00
|
|
|
</select>
|
|
|
|
</ak-form-element-horizontal>
|
|
|
|
<ak-form-element-horizontal
|
2021-04-03 17:26:43 +00:00
|
|
|
label=${t`Group`}
|
2021-03-31 17:15:28 +00:00
|
|
|
name="group">
|
|
|
|
<select class="pf-c-form-control">
|
|
|
|
<option value="" ?selected=${this.binding?.group === undefined}>---------</option>
|
|
|
|
${until(new CoreApi(DEFAULT_CONFIG).coreGroupsList({
|
|
|
|
ordering: "pk"
|
|
|
|
}).then(groups => {
|
|
|
|
return groups.results.map(group => {
|
|
|
|
return html`<option value=${ifDefined(group.pk)} ?selected=${group.pk === this.binding?.group}>${group.name}</option>`;
|
|
|
|
});
|
2021-04-03 22:24:06 +00:00
|
|
|
}), html`<option>${t`Loading...`}</option>`)}
|
2021-03-31 17:15:28 +00:00
|
|
|
</select>
|
|
|
|
</ak-form-element-horizontal>
|
|
|
|
<ak-form-element-horizontal
|
2021-04-03 17:26:43 +00:00
|
|
|
label=${t`User`}
|
2021-03-31 17:15:28 +00:00
|
|
|
name="user">
|
|
|
|
<select class="pf-c-form-control">
|
|
|
|
<option value="" ?selected=${this.binding?.user === undefined}>---------</option>
|
|
|
|
${until(new CoreApi(DEFAULT_CONFIG).coreUsersList({
|
|
|
|
ordering: "pk"
|
|
|
|
}).then(users => {
|
|
|
|
return users.results.map(user => {
|
|
|
|
return html`<option value=${ifDefined(user.pk)} ?selected=${user.pk === this.binding?.user}>${user.name}</option>`;
|
|
|
|
});
|
2021-04-03 22:24:06 +00:00
|
|
|
}), html`<option>${t`Loading...`}</option>`)}
|
2021-03-31 17:15:28 +00:00
|
|
|
</select>
|
|
|
|
</ak-form-element-horizontal>
|
|
|
|
<input required name="target" type="hidden" value=${ifDefined(this.binding?.target || this.targetPk)}>
|
|
|
|
<ak-form-element-horizontal name="enabled">
|
|
|
|
<div class="pf-c-check">
|
2021-04-03 22:36:53 +00:00
|
|
|
<input type="checkbox" class="pf-c-check__input" ?checked=${first(this.binding?.enabled, true)}>
|
2021-03-31 17:15:28 +00:00
|
|
|
<label class="pf-c-check__label">
|
2021-04-03 17:26:43 +00:00
|
|
|
${t`Enabled`}
|
2021-03-31 17:15:28 +00:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</ak-form-element-horizontal>
|
|
|
|
<ak-form-element-horizontal
|
2021-04-03 17:26:43 +00:00
|
|
|
label=${t`Order`}
|
2021-03-31 17:15:28 +00:00
|
|
|
?required=${true}
|
|
|
|
name="order">
|
|
|
|
<input type="number" value="${until(this.getOrder(), this.binding?.order)}" class="pf-c-form-control" required>
|
|
|
|
</ak-form-element-horizontal>
|
|
|
|
<ak-form-element-horizontal
|
2021-04-03 17:26:43 +00:00
|
|
|
label=${t`Timeout`}
|
2021-03-31 17:15:28 +00:00
|
|
|
?required=${true}
|
|
|
|
name="timeout">
|
2021-04-03 09:41:11 +00:00
|
|
|
<input type="number" value="${first(this.binding?.timeout, 30)}" class="pf-c-form-control" required>
|
2021-03-31 17:15:28 +00:00
|
|
|
</ak-form-element-horizontal>
|
|
|
|
</form>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|