2021-03-29 17:21:48 +00:00
import { CoreApi , EventsApi , NotificationRule , NotificationRuleSeverityEnum } from "authentik-api" ;
2021-04-03 17:26:43 +00:00
import { t } from "@lingui/macro" ;
2021-03-29 17:21:48 +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 { until } from "lit-html/directives/until" ;
@customElement ( "ak-event-rule-form" )
export class RuleForm extends Form < NotificationRule > {
@property ( { attribute : false } )
rule? : NotificationRule ;
getSuccessMessage ( ) : string {
if ( this . rule ) {
2021-04-03 17:26:43 +00:00
return t ` Successfully updated rule. ` ;
2021-03-29 17:21:48 +00:00
} else {
2021-04-03 17:26:43 +00:00
return t ` Successfully created rule. ` ;
2021-03-29 17:21:48 +00:00
}
}
send = ( data : NotificationRule ) : Promise < NotificationRule > = > {
if ( this . rule ) {
return new EventsApi ( DEFAULT_CONFIG ) . eventsRulesUpdate ( {
pbmUuid : this.rule.pk || "" ,
data : data
} ) ;
} else {
return new EventsApi ( DEFAULT_CONFIG ) . eventsRulesCreate ( {
data : data
} ) ;
}
} ;
renderSeverity ( ) : TemplateResult {
return html `
< option value = $ { NotificationRuleSeverityEnum.Alert } ? selected = $ { this.rule ? .severity = = = NotificationRuleSeverityEnum.Alert } >
2021-04-03 17:26:43 +00:00
$ { t ` Alert ` }
2021-03-29 17:21:48 +00:00
< / option >
< option value = $ { NotificationRuleSeverityEnum.Warning } ? selected = $ { this.rule ? .severity = = = NotificationRuleSeverityEnum.Warning } >
2021-04-03 17:26:43 +00:00
$ { t ` Warning ` }
2021-03-29 17:21:48 +00:00
< / option >
< option value = $ { NotificationRuleSeverityEnum.Notice } ? selected = $ { this.rule ? .severity = = = NotificationRuleSeverityEnum.Notice } >
2021-04-03 17:26:43 +00:00
$ { t ` Notice ` }
2021-03-29 17:21:48 +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 17:21:48 +00:00
? required = $ { true }
name = "name" >
< input type = "text" value = "${ifDefined(this.rule?.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
2021-04-03 17:26:43 +00:00
label = $ { t ` Group ` }
2021-03-29 17:21:48 +00:00
name = "group" >
< select class = "pf-c-form-control" >
< option value = "" ? selected = $ { this.rule ? .group = = = undefined } > -- -- -- -- - < / option >
$ { until ( new CoreApi ( DEFAULT_CONFIG ) . coreGroupsList ( { } ) . then ( groups = > {
return groups . results . map ( group = > {
return html ` <option value= ${ ifDefined ( group . pk ) } ?selected= ${ this . rule ? . group ? . groupUuid === group . pk } > ${ group . name } </option> ` ;
} ) ;
2021-04-03 22:24:06 +00:00
} ) , html ` <option> ${ t ` Loading... ` } </option> ` ) }
2021-03-29 17:21:48 +00:00
< / 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 ` Transports ` }
2021-03-29 17:21:48 +00:00
? required = $ { true }
name = "transports" >
< select name = "users" class = "pf-c-form-control" multiple >
$ { until ( new EventsApi ( DEFAULT_CONFIG ) . eventsTransportsList ( { } ) . then ( transports = > {
return transports . results . map ( transport = > {
const selected = Array . from ( this . rule ? . transports || [ ] ) . some ( su = > {
return su . uuid == transport . pk ;
} ) ;
return html ` <option value= ${ ifDefined ( transport . pk ) } ?selected= ${ selected } > ${ transport . name } </option> ` ;
} ) ;
2021-04-03 22:24:06 +00:00
} ) , html ` <option> ${ t ` Loading... ` } </option> ` ) }
2021-03-29 17:21:48 +00:00
< / select >
2021-04-03 17:26:43 +00:00
< p class = "pf-c-form__helper-text" > $ { t ` Select which transports should be used to notify the user. If none are selected, the notification will only be shown in the authentik UI. ` } < / p >
< p class = "pf-c-form__helper-text" > $ { t ` Hold control/command to select multiple items. ` } < / p >
2021-03-29 17:21:48 +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 ` Severity ` }
2021-03-29 17:21:48 +00:00
? required = $ { true }
name = "severity" >
< select class = "pf-c-form-control" >
$ { this . renderSeverity ( ) }
< / select >
< / a k - f o r m - e l e m e n t - h o r i z o n t a l >
< / form > ` ;
}
}