2021-03-31 12:14:56 +00:00
import { Flow , FlowDesignationEnum , FlowPolicyEngineModeEnum , FlowsApi } from "authentik-api" ;
2021-04-03 17:26:43 +00:00
import { t } from "@lingui/macro" ;
2021-05-11 10:12:31 +00:00
import { customElement } from "lit-element" ;
2021-03-29 20:24:46 +00:00
import { html , TemplateResult } from "lit-html" ;
import { DEFAULT_CONFIG } from "../../api/Config" ;
import { ifDefined } from "lit-html/directives/if-defined" ;
import "../../elements/forms/HorizontalFormElement" ;
2021-05-11 09:55:25 +00:00
import { ModelForm } from "../../elements/forms/ModelForm" ;
2021-03-29 20:24:46 +00:00
@customElement ( "ak-flow-form" )
2021-05-11 09:55:25 +00:00
export class FlowForm extends ModelForm < Flow , string > {
2021-03-29 20:24:46 +00:00
2021-05-11 09:55:25 +00:00
loadInstance ( pk : string ) : Promise < Flow > {
return new FlowsApi ( DEFAULT_CONFIG ) . flowsInstancesRead ( {
slug : pk ,
} ) ;
}
2021-03-29 20:24:46 +00:00
getSuccessMessage ( ) : string {
2021-05-11 09:55:25 +00:00
if ( this . instance ) {
2021-04-03 17:26:43 +00:00
return t ` Successfully updated flow. ` ;
2021-03-29 20:24:46 +00:00
} else {
2021-04-03 17:26:43 +00:00
return t ` Successfully created flow. ` ;
2021-03-29 20:24:46 +00:00
}
}
send = ( data : Flow ) : Promise < void | Flow > = > {
let writeOp : Promise < Flow > ;
2021-05-11 09:55:25 +00:00
if ( this . instance ) {
2021-03-29 20:24:46 +00:00
writeOp = new FlowsApi ( DEFAULT_CONFIG ) . flowsInstancesUpdate ( {
2021-05-11 09:55:25 +00:00
slug : this.instance.slug ,
2021-03-29 20:24:46 +00:00
data : data
} ) ;
} else {
writeOp = new FlowsApi ( DEFAULT_CONFIG ) . flowsInstancesCreate ( {
data : data
} ) ;
}
const background = this . getFormFile ( ) ;
if ( background ) {
return writeOp . then ( flow = > {
return new FlowsApi ( DEFAULT_CONFIG ) . flowsInstancesSetBackground ( {
slug : flow.slug ,
file : background
} ) ;
} ) ;
}
return writeOp ;
} ;
renderDesignations ( ) : TemplateResult {
return html `
2021-05-11 09:55:25 +00:00
< option value = $ { FlowDesignationEnum.Authentication } ? selected = $ { this.instance ? .designation = = = FlowDesignationEnum.Authentication } >
2021-04-03 17:26:43 +00:00
$ { t ` Authentication ` }
2021-03-29 20:24:46 +00:00
< / option >
2021-05-11 09:55:25 +00:00
< option value = $ { FlowDesignationEnum.Authorization } ? selected = $ { this.instance ? .designation = = = FlowDesignationEnum.Authorization } >
2021-04-03 17:26:43 +00:00
$ { t ` Authorization ` }
2021-03-29 20:24:46 +00:00
< / option >
2021-05-11 09:55:25 +00:00
< option value = $ { FlowDesignationEnum.Enrollment } ? selected = $ { this.instance ? .designation = = = FlowDesignationEnum.Enrollment } >
2021-04-03 17:26:43 +00:00
$ { t ` Enrollment ` }
2021-03-29 20:24:46 +00:00
< / option >
2021-05-11 09:55:25 +00:00
< option value = $ { FlowDesignationEnum.Invalidation } ? selected = $ { this.instance ? .designation = = = FlowDesignationEnum.Invalidation } >
2021-04-03 17:26:43 +00:00
$ { t ` Invalidation ` }
2021-03-29 20:24:46 +00:00
< / option >
2021-05-11 09:55:25 +00:00
< option value = $ { FlowDesignationEnum.Recovery } ? selected = $ { this.instance ? .designation = = = FlowDesignationEnum.Recovery } >
2021-04-03 17:26:43 +00:00
$ { t ` Recovery ` }
2021-03-29 20:24:46 +00:00
< / option >
2021-05-11 09:55:25 +00:00
< option value = $ { FlowDesignationEnum.StageConfiguration } ? selected = $ { this.instance ? .designation = = = FlowDesignationEnum.StageConfiguration } >
2021-04-03 17:26:43 +00:00
$ { t ` Stage Configuration ` }
2021-03-29 20:24:46 +00:00
< / option >
2021-05-11 09:55:25 +00:00
< option value = $ { FlowDesignationEnum.Unenrollment } ? selected = $ { this.instance ? .designation = = = FlowDesignationEnum.Unenrollment } >
2021-04-03 17:26:43 +00:00
$ { t ` Unenrollment ` }
2021-03-29 20:24:46 +00:00
< / option >
` ;
}
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-03-29 20:24:46 +00:00
? required = $ { true }
name = "name" >
2021-05-11 09:55:25 +00:00
< input type = "text" value = "${ifDefined(this.instance?.name)}" class = "pf-c-form-control" required >
2021-03-29 20:24:46 +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 - element - horizontal
2021-04-03 17:26:43 +00:00
label = $ { t ` Title ` }
2021-03-29 20:24:46 +00:00
? required = $ { true }
name = "title" >
2021-05-11 09:55:25 +00:00
< input type = "text" value = "${ifDefined(this.instance?.title)}" class = "pf-c-form-control" required >
2021-04-03 17:26:43 +00:00
< p class = "pf-c-form__helper-text" > $ { t ` Shown as the Title in Flow pages. ` } < / p >
2021-03-29 20:24:46 +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 - element - horizontal
2021-04-04 11:19:52 +00:00
label = $ { t ` Slug ` }
2021-03-29 20:24:46 +00:00
? required = $ { true }
name = "slug" >
2021-05-11 09:55:25 +00:00
< input type = "text" value = "${ifDefined(this.instance?.slug)}" class = "pf-c-form-control" required >
2021-04-03 17:26:43 +00:00
< p class = "pf-c-form__helper-text" > $ { t ` Visible in the URL. ` } < / p >
2021-03-29 20:24:46 +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 - element - horizontal
2021-04-03 17:26:43 +00:00
label = $ { t ` Policy engine mode ` }
2021-03-31 12:14:56 +00:00
? required = $ { true }
name = "policyEngineMode" >
< select class = "pf-c-form-control" >
2021-05-11 09:55:25 +00:00
< option value = $ { FlowPolicyEngineModeEnum.Any } ? selected = $ { this.instance ? .policyEngineMode = = = FlowPolicyEngineModeEnum.Any } >
2021-04-03 17:26:43 +00:00
$ { t ` ANY, any policy must match to grant access. ` }
2021-03-31 12:14:56 +00:00
< / option >
2021-05-11 09:55:25 +00:00
< option value = $ { FlowPolicyEngineModeEnum.All } ? selected = $ { this.instance ? .policyEngineMode = = = FlowPolicyEngineModeEnum.All } >
2021-04-03 17:26:43 +00:00
$ { t ` ALL, all policies must match to grant access. ` }
2021-03-31 12:14:56 +00:00
< / option >
< / select >
< / 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
2021-04-03 17:26:43 +00:00
label = $ { t ` Designation ` }
2021-03-29 20:24:46 +00:00
? required = $ { true }
name = "designation" >
< select class = "pf-c-form-control" >
2021-05-11 09:55:25 +00:00
< option value = "" ? selected = $ { this.instance ? .designation = = = undefined } > -- -- -- -- - < / option >
2021-03-29 20:24:46 +00:00
$ { this . renderDesignations ( ) }
< / select >
2021-04-03 17:26:43 +00:00
< p class = "pf-c-form__helper-text" > $ { t ` Decides what this Flow is used for. For example, the Authentication flow is redirect to when an un-authenticated user visits authentik. ` } < / p >
2021-03-29 20:24:46 +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 - element - horizontal
2021-04-03 17:26:43 +00:00
label = $ { t ` Background ` }
2021-03-29 20:24:46 +00:00
name = "background" >
2021-05-11 09:55:25 +00:00
< input type = "file" value = "${ifDefined(this.instance?.background)}" class = "pf-c-form-control" >
2021-04-03 17:26:43 +00:00
< p class = "pf-c-form__helper-text" > $ { t ` Background shown during execution. ` } < / p >
2021-03-29 20:24:46 +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 >
< / form > ` ;
}
}