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-05-11 10:12:31 +00:00
import { customElement } from "lit-element" ;
2021-03-29 17:21:48 +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" ;
import { until } from "lit-html/directives/until" ;
2021-05-11 09:55:25 +00:00
import { ModelForm } from "../../elements/forms/ModelForm" ;
2021-03-29 17:21:48 +00:00
@customElement ( "ak-event-rule-form" )
2021-05-11 09:55:25 +00:00
export class RuleForm extends ModelForm < NotificationRule , string > {
2021-03-29 17:21:48 +00:00
2021-05-11 09:55:25 +00:00
loadInstance ( pk : string ) : Promise < NotificationRule > {
return new EventsApi ( DEFAULT_CONFIG ) . eventsRulesRead ( {
pbmUuid : pk ,
} ) ;
}
2021-03-29 17:21:48 +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 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 > = > {
2021-05-11 09:55:25 +00:00
if ( this . instance ) {
2021-03-29 17:21:48 +00:00
return new EventsApi ( DEFAULT_CONFIG ) . eventsRulesUpdate ( {
2021-05-11 09:55:25 +00:00
pbmUuid : this.instance.pk || "" ,
2021-03-29 17:21:48 +00:00
data : data
} ) ;
} else {
return new EventsApi ( DEFAULT_CONFIG ) . eventsRulesCreate ( {
data : data
} ) ;
}
} ;
renderSeverity ( ) : TemplateResult {
return html `
2021-05-11 09:55:25 +00:00
< option value = $ { NotificationRuleSeverityEnum.Alert } ? selected = $ { this.instance ? .severity = = = NotificationRuleSeverityEnum.Alert } >
2021-04-03 17:26:43 +00:00
$ { t ` Alert ` }
2021-03-29 17:21:48 +00:00
< / option >
2021-05-11 09:55:25 +00:00
< option value = $ { NotificationRuleSeverityEnum.Warning } ? selected = $ { this.instance ? .severity = = = NotificationRuleSeverityEnum.Warning } >
2021-04-03 17:26:43 +00:00
$ { t ` Warning ` }
2021-03-29 17:21:48 +00:00
< / option >
2021-05-11 09:55:25 +00:00
< option value = $ { NotificationRuleSeverityEnum.Notice } ? selected = $ { this.instance ? .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" >
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 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 ` Group ` }
2021-03-29 17:21:48 +00:00
name = "group" >
< select class = "pf-c-form-control" >
2021-05-11 09:55:25 +00:00
< option value = "" ? selected = $ { this.instance ? .group = = = undefined } > -- -- -- -- - < / option >
2021-03-29 17:21:48 +00:00
$ { until ( new CoreApi ( DEFAULT_CONFIG ) . coreGroupsList ( { } ) . then ( groups = > {
return groups . results . map ( group = > {
2021-05-12 14:41:15 +00:00
return html ` <option value= ${ ifDefined ( group . pk ) } ?selected= ${ this . instance ? . group === group . pk } > ${ group . name } </option> ` ;
2021-03-29 17:21:48 +00:00
} ) ;
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 = > {
2021-05-11 09:55:25 +00:00
const selected = Array . from ( this . instance ? . transports || [ ] ) . some ( su = > {
2021-05-12 14:41:15 +00:00
return su == transport . pk ;
2021-03-29 17:21:48 +00:00
} ) ;
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 > ` ;
}
}