@@ -49,12 +49,20 @@ module.exports = {
49
49
const svc_cleanEmail = req . services . get ( 'clean-email' ) ;
50
50
const clean_email = svc_cleanEmail . clean ( new_email ) ;
51
51
52
+ if ( ! svc_cleanEmail . validate ( clean_email ) ) {
53
+ throw APIError . create ( 'email_not_allowed' , undefined , {
54
+ email : clean_email ,
55
+ } ) ;
56
+ }
57
+
52
58
// check if email is already in use
53
59
const db = req . services . get ( 'database' ) . get ( DB_WRITE , 'auth' ) ;
54
60
const rows = await db . read (
55
61
'SELECT COUNT(*) AS `count` FROM `user` WHERE (`email` = ? OR `clean_email` = ?) AND `email_confirmed` = 1' ,
56
62
[ new_email , clean_email ]
57
63
) ;
64
+
65
+ // TODO: DRY: signup.js, save_account.js
58
66
if ( rows [ 0 ] . count > 0 ) {
59
67
throw APIError . create ( 'email_already_in_use' , null , { email : new_email } ) ;
60
68
}
@@ -84,6 +92,18 @@ module.exports = {
84
92
[ new_email , token , user . id ]
85
93
) ;
86
94
95
+ // Update email change audit table
96
+ await db . write (
97
+ 'INSERT INTO `user_update_audit` ' +
98
+ '(`user_id`, `user_id_keep`, `old_email`, `new_email`, `reason`) ' +
99
+ 'VALUES (?, ?, ?, ?, ?)' ,
100
+ [
101
+ req . user . id , req . user . id ,
102
+ old_email , new_email ,
103
+ 'change_username'
104
+ ]
105
+ ) ;
106
+
87
107
res . send ( { success : true } ) ;
88
108
}
89
109
} ;
0 commit comments