Commit a6448a51 authored by yzx9's avatar yzx9
Browse files

Sync changes from upstream

parent a0e70e4f
Loading
Loading
Loading
Loading
+4 −12
Original line number Diff line number Diff line
@@ -94,9 +94,9 @@ services:

    mongo:
        restart: always
        image: mongo
        image: mongo:4.4
        container_name: mongo
        ports:
        expose:
            - 27017
        volumes:
            - ${MYDATA}/mongo_data:/data/db
@@ -108,17 +108,9 @@ services:

    redis:
        restart: always
        image: redis:5.0.0
        image: redis:6.2
        container_name: redis
        # modify to get rid of the redis issue #35 and #19 with a better solution
        # WARNING: /proc/sys/net/core/somaxconn is set to the lower value of 128.
        # for vm overcommit: enable first on host system 
        # sysctl vm.overcommit_memory=1 (and add it to rc.local)
        # then you do not need it in the redis container
        sysctls:
            - net.core.somaxconn=65535
            # - vm.overcommit_memory=1
        ports:
        expose:
            - 6379
        volumes:
            - ${MYDATA}/redis_data:/data
+525 −191

File changed.

Preview size limit exceeded, changes collapsed.

+118 −127
Original line number Diff line number Diff line
/* eslint-disable
    camelcase,
    max-len,
    no-unused-vars,
/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
 * Modified from 906765c
 * <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 */
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
 * decaffeinate suggestions:
 * DS101: Remove unnecessary use of Array.from
 * DS102: Remove unnecessary code created because of implicit returns
 * DS207: Consider shorter variations of null checks
 * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
 */
let ContactsController
const AuthenticationController = require('../Authentication/AuthenticationController')

const SessionManager = require('../Authentication/SessionManager')
const ContactManager = require('./ContactManager')
const UserGetter = require('../User/UserGetter')
const logger = require('@overleaf/logger')
const Modules = require('../../infrastructure/Modules')
const { Client } = require('ldapts');

module.exports = ContactsController = {
  getContacts(req, res, next) {
    const user_id = SessionManager.getLoggedInUserId(req.session)
    return ContactManager.getContactIds(
      user_id,
      { limit: 50 },
      function (error, contact_ids) {
        if (error != null) {
          return next(error)
const { expressify } = require('../../util/promises')

// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
const { Client } = require('ldapts')
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

function _formatContact(contact) {
  return {
    id: contact._id?.toString(),
    email: contact.email || '',
    first_name: contact.first_name || '',
    last_name: contact.last_name || '',
    type: 'user',
  }
}
        return UserGetter.getUsers(
          contact_ids,
          {

async function getContacts(req, res) {
  const userId = SessionManager.getLoggedInUserId(req.session)

  const contactIds = await ContactManager.promises.getContactIds(userId, {
    limit: 50,
  })

  let contacts = await UserGetter.promises.getUsers(contactIds, {
    email: 1,
    first_name: 1,
    last_name: 1,
    holdingAccount: 1,
          },
          function (error, contacts) {
            if (error != null) {
              return next(error)
            }
  })

  // 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]
  for (let i = 0; i < contactIds.length; i++) {
    const contact_id = contactIds[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]
    (a, b) => positions[a._id?.toString()] - positions[b._id?.toString()]
  )

  // 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 = contacts.filter((c) => !c.holdingAccount)

// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  const ldapcontacts = getLdapContacts(contacts)
  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))
  contacts = contacts.map(_formatContact)

        }
  const additionalContacts = await Modules.promises.hooks.fire(
    'getContacts',
    userId,
    contacts
  )

  contacts = contacts.concat(...(additionalContacts || []))
  return res.json({
    contacts,
  })
  },
  async getLdapContacts(contacts) {
    if (process.env.LDAP_CONTACTS === undefined || !(process.env.LDAP_CONTACTS.toLowerCase() === 'true')) {
}

// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
async function 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);
      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) )
      console.log('Could not bind LDAP reader user: ' + String(ex))
    }
  }

@@ -101,39 +95,36 @@ module.exports = ContactsController = {
  // 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;
    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];
      var obj = searchEntries[i]
      entry['_id'] = undefined
      entry['email'] = obj['mail']
      entry['first_name'] = obj['givenName']
      entry['last_name'] = obj['sn']
       entry['type'] = "user"
      entry['type'] = 'user'
      // Only add to contacts if entry is not there.
      if (contacts.indexOf(entry) === -1) {
          contacts.push(entry);
        contacts.push(entry)
      }
    }
  } catch (ex) {
    console.log(String(ex))
    }  
  } finally {
    // 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
  }
  },
  _formatContact(contact) {
    return {
      id: contact._id != null ? contact._id.toString() : undefined,
      email: contact.email || '',
      first_name: contact.first_name || '',
      last_name: contact.last_name || '',
      type: 'user',
}
  },
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

module.exports = {
  getContacts: expressify(getContacts),
}