aboutsummaryrefslogtreecommitdiff
path: root/overleaf-mods/overleaf-ldap-oauth2/ldap-overleaf-sl/sharelatex/contactcontroller.diff
blob: 0aa4199de9689fffc154599c58a338c4a6f4d01f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
16d15
< const AuthenticationController = require('../Authentication/AuthenticationController')
20c19
< const logger = require('logger-sharelatex')
---
> const logger = require('@overleaf/logger')
22d20
< const { Client } = require('ldapts');
26d23
<     // const user_id = AuthenticationController.getLoggedInUserId(req)
48,78c45,55
<           // UserGetter.getUsers may not preserve order so put them back in order
<           const positions = {}
<           for (let i = 0; i < contact_ids.length; i++) {
<             const contact_id = contact_ids[i]
<             positions[contact_id] = i
<           }
< 	  
<           contacts.sort(
<             (a, b) =>
<               positions[a._id != null ? a._id.toString() : undefined] -
<               positions[b._id != null ? b._id.toString() : undefined]
<           )
< 
<           // Don't count holding accounts to discourage users from repeating mistakes (mistyped or wrong emails, etc)
<           contacts = contacts.filter(c => !c.holdingAccount)
< 	  ContactsController.getLdapContacts(contacts).then((ldapcontacts) => { 
< 	    contacts.push(ldapcontacts)
<             contacts = contacts.map(ContactsController._formatContact)
<             return Modules.hooks.fire('getContacts', user_id, contacts, function(
<               error,
<               additional_contacts
<             ) {
<               if (error != null) {
<                 return next(error)
<               }
<               contacts = contacts.concat(...Array.from(additional_contacts || []))
<               return res.send({
<                 contacts
<               })
<             })
<   	  }).catch(e => console.log("Error appending ldap contacts" + e))
---
>             // UserGetter.getUsers may not preserve order so put them back in order
>             const positions = {}
>             for (let i = 0; i < contact_ids.length; i++) {
>               const contact_id = contact_ids[i]
>               positions[contact_id] = i
>             }
>             contacts.sort(
>               (a, b) =>
>                 positions[a._id != null ? a._id.toString() : undefined] -
>                 positions[b._id != null ? b._id.toString() : undefined]
>             )
80,99c57,60
<         }
<       )
<     })
<   },
<   async getLdapContacts(contacts) {
<     if (process.env.LDAP_CONTACTS === undefined || !(process.env.LDAP_CONTACTS.toLowerCase() === 'true')) {
<        return contacts
<     }
<     const client = new Client({
<     url: process.env.LDAP_SERVER,
<     });
< 
<     // if we need a ldap user try to bind
<     if (process.env.LDAP_BIND_USER) {
<       try {
<         await client.bind(process.env.LDAP_BIND_USER, process.env.LDAP_BIND_PW);
<       } catch (ex) {
<         console.log("Could not bind LDAP reader user: " + String(ex) )
<       }
<     }
---
>             // Don't count holding accounts to discourage users from repeating mistakes (mistyped or wrong emails, etc)
>             contacts = contacts.filter(c => !c.holdingAccount)
> 
>             contacts = contacts.map(ContactsController._formatContact)
101,118c62,79
<     const ldap_base = process.env.LDAP_BASE
<     // get user data
<     try {
<       // if you need an client.bind do it here.
<       const {searchEntries,searchReferences,} = await client.search(ldap_base, {scope: 'sub',filter: process.env.LDAP_CONTACT_FILTER ,});
<       await searchEntries;
<       for (var i = 0; i < searchEntries.length; i++) {
<        var entry = new Map()
<        var obj = searchEntries[i];
<        entry['_id'] = undefined
<        entry['email'] = obj['mail']
<        entry['first_name'] = obj['givenName']
<        entry['last_name'] = obj['sn']
<        entry['type'] = "user"
<        // Only add to contacts if entry is not there.
<        if(contacts.indexOf(entry) === -1) {
<           contacts.push(entry);
<        }
---
>             return Modules.hooks.fire(
>               'getContacts',
>               user_id,
>               contacts,
>               function (error, additional_contacts) {
>                 if (error != null) {
>                   return next(error)
>                 }
>                 contacts = contacts.concat(
>                   ...Array.from(additional_contacts || [])
>                 )
>                 return res.json({
>                   contacts,
>                 })
>               }
>             )
>           }
>         )
120,129c81
<     } catch (ex) {
<       console.log(String(ex))
<     }  
<     //console.log(JSON.stringify(contacts)) 
<     finally {
<      // even if we did not use bind - the constructor of 
<      // new Client() opens a socket to the ldap server
<      client.unbind()
<      return contacts
<     }
---
>     )
130a83
>