mirror of
https://github.com/taobataoma/meanTorrent.git
synced 2026-06-25 00:31:05 +02:00
feat(users): admin/oper can send official invitations now
This commit is contained in:
@@ -7,6 +7,7 @@ var path = require('path'),
|
||||
config = require(path.resolve('./config/config')),
|
||||
mongoose = require('mongoose'),
|
||||
errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller')),
|
||||
validator = require('validator'),
|
||||
nodemailer = require('nodemailer'),
|
||||
User = mongoose.model('User'),
|
||||
Invitation = mongoose.model('Invitation'),
|
||||
@@ -15,6 +16,15 @@ var path = require('path'),
|
||||
|
||||
var smtpTransport = nodemailer.createTransport(config.mailer.options);
|
||||
var traceConfig = config.meanTorrentConfig.trace;
|
||||
var mtDebug = require(path.resolve('./config/lib/debug'));
|
||||
|
||||
|
||||
/**
|
||||
* A Validation function for local strategy email
|
||||
*/
|
||||
var validateEmail = function (email) {
|
||||
return validator.isEmail(email, {require_tld: false});
|
||||
};
|
||||
|
||||
/**
|
||||
* create a Invitation
|
||||
@@ -144,6 +154,7 @@ exports.update = function (req, res) {
|
||||
} else if (results[1] > 0) {
|
||||
return res.status(422).send({message: 'EMAIL_ALREADY_EXIST'});
|
||||
} else {
|
||||
//send invitation mail
|
||||
var httpTransport = 'http://';
|
||||
if (config.secure && config.secure.ssl === true) {
|
||||
httpTransport = 'https://';
|
||||
@@ -192,6 +203,108 @@ exports.update = function (req, res) {
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* official
|
||||
* send official invitation
|
||||
* @param req
|
||||
* @param res
|
||||
*/
|
||||
exports.official = function (req, res) {
|
||||
if (!validateEmail(req.body.email)) {
|
||||
return res.status(422).send({
|
||||
message: 'ERROR: invalid email address!'
|
||||
});
|
||||
} else {
|
||||
|
||||
var countRegisteredEmail = function (callback) {
|
||||
User.count({email: req.body.email}, function (err, count) {
|
||||
if (err) {
|
||||
callback(err, null);
|
||||
} else {
|
||||
callback(null, count);
|
||||
}
|
||||
});
|
||||
};
|
||||
var countInvitedEmail = function (callback) {
|
||||
Invitation.count({to_email: req.body.email}, function (err, count) {
|
||||
if (err) {
|
||||
callback(err, null);
|
||||
} else {
|
||||
callback(null, count);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
async.parallel([countRegisteredEmail, countInvitedEmail], function (err, results) {
|
||||
if (err) {
|
||||
return res.status(422).send(err);
|
||||
} else {
|
||||
if (results[0] > 0) {
|
||||
return res.status(422).send({message: 'EMAIL_ALREADY_REGISTERED'});
|
||||
} else if (results[1] > 0) {
|
||||
return res.status(422).send({message: 'EMAIL_ALREADY_EXIST'});
|
||||
} else {
|
||||
//write invitation data
|
||||
var invitation = new Invitation();
|
||||
invitation.user = req.user;
|
||||
invitation.token = req.user.randomAsciiString(32);
|
||||
invitation.to_email = req.body.email;
|
||||
invitation.status = 1;
|
||||
invitation.invitedat = Date.now();
|
||||
invitation.expiresat = Date.now() + config.meanTorrentConfig.invite.expires;
|
||||
invitation.isOfficial = true;
|
||||
|
||||
invitation.save(function (err) {
|
||||
if (err) {
|
||||
return res.status(422).send({
|
||||
message: errorHandler.getErrorMessage(err)
|
||||
});
|
||||
} else {
|
||||
//create trace log
|
||||
traceLogCreate(req, traceConfig.action.adminSendOfficialInvitation, {
|
||||
to: req.body.email,
|
||||
token: invitation.token
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
//send invitation mail
|
||||
var httpTransport = 'http://';
|
||||
if (config.secure && config.secure.ssl === true) {
|
||||
httpTransport = 'https://';
|
||||
}
|
||||
var baseUrl = req.app.get('domain') || httpTransport + req.headers.host;
|
||||
res.render(path.resolve('modules/invitations/server/templates/invite-sign-up-email'), {
|
||||
to_email: req.body.email,
|
||||
name: req.user.displayName,
|
||||
appName: config.app.title,
|
||||
url: baseUrl + '/api/auth/invite/' + invitation.token,
|
||||
hours: config.meanTorrentConfig.invite.expires / (60 * 60 * 1000)
|
||||
}, function (err, emailHTML) {
|
||||
if (err) {
|
||||
return res.status(422).send({message: 'INVITE_MAIL_RENDER_FAILED'});
|
||||
} else {
|
||||
var mailOptions = {
|
||||
to: req.body.email,
|
||||
from: config.mailer.from,
|
||||
subject: config.app.title + ' invitation',
|
||||
html: emailHTML
|
||||
};
|
||||
smtpTransport.sendMail(mailOptions, function (err) {
|
||||
if (err) {
|
||||
return res.status(422).send({message: 'INVITE_MAIL_SEND_FAILED'});
|
||||
} else {
|
||||
res.json(invitation);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Delete an invitation
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user