Browse Source

Add general error handling

Zebulon McCorkle 10 months ago
parent
commit
dfd1856c0b
No account linked to committer's email address
1 changed files with 44 additions and 40 deletions
  1. 44
    40
      src/routes/update.ts

+ 44
- 40
src/routes/update.ts View File

@@ -37,48 +37,52 @@ function transformModification(modification: { [attribute: string]: string[] }):
37 37
 }
38 38
 
39 39
 export default async function update(ctx: Context) {
40
-    const uid = ctx.params.uid;
41
-    const dn = 'uid=' + uid + ',' + (process.env.BASE || 'ou=people,dc=asymptote,dc=network');
42
-    debug('request update of', uid);
43
-    if (!ctx.request.body.currentPassword) {
44
-        ctx.redirect('/info?uid=' + encodeURIComponent(uid) + '&error=' + encodeURIComponent('Password cannot be empty.'));
45
-        return;
46
-    }
47
-    const client = new LdapClient({
48
-        url: 'ldap://ldap.services.asymptote.network:389',
49
-    });
50
-    await new Promise((resolve, reject) => {
51
-        client.client.starttls({}, [], e => e ? reject(e) : resolve());
52
-    });
53 40
     try {
54
-        await client.bind(dn, ctx.request.body.currentPassword);
55
-    } catch (e) {
56
-        if (e.toString().includes('InvalidCredentialsError')) {
57
-            ctx.redirect('/info?uid=' + encodeURIComponent(uid) + '&error=' + encodeURIComponent('Incorrect password.'));
41
+        const uid = ctx.params.uid;
42
+        const dn = 'uid=' + uid + ',' + (process.env.BASE || 'ou=people,dc=asymptote,dc=network');
43
+        debug('request update of', uid);
44
+        if (!ctx.request.body.currentPassword) {
45
+            ctx.redirect('/info?uid=' + encodeURIComponent(uid) + '&error=' + encodeURIComponent('Password cannot be empty.'));
58 46
             return;
59
-        } else {
60
-            throw e;
61 47
         }
48
+        const client = new LdapClient({
49
+            url: 'ldap://ldap.services.asymptote.network:389',
50
+        });
51
+        await new Promise((resolve, reject) => {
52
+            client.client.starttls({}, [], e => e ? reject(e) : resolve());
53
+        });
54
+        try {
55
+            await client.bind(dn, ctx.request.body.currentPassword);
56
+        } catch (e) {
57
+            if (e.toString().includes('InvalidCredentialsError')) {
58
+                ctx.redirect('/info?uid=' + encodeURIComponent(uid) + '&error=' + encodeURIComponent('Incorrect password.'));
59
+                return;
60
+            } else {
61
+                throw e;
62
+            }
63
+        }
64
+        const modification: { [attribute: string]: string[] } = {
65
+            givenName: [ctx.request.body.givenName],
66
+            sn: [ctx.request.body.sn],
67
+            loginShell: [ctx.request.body.loginShell],
68
+            sshPublicKey: [ctx.request.body.sshPublicKey],
69
+        };
70
+    
71
+        if (ctx.request.body.userPassword === ctx.request.body.userPasswordRepeat && ctx.request.body.userPassword != null) {
72
+            modification.userPassword = ['{CRYPT}' + await crypt(ctx.request.body.userPassword, crypt.createSalt('sha512'))];
73
+        } else if (ctx.request.body.userPassword != null || ctx.request.body.userPasswordRepeat != null) {
74
+            ctx.redirect('/info?uid=' + encodeURIComponent(uid) + '&error=' + encodeURIComponent('Passwords do not match.'));
75
+            return;
76
+        }
77
+    
78
+        const changes = transformModification(modification);
79
+        for (const change of changes) {
80
+            debug('changing', change);
81
+            await client.modify(dn, change);
82
+        }
83
+    
84
+        ctx.redirect('/info?uid=' + encodeURIComponent(uid) + '&success=true');
85
+    } catch (e) {
86
+        ctx.redirect('/info?uid=' + encodeURIComponent(ctx.params.uid) + '&error=' + encodeURIComponent('Unknown error: ' + e.toString()));
62 87
     }
63
-    const modification: { [attribute: string]: string[] } = {
64
-        givenName: [ctx.request.body.givenName],
65
-        sn: [ctx.request.body.sn],
66
-        loginShell: [ctx.request.body.loginShell],
67
-        sshPublicKey: [ctx.request.body.sshPublicKey],
68
-    };
69
-
70
-    if (ctx.request.body.userPassword === ctx.request.body.userPasswordRepeat && ctx.request.body.userPassword != null) {
71
-        modification.userPassword = ['{CRYPT}' + await crypt(ctx.request.body.userPassword, crypt.createSalt('sha512'))];
72
-    } else if (ctx.request.body.userPassword != null || ctx.request.body.userPasswordRepeat != null) {
73
-        ctx.redirect('/info?uid=' + encodeURIComponent(uid) + '&error=' + encodeURIComponent('Passwords do not match.'));
74
-        return;
75
-    }
76
-
77
-    const changes = transformModification(modification);
78
-    for (const change of changes) {
79
-        debug('changing', change);
80
-        await client.modify(dn, change);
81
-    }
82
-
83
-    ctx.redirect('/info?uid=' + encodeURIComponent(uid) + '&success=true');
84 88
 }