2021-09-21 09:31:37 +00:00
|
|
|
import YAML from "yaml";
|
|
|
|
|
|
|
|
import { t } from "@lingui/macro";
|
|
|
|
|
2021-10-28 07:48:51 +00:00
|
|
|
import { TemplateResult, html } from "lit";
|
2021-11-04 21:34:48 +00:00
|
|
|
import { customElement, property } from "lit/decorators.js";
|
|
|
|
import { until } from "lit/directives/until.js";
|
2021-09-21 09:31:37 +00:00
|
|
|
|
2021-08-15 19:48:18 +00:00
|
|
|
import {
|
|
|
|
CoreApi,
|
|
|
|
PoliciesApi,
|
|
|
|
Policy,
|
|
|
|
PolicyTestRequest,
|
|
|
|
PolicyTestResult,
|
|
|
|
} from "@goauthentik/api";
|
2021-09-21 09:31:37 +00:00
|
|
|
|
2021-03-30 15:33:11 +00:00
|
|
|
import { DEFAULT_CONFIG } from "../../api/Config";
|
2021-09-21 09:31:37 +00:00
|
|
|
import "../../elements/CodeMirror";
|
2021-11-28 20:12:53 +00:00
|
|
|
import { PFColor } from "../../elements/Label";
|
2021-03-30 15:33:11 +00:00
|
|
|
import { Form } from "../../elements/forms/Form";
|
|
|
|
import "../../elements/forms/HorizontalFormElement";
|
2021-11-18 19:44:15 +00:00
|
|
|
import { UserOption } from "../../elements/user/utils";
|
2021-05-26 10:08:11 +00:00
|
|
|
import { first } from "../../utils";
|
2021-03-30 15:33:11 +00:00
|
|
|
|
|
|
|
@customElement("ak-policy-test-form")
|
2021-05-16 16:24:15 +00:00
|
|
|
export class PolicyTestForm extends Form<PolicyTestRequest> {
|
2021-08-03 15:52:21 +00:00
|
|
|
@property({ attribute: false })
|
2021-03-30 15:33:11 +00:00
|
|
|
policy?: Policy;
|
|
|
|
|
2021-08-03 15:52:21 +00:00
|
|
|
@property({ attribute: false })
|
2021-03-30 15:33:11 +00:00
|
|
|
result?: PolicyTestResult;
|
|
|
|
|
2021-08-03 15:52:21 +00:00
|
|
|
@property({ attribute: false })
|
2021-05-26 10:08:11 +00:00
|
|
|
request?: PolicyTestRequest;
|
|
|
|
|
2021-03-30 15:33:11 +00:00
|
|
|
getSuccessMessage(): string {
|
2021-04-03 17:26:43 +00:00
|
|
|
return t`Successfully sent test-request.`;
|
2021-03-30 15:33:11 +00:00
|
|
|
}
|
|
|
|
|
2021-05-16 16:24:15 +00:00
|
|
|
send = (data: PolicyTestRequest): Promise<PolicyTestResult> => {
|
2021-05-26 10:08:11 +00:00
|
|
|
this.request = data;
|
2021-08-03 15:52:21 +00:00
|
|
|
return new PoliciesApi(DEFAULT_CONFIG)
|
|
|
|
.policiesAllTestCreate({
|
|
|
|
policyUuid: this.policy?.pk || "",
|
|
|
|
policyTestRequest: data,
|
|
|
|
})
|
|
|
|
.then((result) => (this.result = result));
|
2021-03-30 15:33:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
renderResult(): TemplateResult {
|
2021-08-03 15:52:21 +00:00
|
|
|
return html` <ak-form-element-horizontal label=${t`Passing`}>
|
2021-03-30 15:33:11 +00:00
|
|
|
<div class="pf-c-form__group-label">
|
|
|
|
<div class="c-form__horizontal-group">
|
2021-11-28 20:12:53 +00:00
|
|
|
<span class="pf-c-form__label-text">
|
|
|
|
<ak-label color=${this.result?.passing ? PFColor.Green : PFColor.Red}>
|
|
|
|
${this.result?.passing ? t`Yes` : t`No`}
|
|
|
|
</ak-label>
|
|
|
|
</span>
|
2021-03-30 15:33:11 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</ak-form-element-horizontal>
|
2021-08-03 15:52:21 +00:00
|
|
|
<ak-form-element-horizontal label=${t`Messages`}>
|
2021-03-30 15:33:11 +00:00
|
|
|
<div class="pf-c-form__group-label">
|
|
|
|
<div class="c-form__horizontal-group">
|
|
|
|
<ul>
|
2021-08-03 15:52:21 +00:00
|
|
|
${(this.result?.messages || []).length > 0
|
|
|
|
? this.result?.messages?.map((m) => {
|
|
|
|
return html`<li>
|
|
|
|
<span class="pf-c-form__label-text">${m}</span>
|
|
|
|
</li>`;
|
|
|
|
})
|
|
|
|
: html`<li>
|
|
|
|
<span class="pf-c-form__label-text">-</span>
|
|
|
|
</li>`}
|
2021-03-30 15:33:11 +00:00
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</ak-form-element-horizontal>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
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`User`} ?required=${true} name="user">
|
2021-03-30 15:33:11 +00:00
|
|
|
<select class="pf-c-form-control">
|
2021-08-03 15:52:21 +00:00
|
|
|
${until(
|
|
|
|
new CoreApi(DEFAULT_CONFIG)
|
|
|
|
.coreUsersList({
|
|
|
|
ordering: "username",
|
|
|
|
})
|
|
|
|
.then((users) => {
|
|
|
|
return users.results.map((user) => {
|
|
|
|
return html`<option
|
|
|
|
?selected=${this.request?.user.toString() ===
|
|
|
|
user.pk.toString()}
|
|
|
|
value=${user.pk}
|
|
|
|
>
|
2021-11-18 19:44:15 +00:00
|
|
|
${UserOption(user)}
|
2021-08-03 15:52:21 +00:00
|
|
|
</option>`;
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
html`<option>${t`Loading...`}</option>`,
|
|
|
|
)}
|
2021-03-30 15:33:11 +00:00
|
|
|
</select>
|
|
|
|
</ak-form-element-horizontal>
|
2021-08-03 15:52:21 +00:00
|
|
|
<ak-form-element-horizontal label=${t`Context`} name="context">
|
|
|
|
<ak-codemirror mode="yaml" value=${YAML.stringify(first(this.request?.context, {}))}
|
|
|
|
>>
|
2021-03-30 15:33:11 +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-30 15:33:11 +00:00
|
|
|
</ak-form-element-horizontal>
|
2021-08-03 15:52:21 +00:00
|
|
|
${this.result ? this.renderResult() : html``}
|
2021-03-30 15:33:11 +00:00
|
|
|
</form>`;
|
|
|
|
}
|
|
|
|
}
|