Skip to content

Commit

Permalink
BA-1069 - Fixing final code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
sutherlanda committed Mar 27, 2019
1 parent d812872 commit d193501
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

import { NextFunction, Request, Response } from 'express';
import mongoose from 'mongoose';
import passport from 'passport';
import CoreServerErrors from '../../../../core/server/controllers/CoreServerErrors';
import MessagesServerController from '../../../../messages/server/controllers/MessagesServerController';
import { UserModel } from '../../models/UserModel';
import { IUserModel, UserModel } from '../../models/UserModel';

class UserAuthenticationController {
public static getInstance() {
Expand All @@ -18,9 +19,12 @@ class UserAuthenticationController {
// URLs for which user can't be redirected on signin
private noReturnUrls = ['/authentication/signin', '/authentication/signup'];

private constructor() {}
private constructor() {
this.signin = this.signin.bind(this);
this.signup = this.signup.bind(this);
}

public signup = (req, res) => {
public signup(req: Request, res: Response): void {
// For security measurement we remove the roles from the req.body object
delete req.body.roles;

Expand All @@ -42,35 +46,23 @@ class UserAuthenticationController {
// Remove sensitive data before login
user.password = undefined;
user.salt = undefined;
req.login(user, loginErr => {
if (loginErr) {
res.status(400).send(loginErr);
} else {
res.json(user);
}
});
req.login(user, this.handleLoginResponse(res, user));
}
});
};

/**
* Signin after passport authentication
*/
public signin = (req, res, next) => {
public signin(req: Request, res: Response, next: NextFunction) {
passport.authenticate('local', {}, (err, user, info) => {
if (err || !user) {
res.status(422).send(info);
} else {
// Remove sensitive data before login
user.password = undefined;
user.salt = undefined;
req.login(user, loginErr => {
if (loginErr) {
res.status(400).send(loginErr);
} else {
res.json(user);
}
});
req.login(user, this.handleLoginResponse(res, user));
}
})(req, res, next);
};
Expand Down Expand Up @@ -270,27 +262,15 @@ class UserAuthenticationController {
}
};

private ensureOrgs = (user, orglist) => {
const Org = mongoose.model('Org');
const plist = orglist.map(orgid => {
return new Promise((resolve, reject) => {
Org.findById(orgid).exec((err, org) => {
if (err || !org) {
user.orgsAdmin.pull(orgid);
user.orgsMember.pull(orgid);
user.orgsPending.pull(orgid);
}
resolve();
});
});
});
Promise.all(plist).then(() => {
user.markModified('orgsAdmin');
user.markModified('orgsMember');
user.markModified('orgsPending');
user.save();
});
};
private handleLoginResponse(res: Response, user: IUserModel) {
return (loginErr: any) => {
if (loginErr) {
res.status(400).send(loginErr);
} else {
res.json(user);
}
}
}
}

export default UserAuthenticationController.getInstance();
23 changes: 12 additions & 11 deletions modules/users/server/controllers/users/UserPasswordController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import async from 'async';
import crypto from 'crypto';
import { NextFunction, Request, Response } from 'express';
import nodemailer from 'nodemailer';
import config from '../../../../../config/ApplicationConfig';
import CoreServerErrors from '../../../../core/server/controllers/CoreServerErrors';
Expand Down Expand Up @@ -105,11 +106,7 @@ class UserPasswordController {
});
}
],
err => {
if (err) {
return next(err);
}
}
this.handleErr(next)
);
};

Expand Down Expand Up @@ -137,7 +134,7 @@ class UserPasswordController {
/**
* Reset password POST from email token
*/
public reset = (req, res, next) => {
public async reset(req: Request, res: Response, next: NextFunction): Promise<void> {
// Init Variables
const passwordDetails = req.body;

Expand Down Expand Up @@ -218,11 +215,7 @@ class UserPasswordController {
});
}
],
err => {
if (err) {
return next(err);
}
}
this.handleErr(next)
);
};

Expand Down Expand Up @@ -285,6 +278,14 @@ class UserPasswordController {
});
}
};

private handleErr(next: NextFunction) {
return (err: any) => {
if (err) {
return next(err);
}
}
}
}

export default UserPasswordController.getInstance();
141 changes: 69 additions & 72 deletions scripts/reset-password.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,83 @@
'use strict';

var nodemailer = require('nodemailer'),
mongoose = require('mongoose'),
chalk = require('chalk'),
config = require('../config/config'),
mg = require('../config/lib/mongoose');
mongoose = require('mongoose'),
chalk = require('chalk'),
config = require('../config/config'),
mg = require('../config/lib/mongoose');

var transporter = nodemailer.createTransport(config.mailer.options);
var link = 'reset link here'; // PUT reset link here
var email = {
from: config.mailer.from,
subject: 'Security update'
from: config.mailer.from,
subject: 'Security update'
};
var text = [
'Dear {{name}},',
'\n',
'We have updated our password storage systems to be more secure and more efficient, please click the link below to reset your password so you can login in the future.',
link,
'\n',
'Thanks,',
'The Team'
'Dear {{name}},',
'\n',
'We have updated our password storage systems to be more secure and more efficient, please click the link below to reset your password so you can login in the future.',
link,
'\n',
'Thanks,',
'The Team'
].join('\n');

mg.loadModels();

mg.connect(function (db) {
var User = mongoose.model('User');

User.find().exec(function (err, users) {
if (err) {
throw err;
}

var processedCount = 0,
errorCount = 0;

// report and exit if no users were found
if (users.length === 0) {
return reportAndExit(processedCount, errorCount);
}

for (var i = 0; i < users.length; i++) {
sendEmail(users[i]);
}

function sendEmail(user) {
email.to = user.email;
email.text = email.html = text.replace('{{name}}', user.displayName);

transporter.sendMail(email, emailCallback(user));
}

function emailCallback(user) {
return function (err, info) {
processedCount++;

if (err) {
errorCount++;

if (config.mailer.options.debug) {
console.log('Error: ', err);
}
console.error('[' + processedCount + '/' + users.length + '] ' + chalk.red('Could not send email for ' + user.displayName));
} else {
console.log('[' + processedCount + '/' + users.length + '] Sent reset password email for ' + user.displayName);
}

if (processedCount === users.length) {
return reportAndExit(processedCount, errorCount);
}
};
}

// report the processing results and exit
function reportAndExit(processedCount, errorCount) {
var successCount = processedCount - errorCount;

if (processedCount === 0) {
console.log(chalk.yellow('No users were found.'));
}

process.exit(0);
}
});
mg.connect(function(db) {
var User = mongoose.model('User');

User.find().exec(function(err, users) {
if (err) {
throw err;
}

var processedCount = 0,
errorCount = 0;

// report and exit if no users were found
if (users.length === 0) {
return reportAndExit(processedCount, errorCount);
}

for (var i = 0; i < users.length; i++) {
sendEmail(users[i]);
}

function sendEmail(user) {
email.to = user.email;
email.text = email.html = text.replace('{{name}}', user.displayName);

transporter.sendMail(email, emailCallback(user));
}

function emailCallback(user) {
return function(err, info) {
processedCount++;

if (err) {
errorCount++;

if (config.mailer.options.debug) {
console.log('Error: ', err);
}
console.error('[' + processedCount + '/' + users.length + '] ' + chalk.red('Could not send email for ' + user.displayName));
} else {
console.log('[' + processedCount + '/' + users.length + '] Sent reset password email for ' + user.displayName);
}

if (processedCount === users.length) {
return reportAndExit(processedCount, errorCount);
}
};
}

// report the processing results and exit
function reportAndExit(processedCount, errorCount) {
if (processedCount === 0) {
console.log(chalk.yellow('No users were found.'));
}
process.exit(0);
}
});
});

0 comments on commit d193501

Please sign in to comment.