Skip to content

Commit

Permalink
fix(gsuite): implemented few TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
WikiRik committed Oct 14, 2021
1 parent 7409058 commit c4f0fab
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/cron.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const cron = require('node-cron');
const superagent = require('superagent');
const _ = require('lodash');

const logger = require('./logger');
Expand Down Expand Up @@ -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.');
Expand Down
1 change: 1 addition & 0 deletions middlewares/campaigns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ', ''),
Expand Down
26 changes: 21 additions & 5 deletions middlewares/members.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
Expand All @@ -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.'
Expand All @@ -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.

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit c4f0fab

Please sign in to comment.