@@ -45,6 +45,33 @@ struct MailConfirmation {
45
45
pub name : String ,
46
46
}
47
47
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
+
48
75
#[ tracing:: instrument]
49
76
pub fn handle_register_name_and_email ( context : HandleGetContext ) -> AtomicResult < Resource > {
50
77
let HandleGetContext {
@@ -57,7 +84,7 @@ pub fn handle_register_name_and_email(context: HandleGetContext) -> AtomicResult
57
84
let store = context. store ;
58
85
for ( k, v) in subject. query_pairs ( ) {
59
86
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 ) ? ) ,
61
88
"email" => email_option = Some ( EmailAddress :: new ( v. to_string ( ) ) ?) ,
62
89
_ => { }
63
90
}
@@ -68,13 +95,14 @@ pub fn handle_register_name_and_email(context: HandleGetContext) -> AtomicResult
68
95
} ;
69
96
70
97
let name = name_option. ok_or ( "No name provided" ) ?;
98
+ let _validate_name = Value :: new ( & name. to_string ( ) , & crate :: datatype:: DataType :: Slug ) ?;
71
99
let email = email_option. ok_or ( "No email provided" ) ?. check_used ( store) ?;
72
100
73
101
// send the user an e-mail to confirm sign up
74
102
let store_clone = store. clone ( ) ;
75
103
let confirmation_token_struct = MailConfirmation {
76
104
email : email. clone ( ) ,
77
- name : name. clone ( ) ,
105
+ name : name. to_string ( ) ,
78
106
} ;
79
107
let token = crate :: token:: sign_claim ( store, confirmation_token_struct) ?;
80
108
let mut confirm_url = store
0 commit comments