@@ -50,16 +50,22 @@ fn get_variables(
5050 auth_action : AuthAction ,
5151) -> Result < Map < String , Value > > {
5252 let mut variables: Map < String , Value > = Default :: default ( ) ;
53- variables. insert (
54- "user" . to_string ( ) ,
55- json ! ( {
56- "first_name" : user. first_name. clone( ) ,
57- "last_name" : user. last_name. clone( ) ,
58- "username" : user. username. clone( ) ,
59- "first_name" : user. first_name. clone( ) ,
60- "email" : user. email. clone( ) ,
61- } ) ,
62- ) ;
53+ let mut user_variables = Map :: new ( ) ;
54+ user_variables. insert ( "first_name" . to_string ( ) , json ! ( user. first_name) ) ;
55+ user_variables. insert ( "last_name" . to_string ( ) , json ! ( user. last_name) ) ;
56+ user_variables. insert ( "username" . to_string ( ) , json ! ( user. username) ) ;
57+ user_variables. insert ( "email" . to_string ( ) , json ! ( user. email) ) ;
58+
59+ let attributes = user. attributes . clone ( ) . unwrap_or_default ( ) ;
60+ for ( attribute_name, values) in & attributes {
61+ if let Some ( first_value) = values. first ( ) {
62+ user_variables
63+ . entry ( attribute_name. clone ( ) )
64+ . or_insert_with ( || json ! ( first_value) ) ;
65+ }
66+ }
67+ user_variables. insert ( "attributes" . to_string ( ) , json ! ( attributes) ) ;
68+ variables. insert ( "user" . to_string ( ) , Value :: Object ( user_variables) ) ;
6369 variables. insert ( "tenant_id" . to_string ( ) , json ! ( tenant_id. clone( ) ) ) ;
6470 if let Some ( ref election_event) = election_event {
6571 let default_language = election_event. get_default_language ( ) ;
@@ -644,3 +650,47 @@ pub async fn send_template_email_or_sms(
644650 }
645651 }
646652}
653+
654+ #[ cfg( test) ]
655+ mod tests {
656+ use super :: get_variables;
657+ use sequent_core:: services:: generate_urls:: AuthAction ;
658+ use sequent_core:: types:: keycloak:: User ;
659+ use serde_json:: json;
660+ use std:: collections:: HashMap ;
661+
662+ #[ test]
663+ fn get_variables_exposes_dynamic_and_multivalued_user_attributes ( ) {
664+ let user = User {
665+ username : Some ( "canonical-user" . to_string ( ) ) ,
666+ attributes : Some ( HashMap :: from ( [
667+ (
668+ "dateOfBirth" . to_string ( ) ,
669+ vec ! [ "2000-01-01" . to_string( ) , "ignored-first-value" . to_string( ) ] ,
670+ ) ,
671+ (
672+ "username" . to_string ( ) ,
673+ vec ! [ "untrusted-collision" . to_string( ) ] ,
674+ ) ,
675+ ( "empty" . to_string ( ) , Vec :: new ( ) ) ,
676+ ] ) ) ,
677+ ..User :: default ( )
678+ } ;
679+
680+ let variables = get_variables ( & user, None , "tenant-id" . to_string ( ) , AuthAction :: Login )
681+ . expect ( "variables should be generated" ) ;
682+
683+ assert_eq ! ( variables[ "user" ] [ "username" ] , json!( "canonical-user" ) ) ;
684+ assert_eq ! ( variables[ "user" ] [ "dateOfBirth" ] , json!( "2000-01-01" ) ) ;
685+ assert ! ( variables[ "user" ] . get( "empty" ) . is_none( ) ) ;
686+ assert_eq ! (
687+ variables[ "user" ] [ "attributes" ] [ "dateOfBirth" ] ,
688+ json!( [ "2000-01-01" , "ignored-first-value" ] )
689+ ) ;
690+ assert_eq ! (
691+ variables[ "user" ] [ "attributes" ] [ "username" ] ,
692+ json!( [ "untrusted-collision" ] )
693+ ) ;
694+ assert_eq ! ( variables[ "user" ] [ "attributes" ] [ "empty" ] , json!( [ ] ) ) ;
695+ }
696+ }
0 commit comments