@@ -45,6 +45,33 @@ struct MailConfirmation {
4545 pub name : String ,
4646}
4747
48+ #[ derive( Debug , Clone ) ]
49+ struct UserName {
50+ pub name : String ,
51+ }
52+
53+ impl UserName {
54+ /// Throws error if email address is already taken
55+ pub fn check_used ( name : & str , store : & impl Storelike ) -> AtomicResult < Self > {
56+ let mut drive_url = store
57+ . get_self_url ( )
58+ . ok_or ( "No self url, cant check name" ) ?
59+ . clone ( ) ;
60+ drive_url. set_subdomain ( Some ( name) ) ?;
61+
62+ match store. get_resource ( & drive_url. to_string ( ) ) {
63+ Ok ( _) => Err ( "Name already used" . into ( ) ) ,
64+ Err ( _) => Ok ( Self { name : name. into ( ) } ) ,
65+ }
66+ }
67+ }
68+
69+ impl std:: fmt:: Display for UserName {
70+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
71+ write ! ( f, "{}" , self . name)
72+ }
73+ }
74+
4875#[ tracing:: instrument]
4976pub fn handle_register_name_and_email ( context : HandleGetContext ) -> AtomicResult < Resource > {
5077 let HandleGetContext {
@@ -57,7 +84,7 @@ pub fn handle_register_name_and_email(context: HandleGetContext) -> AtomicResult
5784 let store = context. store ;
5885 for ( k, v) in subject. query_pairs ( ) {
5986 match k. as_ref ( ) {
60- "name" | urls:: NAME => name_option = Some ( v . to_string ( ) ) ,
87+ "name" | urls:: NAME => name_option = Some ( UserName :: check_used ( & v , store ) ? ) ,
6188 "email" => email_option = Some ( EmailAddress :: new ( v. to_string ( ) ) ?) ,
6289 _ => { }
6390 }
@@ -68,13 +95,14 @@ pub fn handle_register_name_and_email(context: HandleGetContext) -> AtomicResult
6895 } ;
6996
7097 let name = name_option. ok_or ( "No name provided" ) ?;
98+ let _validate_name = Value :: new ( & name. to_string ( ) , & crate :: datatype:: DataType :: Slug ) ?;
7199 let email = email_option. ok_or ( "No email provided" ) ?. check_used ( store) ?;
72100
73101 // send the user an e-mail to confirm sign up
74102 let store_clone = store. clone ( ) ;
75103 let confirmation_token_struct = MailConfirmation {
76104 email : email. clone ( ) ,
77- name : name. clone ( ) ,
105+ name : name. to_string ( ) ,
78106 } ;
79107 let token = crate :: token:: sign_claim ( store, confirmation_token_struct) ?;
80108 let mut confirm_url = store
0 commit comments