From 9b38d70fe13a5fbaf4702071b4b4fffb314406b8 Mon Sep 17 00:00:00 2001 From: WikiRik Date: Thu, 15 Jul 2021 22:08:54 +0000 Subject: [PATCH] fix(gsuite): implemented few TODOs --- lib/cron.js | 4 ++++ middlewares/campaigns.js | 1 + middlewares/members.js | 26 +++++++++++++++++++++----- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/cron.js b/lib/cron.js index 85c5dc59..c02278dc 100644 --- a/lib/cron.js +++ b/lib/cron.js @@ -1,4 +1,5 @@ const cron = require('node-cron'); +const superagent = require('superagent'); const _ = require('lodash'); const logger = require('./logger'); @@ -26,6 +27,9 @@ const JobCallbacks = { await confirmation.destroy(); // TODO: check that GSuite account is also deleted + if (user.gsuite_id) { + await superagent.delete('gsuite-wrapper:8084/accounts/' + user.gsuite_id); + } await user.destroy(); logger.info('Deleted.'); diff --git a/middlewares/campaigns.js b/middlewares/campaigns.js index 8016d64c..85fda5bc 100644 --- a/middlewares/campaigns.js +++ b/middlewares/campaigns.js @@ -36,6 +36,7 @@ exports.registerUser = async (req, res) => { }); // TODO: add uniqueness check; transliterate gsuiteEmail in case of umlauts or other special characters + // TODO: probably move this elsewhere since not all MyAEGEE users need a GSuite account const gsuiteEmail = req.body.first_name + '.' + req.body.last_name + '@' + constants.GSUITE_DOMAIN; const payload = { primaryEmail: gsuiteEmail.toLowerCase().replace(' ', ''), diff --git a/middlewares/members.js b/middlewares/members.js index 8b70ee58..45859a48 100644 --- a/middlewares/members.js +++ b/middlewares/members.js @@ -2,6 +2,7 @@ const moment = require('moment'); const _ = require('lodash'); const superagent = require('superagent'); +const crypto = require('crypto'); const { User, Body, MailChange, MailConfirmation } = require('../models'); const constants = require('../lib/constants'); const helpers = require('../lib/helpers'); @@ -125,7 +126,7 @@ exports.updateUser = async (req, res) => { await req.currentUser.update(req.body, { fields: constants.FIELDS_TO_UPDATE.USER.UPDATE }); // TODO: update first/late name to GSuite account (if there is an account attached) - if (req.body.first_name || req.body.last_name) { + if (req.currentUser.gsuite_id && (req.body.first_name || req.body.last_name)) { const payload = { name: { givenName: req.body.first_name || req.currentUser.first_name, @@ -147,8 +148,12 @@ exports.deleteUser = async (req, res) => { } // TODO: if user gets deleted the gsuite account also gets deleted (if there was an account attached) - await superagent.delete('gsuite-wrapper:8084/accounts/' + req.currentUser.gsuite_id); + if (req.currentUser.gsuite_id) { + await superagent.delete('gsuite-wrapper:8084/accounts/' + req.currentUser.gsuite_id); + } + await req.currentUser.destroy(); + return res.json({ success: true, message: 'User is deleted.' @@ -167,8 +172,14 @@ exports.setUserPassword = async (req, res) => { } await userWithPassword.update({ password: req.body.password }); + // TODO: password should be sent to gsuite-wrapper - // await superagent.post('gsuite-wrapper:8084/accounts?SETPASS') + if (req.currentUser.gsuite_id) { + const payload = { + password: crypto.createHash('sha1').update(JSON.stringify(req.body.password)).digest('hex') + }; + await superagent.put('gsuite-wrapper:8084/accounts/' + req.currentUser.gsuite_id, payload); + } // TODO: add a mail that the password was changed. @@ -204,7 +215,10 @@ exports.confirmUser = async (req, res) => { await confirmation.destroy(); - await superagent.put('gsuite-wrapper:8084/accounts/'+req.currentUser.gsuite_id, { 'suspended': false } ); + // TODO: probably move this to another method since not all MyAEGEE members should have a GSuite account + if (req.currentUser.gsuite_id) { + await superagent.put('gsuite-wrapper:8084/accounts/' + req.currentUser.gsuite_id, { suspended: false }); + } return res.json({ success: true, @@ -306,7 +320,9 @@ exports.confirmEmailChange = async (req, res) => { await sequelize.transaction(async (t) => { await mailChange.user.update({ email: mailChange.new_email }, { transaction: t }); await mailChange.destroy({ transaction: t }); - await superagent.put('gsuite-wrapper:8084/accounts/' + req.currentUser.gsuite_id, { secondaryEmail: mailChange.new_email }); + if (req.currentUser.gsuite_id) { + await superagent.put('gsuite-wrapper:8084/accounts/' + req.currentUser.gsuite_id, { secondaryEmail: mailChange.new_email }); + } }); return res.json({