ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anovi...@apache.org
Subject ignite git commit: IGNITE-843 Refactor serve to es6.
Date Mon, 15 Feb 2016 03:35:28 GMT
Repository: ignite
Updated Branches:
  refs/heads/ignite-843-rc2 b1020f460 -> 4220afa5a


IGNITE-843 Refactor serve to es6.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/4220afa5
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/4220afa5
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/4220afa5

Branch: refs/heads/ignite-843-rc2
Commit: 4220afa5a2b3d1be680d0ea0126089303b321fd5
Parents: b1020f4
Author: Andrey <anovikov@gridgain.com>
Authored: Mon Feb 15 10:35:47 2016 +0700
Committer: Andrey <anovikov@gridgain.com>
Committed: Mon Feb 15 10:35:47 2016 +0700

----------------------------------------------------------------------
 .../src/main/js/serve/mongo.js                  |   3 +
 .../src/main/js/serve/routes/notebooks.js       |  10 +-
 .../src/main/js/serve/routes/profile.js         |   2 +-
 .../src/main/js/serve/routes/public.js          | 191 ++++++++++---------
 4 files changed, 105 insertions(+), 101 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/4220afa5/modules/control-center-web/src/main/js/serve/mongo.js
----------------------------------------------------------------------
diff --git a/modules/control-center-web/src/main/js/serve/mongo.js b/modules/control-center-web/src/main/js/serve/mongo.js
index c0d5228..f6ff790 100644
--- a/modules/control-center-web/src/main/js/serve/mongo.js
+++ b/modules/control-center-web/src/main/js/serve/mongo.js
@@ -27,6 +27,9 @@ module.exports = {
 module.exports.factory = function(deepPopulatePlugin, passportMongo, settings, pluginMongo)
{
     const mongoose = require('mongoose');
 
+    // Use native promises
+    mongoose.Promise = global.Promise;
+
     const deepPopulate = deepPopulatePlugin(mongoose);
 
     // Connect to mongoDB database.

http://git-wip-us.apache.org/repos/asf/ignite/blob/4220afa5/modules/control-center-web/src/main/js/serve/routes/notebooks.js
----------------------------------------------------------------------
diff --git a/modules/control-center-web/src/main/js/serve/routes/notebooks.js b/modules/control-center-web/src/main/js/serve/routes/notebooks.js
index e700921..48e9a5c 100644
--- a/modules/control-center-web/src/main/js/serve/routes/notebooks.js
+++ b/modules/control-center-web/src/main/js/serve/routes/notebooks.js
@@ -33,19 +33,17 @@ module.exports.factory = function(express, mongo) {
          * @param res Response.
          */
         router.post('/list', function(req, res) {
-            var user_id = req.currentUserId();
+            const user_id = req.currentUserId();
 
             // Get owned space and all accessed space.
-            mongo.Space.find({$or: [{owner: user_id}, {usedBy: {$elemMatch: {account: user_id}}}]},
function(err, spaces) {
+            mongo.Space.find({$or: [{owner: user_id}, {usedBy: {$elemMatch: {account: user_id}}}]},
(err, spaces) => {
                 if (err)
                     return res.status(500).send(err.message);
 
-                var space_ids = spaces.map(function(value) {
-                    return value._id;
-                });
+                const space_ids = spaces.map((value) => value._id);
 
                 // Get all metadata for spaces.
-                mongo.Notebook.find({space: {$in: space_ids}}).select('_id name').sort('name').exec(function(err,
notebooks) {
+                mongo.Notebook.find({space: {$in: space_ids}}).select('_id name').sort('name').exec((err,
notebooks) => {
                     if (err)
                         return res.status(500).send(err.message);
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/4220afa5/modules/control-center-web/src/main/js/serve/routes/profile.js
----------------------------------------------------------------------
diff --git a/modules/control-center-web/src/main/js/serve/routes/profile.js b/modules/control-center-web/src/main/js/serve/routes/profile.js
index 5a22010..e080ce3 100644
--- a/modules/control-center-web/src/main/js/serve/routes/profile.js
+++ b/modules/control-center-web/src/main/js/serve/routes/profile.js
@@ -24,7 +24,7 @@ module.exports = {
 
 module.exports.factory = function(_, express, mongo) {
     return new Promise((resolveFactory) => {
-        const router = express.Router();
+        const router = new express.Router();
 
         /**
          * Save user profile.

http://git-wip-us.apache.org/repos/asf/ignite/blob/4220afa5/modules/control-center-web/src/main/js/serve/routes/public.js
----------------------------------------------------------------------
diff --git a/modules/control-center-web/src/main/js/serve/routes/public.js b/modules/control-center-web/src/main/js/serve/routes/public.js
index 476ded2..9c06015 100644
--- a/modules/control-center-web/src/main/js/serve/routes/public.js
+++ b/modules/control-center-web/src/main/js/serve/routes/public.js
@@ -25,8 +25,8 @@ module.exports = {
 };
 
 module.exports.factory = function(express, passport, nodemailer, settings, mongo) {
-    return new Promise(function(resolve) {
-        const router = express.Router();
+    return new Promise(function(factoryResolve) {
+        const router = new express.Router();
 
         const _randomString = () => {
             const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
@@ -42,9 +42,9 @@ module.exports.factory = function(express, passport, nodemailer, settings,
mongo
 
         // GET user.
         router.post('/user', function(req, res) {
-            var becomeUsed = req.session.viewedUser && req.user.admin;
+            const becomeUsed = req.session.viewedUser && req.user.admin;
 
-            var user = req.user;
+            let user = req.user;
 
             if (becomeUsed) {
                 user = req.session.viewedUser;
@@ -59,28 +59,28 @@ module.exports.factory = function(express, passport, nodemailer, settings,
mongo
          * Register new account.
          */
         router.post('/register', function(req, res) {
-            mongo.Account.count(function(err, cnt) {
-                if (err)
-                    return res.status(401).send(err.message);
+            mongo.Account.count(function(errCount, cnt) {
+                if (errCount)
+                    return res.status(401).send(errCount.message);
 
                 req.body.admin = cnt === 0;
 
-                var account = new mongo.Account(req.body);
+                const _account = new mongo.Account(req.body);
 
-                account.token = _randomString();
+                _account.token = _randomString();
 
-                mongo.Account.register(account, req.body.password, function(err, account)
{
-                    if (err)
-                        return res.status(401).send(err.message);
+                mongo.Account.register(_account, req.body.password, function(errRegister,
account) {
+                    if (errRegister)
+                        return res.status(401).send(errRegister.message);
 
                     if (!account)
                         return res.status(500).send('Failed to create account.');
 
                     new mongo.Space({name: 'Personal space', owner: account._id}).save();
 
-                    req.logIn(account, {}, function(err) {
-                        if (err)
-                            return res.status(401).send(err.message);
+                    req.logIn(account, {}, function(errLogIn) {
+                        if (errLogIn)
+                            return res.status(401).send(errLogIn.message);
 
                         return res.sendStatus(200);
                     });
@@ -92,16 +92,16 @@ module.exports.factory = function(express, passport, nodemailer, settings,
mongo
          * Login in exist account.
          */
         router.post('/login', function(req, res, next) {
-            passport.authenticate('local', function(err, user) {
-                if (err)
-                    return res.status(401).send(err.message);
+            passport.authenticate('local', function(errAuth, user) {
+                if (errAuth)
+                    return res.status(401).send(errAuth.message);
 
                 if (!user)
                     return res.status(401).send('Invalid email or password');
 
-                req.logIn(user, {}, function(err) {
-                    if (err)
-                        return res.status(401).send(err.message);
+                req.logIn(user, {}, function(errLogIn) {
+                    if (errLogIn)
+                        return res.status(401).send(errLogIn.message);
 
                     return res.sendStatus(200);
                 });
@@ -121,7 +121,7 @@ module.exports.factory = function(express, passport, nodemailer, settings,
mongo
          * Send e-mail to user with reset token.
          */
         router.post('/password/forgot', function(req, res) {
-            var transporter = {
+            const transporter = {
                 service: settings.smtp.service,
                 auth: {
                     user: settings.smtp.email,
@@ -132,26 +132,21 @@ module.exports.factory = function(express, passport, nodemailer, settings,
mongo
             if (transporter.service === '' || transporter.auth.user === '' || transporter.auth.pass
=== '')
                 return res.status(401).send('Can\'t send e-mail with instructions to reset
password. Please ask webmaster to setup SMTP server!');
 
-            var token = _randomString();
+            const token = _randomString();
 
-            mongo.Account.findOne({email: req.body.email}, function(err, user) {
-                if (!user)
-                    return res.status(401).send('No account with that email address exists!');
-
-                // TODO IGNITE-843 Send email to admin
-                if (err)
-                    return res.status(401).send('Failed to reset password!');
+            mongo.Account.findOne({email: req.body.email}).exec()
+                .then((user) => {
+                    if (!user)
+                        return Promise.reject('Account with that email address does not exists!');
 
-                user.resetPasswordToken = token;
-
-                user.save(function(err) {
-                    // TODO IGNITE-843 Send email to admin
-                    if (err)
-                        return res.status(401).send('Failed to reset password!');
+                    user.resetPasswordToken = token;
 
-                    var mailer = nodemailer.createTransport(transporter);
+                    return user.save();
+                })
+                .then((user) => {
+                    const mailer = nodemailer.createTransport(transporter);
 
-                    var mailOptions = {
+                    const mailOptions = {
                         from: settings.smtp.address(settings.smtp.username, settings.smtp.email),
                         to: settings.smtp.address(user.username, user.email),
                         subject: 'Password Reset',
@@ -163,88 +158,96 @@ module.exports.factory = function(express, passport, nodemailer, settings,
mongo
                         settings.smtp.username + '\n'
                     };
 
-                    mailer.sendMail(mailOptions, function(err) {
-                        if (err)
-                            return res.status(401).send('Failed to send e-mail with reset
link! ' + err);
+                    return new Promise((resolve, reject) => {
+                        mailer.sendMail(mailOptions, (err) => {
+                            if (err)
+                                return reject();
 
-                        return res.status(200).send('An e-mail has been sent with further
instructions.');
+                            resolve(res.status(200).send('An e-mail has been sent with further
instructions.'));
+                        });
                     });
+                })
+                .catch((err) => {
+                    // TODO IGNITE-843 Send email to admin
+                    return res.status(401).send('Failed to send e-mail with reset link!'
+ (err ? '<br>' + err : ''));
                 });
-            });
         });
 
         /**
          * Change password with given token.
          */
         router.post('/password/reset', function(req, res) {
-            mongo.Account.findOne({resetPasswordToken: req.body.token}, function(err, user)
{
-                if (!user)
-                    return res.status(500).send('Invalid token for password reset!');
+            mongo.Account.findOne({resetPasswordToken: req.body.token}).exec()
+                .then((user) => {
+                    return new Promise((resolve, reject) => {
+                        if (!user)
+                            return reject();
 
-                // TODO IGNITE-843 Send email to admin
-                if (err)
-                    return res.status(500).send('Failed to reset password!');
+                        user.setPassword(req.body.password, (err, _user) => {
+                            if (err) {
+                                console.log('Failed to reset password: ' + err.message);
 
-                user.setPassword(req.body.password, function(err, updatedUser) {
-                    if (err)
-                        return res.status(500).send(err.message);
+                                return reject();
+                            }
 
-                    updatedUser.resetPasswordToken = undefined;
+                            _user.resetPasswordToken = undefined;
 
-                    updatedUser.save(function(err) {
-                        if (err)
-                            return res.status(500).send(err.message);
+                            resolve(_user.save());
+                        });
+                    });
+                })
+                .then((user) => {
+                    const transporter = {
+                        service: settings.smtp.service,
+                        auth: {
+                            user: settings.smtp.email,
+                            pass: settings.smtp.password
+                        }
+                    };
 
-                        var transporter = {
-                            service: settings.smtp.service,
-                            auth: {
-                                user: settings.smtp.email,
-                                pass: settings.smtp.password
-                            }
-                        };
-
-                        var mailer = nodemailer.createTransport(transporter);
-
-                        var mailOptions = {
-                            from: settings.smtp.address(settings.smtp.username, settings.smtp.email),
-                            to: settings.smtp.address(user.username, user.email),
-                            subject: 'Your password has been changed',
-                            text: 'Hello,\n\n' +
-                            'This is a confirmation that the password for your account '
+ user.email + ' has just been changed.\n\n' +
-                            'Now you can login: http://' + req.headers.host + '\n\n' +
-                            '--------------\n' +
-                            'Apache Ignite Web Console\n'
-                        };
-
-                        mailer.sendMail(mailOptions, function(err) {
+                    const mailer = nodemailer.createTransport(transporter);
+
+                    const mailOptions = {
+                        from: settings.smtp.address(settings.smtp.username, settings.smtp.email),
+                        to: settings.smtp.address(user.username, user.email),
+                        subject: 'Your password has been changed',
+                        text: 'Hello,\n\n' +
+                        'This is a confirmation that the password for your account ' + user.email
+ ' has just been changed.\n\n' +
+                        'Now you can login: http://' + req.headers.host + '\n\n' +
+                        '--------------\n' +
+                        'Apache Ignite Web Console\n'
+                    };
+
+                    return new Promise((resolve) => {
+                        mailer.sendMail(mailOptions, (err) => {
                             if (err)
-                                return res.status(503).send('Password was changed, but failed
to send confirmation e-mail!<br />' + err);
+                                return resolve(res.status(500).send('Password was changed,
but failed to send confirmation e-mail!'));
 
-                            return res.status(200).send(user.email);
+                            resolve(res.status(200).send(user.email));
                         });
                     });
+                })
+                .catch(() => {
+                    res.status(500).send('Failed to reset password! Please check link from
e-mail.');
                 });
-            });
         });
 
         /* GET reset password page. */
         router.post('/validate/token', function(req, res) {
-            var token = req.body.token;
-
-            var data = {token};
+            const token = req.body.token;
 
-            mongo.Account.findOne({resetPasswordToken: token}, function(err, user) {
-                if (!user)
-                    data.error = 'Invalid token for password reset!';
-                else if (err)
-                    data.error = err;
-                else
-                    data.email = user.email;
+            mongo.Account.findOne({resetPasswordToken: token}).exec()
+                .then((user) => {
+                    return new Promise((resolve, reject) => {
+                        if (!user)
+                            return reject('Invalid token for password reset!');
 
-                res.json(data);
-            });
+                        resolve(res.json({token, email: user.email}));
+                    });
+                })
+                .catch((error) => res.json({error}));
         });
 
-        resolve(router);
+        factoryResolve(router);
     });
 };


Mime
View raw message