2021-03-29 14:16:27 +00:00
import { CoreApi , User } from "authentik-api" ;
import { gettext } from "django" ;
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/CodeMirror" ;
import YAML from "yaml" ;
@customElement ( "ak-user-form" )
export class UserForm extends Form < User > {
@property ( { attribute : false } )
user? : User ;
getSuccessMessage ( ) : string {
if ( this . user ) {
return gettext ( "Successfully updated user." ) ;
} else {
return gettext ( "Successfully created user." ) ;
}
}
send = ( data : User ) : Promise < User > = > {
if ( this . user ) {
return new CoreApi ( DEFAULT_CONFIG ) . coreUsersUpdate ( {
id : this.user.pk || 0 ,
data : data
} ) ;
} else {
return new CoreApi ( DEFAULT_CONFIG ) . coreUsersCreate ( {
data : data
} ) ;
}
} ;
renderForm ( ) : TemplateResult {
return html ` <form class="pf-c-form pf-m-horizontal">
2021-03-29 16:22:15 +00:00
< ak - form - element - horizontal
label = $ { gettext ( "Username" ) }
? required = $ { true }
name = "username" >
2021-03-29 16:25:59 +00:00
< input type = "text" value = "${ifDefined(this.user?.username)}" class = "pf-c-form-control" required >
2021-03-29 14:16:27 +00:00
< p class = "pf-c-form__helper-text" > $ { gettext ( "Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only." ) } < / p >
< / a k - f o r m - e l e m e n t - h o r i z o n t a l >
2021-03-29 16:22:15 +00:00
< ak - form - element - horizontal
label = $ { gettext ( "Name" ) }
? required = $ { true }
name = "name" >
2021-03-29 16:25:59 +00:00
< input type = "text" value = "${ifDefined(this.user?.name)}" class = "pf-c-form-control" required >
2021-03-29 14:16:27 +00:00
< p class = "pf-c-form__helper-text" > $ { gettext ( "User's display name." ) } < / p >
< / a k - f o r m - e l e m e n t - h o r i z o n t a l >
2021-03-29 16:22:15 +00:00
< ak - form - element - horizontal
label = $ { gettext ( "Email" ) }
? required = $ { true }
name = "email" >
2021-03-29 16:25:59 +00:00
< input type = "email" autocomplete = "off" value = "${ifDefined(this.user?.email)}" class = "pf-c-form-control" required >
2021-03-29 14:16:27 +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 >
2021-03-29 16:22:15 +00:00
< ak - form - element - horizontal
name = "isActive" >
2021-03-29 14:16:27 +00:00
< div class = "pf-c-check" >
2021-03-29 16:22:15 +00:00
< input type = "checkbox" class = "pf-c-check__input" ? checked = $ { this.user ? .isActive | | false } >
2021-03-29 14:16:27 +00:00
< label class = "pf-c-check__label" >
$ { gettext ( "Is active" ) }
< / label >
< / div >
< p class = "pf-c-form__helper-text" > $ { gettext ( "Designates whether this user should be treated as active. Unselect this instead of deleting accounts." ) } < / p >
< / a k - f o r m - e l e m e n t - h o r i z o n t a l >
2021-03-29 16:22:15 +00:00
< ak - form - element - horizontal
label = $ { gettext ( "Attributes" ) }
name = "attributes" >
< ak - codemirror mode = "yaml" value = "${YAML.stringify(this.user?.attributes)}" >
2021-03-29 14:16:27 +00:00
< / a k - c o d e m i r r o r >
< / a k - f o r m - e l e m e n t - h o r i z o n t a l >
< / form > ` ;
}
}