2013-09-24 14:46:30 +04:00
|
|
|
var Ghost = require('../../ghost'),
|
|
|
|
_ = require('underscore'),
|
|
|
|
fs = require('fs-extra'),
|
|
|
|
path = require('path'),
|
|
|
|
api = require('../api'),
|
|
|
|
moment = require('moment'),
|
|
|
|
errors = require('../errorHandling'),
|
|
|
|
|
|
|
|
ghost = new Ghost(),
|
|
|
|
dataProvider = ghost.dataProvider,
|
2013-06-25 15:43:15 +04:00
|
|
|
adminNavbar,
|
2013-08-22 23:48:36 +04:00
|
|
|
adminControllers,
|
|
|
|
loginSecurity = [];
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// TODO: combine path/navClass to single "slug(?)" variable with no prefix
|
|
|
|
adminNavbar = {
|
|
|
|
content: {
|
|
|
|
name: 'Content',
|
|
|
|
navClass: 'content',
|
|
|
|
key: 'admin.navbar.content',
|
2013-09-11 18:38:09 +04:00
|
|
|
path: '/'
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
|
|
|
add: {
|
|
|
|
name: 'New Post',
|
|
|
|
navClass: 'editor',
|
|
|
|
key: 'admin.navbar.editor',
|
|
|
|
path: '/editor/'
|
|
|
|
},
|
|
|
|
settings: {
|
|
|
|
name: 'Settings',
|
|
|
|
navClass: 'settings',
|
|
|
|
key: 'admin.navbar.settings',
|
|
|
|
path: '/settings/'
|
|
|
|
}
|
|
|
|
};
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-08-22 23:48:36 +04:00
|
|
|
|
2013-06-25 20:58:26 +04:00
|
|
|
// TODO: make this a util or helper
|
2013-06-25 15:43:15 +04:00
|
|
|
function setSelected(list, name) {
|
|
|
|
_.each(list, function (item, key) {
|
|
|
|
item.selected = key === name;
|
2013-05-29 04:10:39 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
return list;
|
|
|
|
}
|
2013-05-29 04:10:39 +04:00
|
|
|
|
2013-09-08 00:55:01 +04:00
|
|
|
// TODO: this could be a separate module
|
|
|
|
function getUniqueFileName(dir, name, ext, i, done) {
|
|
|
|
var filename,
|
|
|
|
append = '';
|
|
|
|
|
|
|
|
if (i) {
|
|
|
|
append = '-' + i;
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = path.join(dir, name + append + ext);
|
|
|
|
fs.exists(filename, function (exists) {
|
|
|
|
if (exists) {
|
|
|
|
setImmediate(function () {
|
|
|
|
i = i + 1;
|
|
|
|
return getUniqueFileName(dir, name, ext, i, done);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return done(filename);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
adminControllers = {
|
2013-08-05 21:31:29 +04:00
|
|
|
'uploader': function (req, res) {
|
2013-09-08 00:55:01 +04:00
|
|
|
|
2013-08-05 21:31:29 +04:00
|
|
|
var currentDate = moment(),
|
2013-09-19 10:26:26 +04:00
|
|
|
month = currentDate.format('MMM'),
|
|
|
|
year = currentDate.format('YYYY'),
|
2013-08-05 21:31:29 +04:00
|
|
|
tmp_path = req.files.uploadimage.path,
|
2013-10-10 14:44:31 +04:00
|
|
|
imagespath = path.join(ghost.paths().appRoot, 'content/images'),
|
|
|
|
dir = path.join(imagespath, year, month),
|
2013-08-14 00:04:07 +04:00
|
|
|
ext = path.extname(req.files.uploadimage.name).toLowerCase(),
|
2013-10-11 23:26:09 +04:00
|
|
|
type = req.files.uploadimage.type || req.files.uploadimage.headers['content-type'],
|
2013-09-18 00:11:22 +04:00
|
|
|
basename = path.basename(req.files.uploadimage.name, ext).replace(/[\W]/gi, '_');
|
2013-08-05 21:31:29 +04:00
|
|
|
|
2013-09-08 00:55:01 +04:00
|
|
|
function renameFile(target_path) {
|
2013-08-05 21:31:29 +04:00
|
|
|
// adds directories recursively
|
|
|
|
fs.mkdirs(dir, function (err) {
|
|
|
|
if (err) {
|
2013-09-18 12:59:42 +04:00
|
|
|
return errors.logError(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.copy(tmp_path, target_path, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return errors.logError(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.unlink(tmp_path, function (e) {
|
2013-08-05 21:31:29 +04:00
|
|
|
if (err) {
|
2013-09-18 12:59:42 +04:00
|
|
|
return errors.logError(err);
|
2013-08-05 21:31:29 +04:00
|
|
|
}
|
2013-09-18 12:59:42 +04:00
|
|
|
|
|
|
|
// the src for the image must be in URI format, not a file system path, which in Windows uses \
|
2013-10-10 14:44:31 +04:00
|
|
|
var src = path.join('/', target_path.replace(ghost.paths().appRoot, "")).replace(new RegExp('\\' + path.sep, 'g'), '/');
|
2013-09-18 12:59:42 +04:00
|
|
|
return res.send(src);
|
2013-08-05 21:31:29 +04:00
|
|
|
});
|
2013-09-18 12:59:42 +04:00
|
|
|
});
|
2013-08-05 21:31:29 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-10-17 17:28:28 +04:00
|
|
|
//limit uploads to type && extension
|
|
|
|
if ((type === 'image/jpeg' || type === 'image/png' || type === 'image/gif')
|
|
|
|
&& (ext === '.jpg' || ext === '.jpeg' || ext === '.png' || ext === '.gif')) {
|
2013-09-08 00:55:01 +04:00
|
|
|
getUniqueFileName(dir, basename, ext, null, function (filename) {
|
|
|
|
renameFile(filename);
|
|
|
|
});
|
2013-08-05 21:31:29 +04:00
|
|
|
} else {
|
2013-10-21 00:25:00 +04:00
|
|
|
res.send(415, 'Unsupported Media Type');
|
2013-08-05 21:31:29 +04:00
|
|
|
}
|
|
|
|
},
|
2013-06-25 15:43:15 +04:00
|
|
|
'login': function (req, res) {
|
2013-09-12 12:59:58 +04:00
|
|
|
res.render('login', {
|
2013-06-25 15:43:15 +04:00
|
|
|
bodyClass: 'ghost-login',
|
|
|
|
hideNavbar: true,
|
|
|
|
adminNav: setSelected(adminNavbar, 'login')
|
2013-05-11 20:44:25 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
|
|
|
'auth': function (req, res) {
|
2013-08-22 23:48:36 +04:00
|
|
|
var currentTime = process.hrtime()[0],
|
|
|
|
denied = '';
|
|
|
|
loginSecurity = _.filter(loginSecurity, function (ipTime) {
|
|
|
|
return (ipTime.time + 2 > currentTime);
|
|
|
|
});
|
|
|
|
denied = _.find(loginSecurity, function (ipTime) {
|
|
|
|
return (ipTime.ip === req.connection.remoteAddress);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-08-22 23:48:36 +04:00
|
|
|
|
|
|
|
if (!denied) {
|
|
|
|
loginSecurity.push({ip: req.connection.remoteAddress, time: process.hrtime()[0]});
|
|
|
|
api.users.check({email: req.body.email, pw: req.body.password}).then(function (user) {
|
|
|
|
req.session.user = user.id;
|
|
|
|
res.json(200, {redirect: req.body.redirect ? '/ghost/'
|
|
|
|
+ decodeURIComponent(req.body.redirect) : '/ghost/'});
|
|
|
|
}, function (error) {
|
|
|
|
res.json(401, {error: error.message});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.json(401, {error: 'Slow down, there are way too many login attempts!'});
|
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-08-06 03:49:06 +04:00
|
|
|
changepw: function (req, res) {
|
|
|
|
api.users.changePassword({
|
2013-08-09 05:22:49 +04:00
|
|
|
currentUser: req.session.user,
|
2013-08-06 03:49:06 +04:00
|
|
|
oldpw: req.body.password,
|
|
|
|
newpw: req.body.newpassword,
|
|
|
|
ne2pw: req.body.ne2password
|
2013-08-20 22:52:44 +04:00
|
|
|
}).then(function () {
|
2013-08-06 03:49:06 +04:00
|
|
|
res.json(200, {msg: 'Password changed successfully'});
|
|
|
|
}, function (error) {
|
2013-08-20 22:52:44 +04:00
|
|
|
res.send(401, {error: error.message});
|
2013-08-06 03:49:06 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
},
|
2013-06-25 15:43:15 +04:00
|
|
|
'signup': function (req, res) {
|
|
|
|
res.render('signup', {
|
2013-09-12 12:59:58 +04:00
|
|
|
bodyClass: 'ghost-signup',
|
2013-06-25 15:43:15 +04:00
|
|
|
hideNavbar: true,
|
|
|
|
adminNav: setSelected(adminNavbar, 'login')
|
|
|
|
});
|
|
|
|
},
|
2013-09-01 02:20:12 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
'doRegister': function (req, res) {
|
2013-09-08 23:16:40 +04:00
|
|
|
var name = req.body.name,
|
|
|
|
email = req.body.email,
|
2013-06-25 15:43:15 +04:00
|
|
|
password = req.body.password;
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-08-19 01:50:42 +04:00
|
|
|
api.users.add({
|
2013-09-12 02:04:49 +04:00
|
|
|
name: name,
|
|
|
|
email: email,
|
2013-08-19 01:50:42 +04:00
|
|
|
password: password
|
|
|
|
}).then(function (user) {
|
2013-09-17 06:08:36 +04:00
|
|
|
api.settings.edit('email', email).then(function () {
|
|
|
|
if (req.session.user === undefined) {
|
|
|
|
req.session.user = user.id;
|
|
|
|
}
|
|
|
|
res.json(200, {redirect: '/ghost/'});
|
|
|
|
});
|
|
|
|
}).otherwise(function (error) {
|
2013-08-19 01:50:42 +04:00
|
|
|
res.json(401, {error: error.message});
|
|
|
|
});
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-09-01 02:20:12 +04:00
|
|
|
|
|
|
|
'forgotten': function (req, res) {
|
2013-09-12 12:59:58 +04:00
|
|
|
res.render('forgotten', {
|
2013-09-01 02:20:12 +04:00
|
|
|
bodyClass: 'ghost-forgotten',
|
|
|
|
hideNavbar: true,
|
|
|
|
adminNav: setSelected(adminNavbar, 'login')
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
'resetPassword': function (req, res) {
|
|
|
|
var email = req.body.email;
|
|
|
|
|
|
|
|
api.users.forgottenPassword(email).then(function (user) {
|
|
|
|
var message = {
|
|
|
|
to: email,
|
|
|
|
subject: 'Your new password',
|
|
|
|
html: "<p><strong>Hello!</strong></p>" +
|
2013-09-09 14:51:12 +04:00
|
|
|
"<p>You've reset your password. Here's the new one: " + user.newPassword + "</p>" +
|
|
|
|
"<p>Ghost <br/>" +
|
2013-09-11 19:23:07 +04:00
|
|
|
'<a href="' + ghost.config().url + '">' +
|
|
|
|
ghost.config().url + '</a></p>'
|
2013-09-01 02:20:12 +04:00
|
|
|
};
|
|
|
|
|
2013-09-04 17:57:41 +04:00
|
|
|
return ghost.mail.send(message);
|
|
|
|
}).then(function success() {
|
|
|
|
var notification = {
|
|
|
|
type: 'success',
|
|
|
|
message: 'Your password was changed successfully. Check your email for details.',
|
|
|
|
status: 'passive',
|
|
|
|
id: 'successresetpw'
|
|
|
|
};
|
|
|
|
|
|
|
|
return api.notifications.add(notification).then(function () {
|
|
|
|
res.json(200, {redirect: '/ghost/signin/'});
|
|
|
|
});
|
2013-09-01 02:20:12 +04:00
|
|
|
|
2013-09-04 17:57:41 +04:00
|
|
|
}, function failure(error) {
|
2013-09-01 02:20:12 +04:00
|
|
|
res.json(401, {error: error.message});
|
2013-09-04 17:57:41 +04:00
|
|
|
}).otherwise(errors.logAndThrowError);
|
2013-09-01 02:20:12 +04:00
|
|
|
},
|
2013-06-25 15:43:15 +04:00
|
|
|
'logout': function (req, res) {
|
2013-10-18 15:24:01 +04:00
|
|
|
req.session = null;
|
2013-09-04 17:57:41 +04:00
|
|
|
var notification = {
|
2013-08-20 03:40:09 +04:00
|
|
|
type: 'success',
|
2013-08-24 21:35:31 +04:00
|
|
|
message: 'You were successfully signed out',
|
2013-08-20 03:40:09 +04:00
|
|
|
status: 'passive',
|
|
|
|
id: 'successlogout'
|
|
|
|
};
|
|
|
|
|
2013-09-04 17:57:41 +04:00
|
|
|
return api.notifications.add(notification).then(function () {
|
|
|
|
res.redirect('/ghost/signin/');
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
|
|
|
'index': function (req, res) {
|
2013-09-11 18:38:09 +04:00
|
|
|
res.render('content', {
|
|
|
|
bodyClass: 'manage',
|
|
|
|
adminNav: setSelected(adminNavbar, 'content')
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
'editor': function (req, res) {
|
|
|
|
if (req.params.id !== undefined) {
|
2013-09-05 00:05:36 +04:00
|
|
|
res.render('editor', {
|
|
|
|
bodyClass: 'editor',
|
|
|
|
adminNav: setSelected(adminNavbar, 'content')
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
} else {
|
|
|
|
res.render('editor', {
|
|
|
|
bodyClass: 'editor',
|
|
|
|
adminNav: setSelected(adminNavbar, 'add')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'content': function (req, res) {
|
2013-09-05 00:05:36 +04:00
|
|
|
res.render('content', {
|
|
|
|
bodyClass: 'manage',
|
|
|
|
adminNav: setSelected(adminNavbar, 'content')
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-09-18 06:31:43 +04:00
|
|
|
'settings': function (req, res, next) {
|
|
|
|
|
|
|
|
// TODO: Centralise list/enumeration of settings panes, so we don't
|
|
|
|
// run into trouble in future.
|
2013-09-24 14:46:30 +04:00
|
|
|
var allowedSections = ['', 'general', 'user'],
|
|
|
|
section = req.url.replace(/(^\/ghost\/settings[\/]*|\/$)/ig, '');
|
2013-09-18 06:31:43 +04:00
|
|
|
|
|
|
|
if (allowedSections.indexOf(section) < 0) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2013-09-05 00:05:36 +04:00
|
|
|
res.render('settings', {
|
|
|
|
bodyClass: 'settings',
|
|
|
|
adminNav: setSelected(adminNavbar, 'settings')
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
|
|
|
'debug': { /* ugly temporary stuff for managing the app before it's properly finished */
|
|
|
|
index: function (req, res) {
|
|
|
|
res.render('debug', {
|
|
|
|
bodyClass: 'settings',
|
|
|
|
adminNav: setSelected(adminNavbar, 'settings')
|
|
|
|
});
|
2013-05-11 20:44:25 +04:00
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
|
|
|
};
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-08-09 05:22:49 +04:00
|
|
|
module.exports = adminControllers;
|