import { DEFAULT_CONFIG } from "@goauthentik/common/api/config"; import { first } from "@goauthentik/common/utils"; import "@goauthentik/elements/CodeMirror"; import { PFColor } from "@goauthentik/elements/Label"; import { Form } from "@goauthentik/elements/forms/Form"; import "@goauthentik/elements/forms/HorizontalFormElement"; import "@goauthentik/elements/forms/SearchSelect"; import YAML from "yaml"; import { t } from "@lingui/macro"; import { CSSResult, TemplateResult, html } from "lit"; import { customElement, property, state } from "lit/decorators.js"; import PFDescriptionList from "@patternfly/patternfly/components/DescriptionList/description-list.css"; import { CoreApi, CoreUsersListRequest, PoliciesApi, Policy, PolicyTestRequest, PolicyTestResult, User, } from "@goauthentik/api"; @customElement("ak-policy-test-form") export class PolicyTestForm extends Form { @property({ attribute: false }) policy?: Policy; @state() result?: PolicyTestResult; @property({ attribute: false }) request?: PolicyTestRequest; getSuccessMessage(): string { return t`Successfully sent test-request.`; } send = async (data: PolicyTestRequest): Promise => { this.request = data; const result = await new PoliciesApi(DEFAULT_CONFIG).policiesAllTestCreate({ policyUuid: this.policy?.pk || "", policyTestRequest: data, }); return (this.result = result); }; static get styles(): CSSResult[] { return super.styles.concat(PFDescriptionList); } renderResult(): TemplateResult { return html`
${this.result?.passing ? t`Yes` : t`No`}
    ${(this.result?.messages || []).length > 0 ? this.result?.messages?.map((m) => { return html`
  • ${m}
  • `; }) : html`
  • -
  • `}
${(this.result?.logMessages || []).length > 0 ? this.result?.logMessages?.map((m) => { return html`
${m.log_level}
${m.event}
`; }) : html`
${t`No log messages.`}
`}
`; } renderForm(): TemplateResult { return html`
=> { const args: CoreUsersListRequest = { ordering: "username", }; if (query !== undefined) { args.search = query; } const users = await new CoreApi(DEFAULT_CONFIG).coreUsersList(args); return users.results; }} .renderElement=${(user: User): string => { return user.username; }} .renderDescription=${(user: User): TemplateResult => { return html`${user.name}`; }} .value=${(user: User | undefined): number | undefined => { return user?.pk; }} .selected=${(user: User): boolean => { return this.request?.user.toString() === user.pk.toString(); }} >

${t`Set custom attributes using YAML or JSON.`}

${this.result ? this.renderResult() : html``}
`; } }