2021-04-02 15:12:21 +00:00
import { ExpressionPolicy , PoliciesApi } from "authentik-api" ;
2021-04-03 17:26:43 +00:00
import { t } from "@lingui/macro" ;
2021-04-02 14:42:30 +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 { ifDefined } from "lit-html/directives/if-defined" ;
import "../../../elements/forms/HorizontalFormElement" ;
import "../../../elements/forms/FormGroup" ;
2021-04-02 15:12:21 +00:00
import "../../../elements/CodeMirror" ;
2021-04-02 14:42:30 +00:00
@customElement ( "ak-policy-expression-form" )
export class ExpressionPolicyForm extends Form < ExpressionPolicy > {
set policyUUID ( value : string ) {
new PoliciesApi ( DEFAULT_CONFIG ) . policiesExpressionRead ( {
policyUuid : value ,
} ) . then ( policy = > {
this . policy = policy ;
} ) ;
}
@property ( { attribute : false } )
policy? : ExpressionPolicy ;
getSuccessMessage ( ) : string {
if ( this . policy ) {
2021-04-03 17:26:43 +00:00
return t ` Successfully updated policy. ` ;
2021-04-02 14:42:30 +00:00
} else {
2021-04-03 17:26:43 +00:00
return t ` Successfully created policy. ` ;
2021-04-02 14:42:30 +00:00
}
}
send = ( data : ExpressionPolicy ) : Promise < ExpressionPolicy > = > {
if ( this . policy ) {
return new PoliciesApi ( DEFAULT_CONFIG ) . policiesExpressionUpdate ( {
policyUuid : this.policy.pk || "" ,
data : data
} ) ;
} else {
return new PoliciesApi ( DEFAULT_CONFIG ) . policiesExpressionCreate ( {
data : data
} ) ;
}
} ;
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 ` Name ` }
2021-04-02 14:42:30 +00:00
? required = $ { true }
name = "name" >
< input type = "text" value = "${ifDefined(this.policy?.name || " " ) } " class = "pf-c-form-control" required >
< / a k - f o r m - e l e m e n t - h o r i z o n t a l >
< ak - form - element - horizontal name = "executionLogging" >
< div class = "pf-c-check" >
< input type = "checkbox" class = "pf-c-check__input" ? checked = $ { this.policy ? .executionLogging | | false } >
< label class = "pf-c-check__label" >
2021-04-03 17:26:43 +00:00
$ { t ` Execution logging ` }
2021-04-02 14:42:30 +00:00
< / label >
< / div >
2021-04-03 17:26:43 +00:00
< p class = "pf-c-form__helper-text" > $ { t ` When this option is enabled, all executions of this policy will be logged. By default, only execution errors are logged. ` } < / p >
2021-04-02 14:42:30 +00:00
< / a k - f o r m - e l e m e n t - h o r i z o n t a l >
< ak - form - group .expanded = $ { true } >
< span slot = "header" >
2021-04-03 17:26:43 +00:00
$ { t ` Policy-specific settings ` }
2021-04-02 14:42:30 +00:00
< / span >
< div slot = "body" class = "pf-c-form" >
< ak - form - element - horizontal
2021-04-03 17:26:43 +00:00
label = $ { t ` Expression ` }
2021-04-02 14:42:30 +00:00
name = "expression" >
< ak - codemirror mode = "python" value = "${ifDefined(this.policy?.expression)}" >
< / a k - c o d e m i r r o r >
< p class = "pf-c-form__helper-text" >
2021-04-03 18:42:08 +00:00
$ { t ` Expression using Python. ` }
< a href = "https://goauthentik.io/docs/property-mappings/expression/" >
$ { t ` See documentation for a list of all variables. ` }
< / a >
2021-04-02 14:42:30 +00:00
< / p >
< / a k - f o r m - e l e m e n t - h o r i z o n t a l >
< / div >
< / a k - f o r m - g r o u p >
< / form > ` ;
}
}