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-04-04 20:56:28 +00:00
|
|
|
import { css, CSSResult, customElement, property } from "lit-element";
|
2021-03-31 17:15:28 +00:00
|
|
|
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";
|
2021-04-04 20:56:28 +00:00
|
|
|
import PFToggleGroup from "@patternfly/patternfly/components/ToggleGroup/toggle-group.css";
|
|
|
|
import PFContent from "@patternfly/patternfly/components/Content/content.css";
|
|
|
|
|
|
|
|
enum target {
|
|
|
|
policy, group, user
|
2021-04-04 21:19:08 +00:00
|
|
|
}
|
2021-03-31 17:15:28 +00:00
|
|
|
|
|
|
|
@customElement("ak-policy-binding-form")
|
|
|
|
export class PolicyBindingForm extends Form<PolicyBinding> {
|
|
|
|
|
|
|
|
@property({attribute: false})
|
2021-04-04 20:56:28 +00:00
|
|
|
set binding(value: PolicyBinding | undefined) {
|
|
|
|
this._binding = value;
|
|
|
|
if (value?.policyObj) {
|
|
|
|
this.policyGroupUser = target.policy;
|
|
|
|
}
|
|
|
|
if (value?.groupObj) {
|
|
|
|
this.policyGroupUser = target.group;
|
|
|
|
}
|
|
|
|
if (value?.userObj) {
|
|
|
|
this.policyGroupUser = target.user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get binding(): PolicyBinding | undefined {
|
|
|
|
return this._binding;
|
|
|
|
}
|
|
|
|
|
|
|
|
_binding?: PolicyBinding;
|
2021-03-31 17:15:28 +00:00
|
|
|
|
|
|
|
@property()
|
|
|
|
targetPk?: string;
|
|
|
|
|
2021-04-04 21:19:08 +00:00
|
|
|
@property({type: Number})
|
2021-04-04 20:56:28 +00:00
|
|
|
policyGroupUser?: target;
|
|
|
|
|
2021-03-31 17:15:28 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 20:56:28 +00:00
|
|
|
static get styles(): CSSResult[] {
|
|
|
|
return super.styles.concat(PFToggleGroup, PFContent, css`
|
|
|
|
.pf-c-toggle-group {
|
|
|
|
justify-content: center;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
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">
|
2021-04-04 20:56:28 +00:00
|
|
|
<div class="pf-c-card pf-m-selectable pf-m-selected">
|
|
|
|
<div class="pf-c-card__body">
|
|
|
|
<div class="pf-c-toggle-group">
|
|
|
|
<div class="pf-c-toggle-group__item">
|
|
|
|
<button class="pf-c-toggle-group__button ${this.policyGroupUser === target.policy ? "pf-m-selected": ""}" type="button" @click=${() => {
|
|
|
|
this.policyGroupUser = target.policy;
|
|
|
|
}}>
|
|
|
|
<span class="pf-c-toggle-group__text">${t`Policy`}</span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div class="pf-c-divider pf-m-vertical" role="separator"></div>
|
|
|
|
<div class="pf-c-toggle-group__item">
|
|
|
|
<button class="pf-c-toggle-group__button ${this.policyGroupUser === target.group ? "pf-m-selected" : ""}" type="button" @click=${() => {
|
|
|
|
this.policyGroupUser = target.group;
|
|
|
|
}}>
|
|
|
|
<span class="pf-c-toggle-group__text">${t`Group`}</span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div class="pf-c-divider pf-m-vertical" role="separator"></div>
|
|
|
|
<div class="pf-c-toggle-group__item">
|
|
|
|
<button class="pf-c-toggle-group__button ${this.policyGroupUser === target.user ? "pf-m-selected" : ""}" type="button" @click=${() => {
|
|
|
|
this.policyGroupUser = target.user;
|
|
|
|
}}>
|
|
|
|
<span class="pf-c-toggle-group__text">${t`User`}</span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="pf-c-card__footer">
|
|
|
|
<ak-form-element-horizontal
|
|
|
|
label=${t`Policy`}
|
|
|
|
name="policy"
|
|
|
|
?hidden=${this.policyGroupUser !== target.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);
|
|
|
|
}), html`<option>${t`Loading...`}</option>`)}
|
|
|
|
</select>
|
|
|
|
</ak-form-element-horizontal>
|
|
|
|
<ak-form-element-horizontal
|
|
|
|
label=${t`Group`}
|
|
|
|
name="group"
|
|
|
|
?hidden=${this.policyGroupUser !== target.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>`;
|
|
|
|
});
|
|
|
|
}), html`<option>${t`Loading...`}</option>`)}
|
|
|
|
</select>
|
|
|
|
</ak-form-element-horizontal>
|
|
|
|
<ak-form-element-horizontal
|
|
|
|
label=${t`User`}
|
|
|
|
name="user"
|
|
|
|
?hidden=${this.policyGroupUser !== target.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>`;
|
|
|
|
});
|
|
|
|
}), html`<option>${t`Loading...`}</option>`)}
|
|
|
|
</select>
|
|
|
|
</ak-form-element-horizontal>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-03-31 17:15:28 +00:00
|
|
|
<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>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|