2013-05-16 15:21:13 +04:00
|
|
|
/*global require, module */
|
2013-05-11 20:44:25 +04:00
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Ghost = require('../../ghost'),
|
2013-06-24 00:55:03 +04:00
|
|
|
dataExport = require('../../shared/data/export'),
|
|
|
|
dataImport = require('../../shared/data/import'),
|
2013-05-11 20:44:25 +04:00
|
|
|
_ = require('underscore'),
|
|
|
|
fs = require('fs'),
|
2013-06-24 00:55:03 +04:00
|
|
|
path = require('path'),
|
|
|
|
when = require('when'),
|
|
|
|
nodefn = require('when/node/function'),
|
2013-05-16 15:21:13 +04:00
|
|
|
api = require('../../shared/api'),
|
2013-05-11 20:44:25 +04:00
|
|
|
|
|
|
|
ghost = new Ghost(),
|
2013-06-01 18:47:41 +04:00
|
|
|
dataProvider = ghost.dataProvider,
|
2013-05-11 20:44:25 +04:00
|
|
|
adminNavbar,
|
|
|
|
adminControllers;
|
|
|
|
|
|
|
|
// TODO: combine path/navClass to single "slug(?)" variable with no prefix
|
|
|
|
adminNavbar = {
|
|
|
|
dashboard: {
|
|
|
|
name: 'Dashboard',
|
|
|
|
navClass: 'dashboard',
|
|
|
|
key: 'admin.navbar.dashboard',
|
2013-05-29 04:10:39 +04:00
|
|
|
// defaultString: 'dashboard',
|
|
|
|
path: '/'
|
2013-05-11 20:44:25 +04:00
|
|
|
},
|
2013-05-29 04:10:39 +04:00
|
|
|
content: {
|
2013-05-11 20:44:25 +04:00
|
|
|
name: 'Content',
|
|
|
|
navClass: 'content',
|
2013-05-29 04:10:39 +04:00
|
|
|
key: 'admin.navbar.content',
|
|
|
|
// defaultString: 'content',
|
|
|
|
path: '/content/'
|
2013-05-11 20:44:25 +04:00
|
|
|
},
|
|
|
|
add: {
|
|
|
|
name: 'New Post',
|
|
|
|
navClass: 'editor',
|
|
|
|
key: 'admin.navbar.editor',
|
2013-05-29 04:10:39 +04:00
|
|
|
// defaultString: 'editor',
|
|
|
|
path: '/editor/'
|
2013-05-11 20:44:25 +04:00
|
|
|
},
|
|
|
|
settings: {
|
|
|
|
name: 'Settings',
|
|
|
|
navClass: 'settings',
|
|
|
|
key: 'admin.navbar.settings',
|
2013-05-29 04:10:39 +04:00
|
|
|
// defaultString: 'settings',
|
|
|
|
path: '/settings/'
|
2013-05-11 20:44:25 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-31 09:58:20 +04:00
|
|
|
ghost.doFilter('messWithAdmin', adminNavbar, function () {
|
2013-05-29 04:10:39 +04:00
|
|
|
console.log('the dofilter hook called in /core/admin/controllers/index.js');
|
|
|
|
});
|
|
|
|
|
2013-05-11 20:44:25 +04:00
|
|
|
// TODO - make this a util or helper
|
|
|
|
function setSelected(list, name) {
|
|
|
|
_.each(list, function (item, key) {
|
|
|
|
item.selected = key === name;
|
|
|
|
});
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
adminControllers = {
|
2013-05-19 15:19:39 +04:00
|
|
|
'login': function (req, res) {
|
|
|
|
res.render('login', {
|
|
|
|
bodyClass: 'ghost-login',
|
|
|
|
hideNavbar: true,
|
|
|
|
adminNav: setSelected(adminNavbar, 'login')
|
|
|
|
});
|
|
|
|
},
|
|
|
|
'auth': function (req, res) {
|
2013-05-26 21:44:01 +04:00
|
|
|
api.users.check({email: req.body.email, pw: req.body.password}).then(function (user) {
|
|
|
|
console.log('user found: ', user);
|
|
|
|
req.session.user = "ghostadmin";
|
|
|
|
res.redirect(req.query.redirect || '/ghost/');
|
2013-05-28 01:03:13 +04:00
|
|
|
}, function (error) {
|
2013-05-26 21:44:01 +04:00
|
|
|
// Do something here to signal the reason for an error
|
2013-05-28 01:03:13 +04:00
|
|
|
req.flash('error', error.message);
|
2013-05-26 21:44:01 +04:00
|
|
|
res.redirect('/ghost/login/');
|
2013-05-21 05:03:35 +04:00
|
|
|
});
|
|
|
|
},
|
2013-06-15 22:11:17 +04:00
|
|
|
'signup': function (req, res) {
|
|
|
|
res.render('signup', {
|
2013-05-21 05:03:35 +04:00
|
|
|
bodyClass: 'ghost-login',
|
|
|
|
hideNavbar: true,
|
|
|
|
adminNav: setSelected(adminNavbar, 'login')
|
|
|
|
});
|
|
|
|
},
|
|
|
|
'doRegister': function (req, res) {
|
2013-05-28 01:03:13 +04:00
|
|
|
var email = req.body.email_address,
|
|
|
|
password = req.body.password;
|
|
|
|
|
|
|
|
if (email !== '' && password.length > 5) {
|
2013-05-26 21:44:01 +04:00
|
|
|
api.users.add({
|
2013-05-28 01:03:13 +04:00
|
|
|
email_address: email,
|
|
|
|
password: password
|
2013-05-26 21:44:01 +04:00
|
|
|
}).then(function (user) {
|
2013-05-21 05:03:35 +04:00
|
|
|
console.log('user added', user);
|
|
|
|
res.redirect('/ghost/login/');
|
2013-05-26 21:17:46 +04:00
|
|
|
}, function (error) {
|
2013-05-28 01:03:13 +04:00
|
|
|
req.flash('error', error.message);
|
2013-06-15 22:11:17 +04:00
|
|
|
res.redirect('/ghost/signup/');
|
2013-05-21 05:03:35 +04:00
|
|
|
});
|
2013-05-24 10:19:19 +04:00
|
|
|
} else {
|
|
|
|
req.flash('error', "The password is too short. Have at least 6 characters in there");
|
2013-05-26 16:38:14 +04:00
|
|
|
res.redirect('back');
|
2013-05-19 15:19:39 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'logout': function (req, res) {
|
|
|
|
delete req.session.user;
|
|
|
|
req.flash('success', "You were successfully logged out");
|
|
|
|
res.redirect('/ghost/login/');
|
|
|
|
},
|
2013-05-11 20:44:25 +04:00
|
|
|
'index': function (req, res) {
|
|
|
|
res.render('dashboard', {
|
|
|
|
bodyClass: 'dashboard',
|
|
|
|
adminNav: setSelected(adminNavbar, 'dashboard')
|
|
|
|
});
|
|
|
|
},
|
|
|
|
'editor': function (req, res) {
|
|
|
|
if (req.params.id !== undefined) {
|
2013-05-17 00:56:26 +04:00
|
|
|
api.posts.read({id: parseInt(req.params.id, 10)})
|
2013-05-16 15:21:13 +04:00
|
|
|
.then(function (post) {
|
|
|
|
res.render('editor', {
|
|
|
|
bodyClass: 'editor',
|
2013-05-29 04:10:39 +04:00
|
|
|
adminNav: setSelected(adminNavbar, 'content'),
|
2013-05-17 00:56:26 +04:00
|
|
|
title: post.get('title'),
|
|
|
|
content: post.get('content')
|
2013-05-16 15:21:13 +04:00
|
|
|
});
|
2013-05-11 20:44:25 +04:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.render('editor', {
|
|
|
|
bodyClass: 'editor',
|
|
|
|
adminNav: setSelected(adminNavbar, 'add')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2013-05-29 04:10:39 +04:00
|
|
|
'content': function (req, res) {
|
2013-06-01 23:30:42 +04:00
|
|
|
api.posts.browse({status: req.params.status || 'all'})
|
|
|
|
.then(function (page) {
|
2013-05-29 04:10:39 +04:00
|
|
|
res.render('content', {
|
2013-05-16 15:21:13 +04:00
|
|
|
bodyClass: 'manage',
|
2013-05-29 04:10:39 +04:00
|
|
|
adminNav: setSelected(adminNavbar, 'content'),
|
2013-06-01 23:30:42 +04:00
|
|
|
posts: page.posts
|
2013-05-16 15:21:13 +04:00
|
|
|
});
|
2013-05-11 20:44:25 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
'settings': function (req, res) {
|
2013-05-23 17:57:37 +04:00
|
|
|
api.settings.browse()
|
|
|
|
.then(function (settings) {
|
|
|
|
res.render('settings', {
|
|
|
|
bodyClass: 'settings',
|
|
|
|
adminNav: setSelected(adminNavbar, 'settings'),
|
|
|
|
settings: settings
|
|
|
|
});
|
|
|
|
});
|
2013-05-11 20:44:25 +04:00
|
|
|
},
|
|
|
|
'debug': { /* ugly temporary stuff for managing the app before it's properly finished */
|
|
|
|
index: function (req, res) {
|
|
|
|
res.render('debug', {
|
|
|
|
bodyClass: 'settings',
|
2013-05-19 15:19:39 +04:00
|
|
|
adminNav: setSelected(adminNavbar, 'settings')
|
2013-05-11 20:44:25 +04:00
|
|
|
});
|
|
|
|
},
|
2013-06-24 00:55:03 +04:00
|
|
|
'export': function (req, res) {
|
|
|
|
// Get current version from settings
|
|
|
|
api.settings.read({ key: "currentVersion" })
|
|
|
|
.then(function (setting) {
|
|
|
|
// Export the current versions data
|
|
|
|
return dataExport(setting.value);
|
|
|
|
}, function () {
|
|
|
|
// If no setting, assume 001
|
|
|
|
return dataExport("001");
|
|
|
|
})
|
|
|
|
.then(function (exportedData) {
|
|
|
|
// Save the exported data to the file system for download
|
|
|
|
var fileName = path.resolve(__dirname + '/../../shared/data/export/exported-' + (new Date().getTime()) + '.json');
|
|
|
|
|
|
|
|
return nodefn.call(fs.writeFile, fileName, JSON.stringify(exportedData)).then(function () {
|
|
|
|
return when(fileName);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(function (exportedFilePath) {
|
|
|
|
// Send the exported data file
|
|
|
|
res.download(exportedFilePath, 'GhostData.json');
|
|
|
|
})
|
|
|
|
.otherwise(function (error) {
|
|
|
|
// Notify of an error if it occurs
|
|
|
|
req.flash("error", error.message || error);
|
|
|
|
res.redirect("/ghost/debug");
|
|
|
|
});
|
2013-05-11 20:44:25 +04:00
|
|
|
},
|
2013-06-24 00:55:03 +04:00
|
|
|
'import': function (req, res) {
|
|
|
|
if (!req.files.importfile) {
|
|
|
|
// Notify of an error if it occurs
|
|
|
|
req.flash("error", "Must select a file to import");
|
|
|
|
return res.redirect("/ghost/debug");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current version for importing
|
|
|
|
api.settings.read({ key: "currentVersion" })
|
|
|
|
.then(function (setting) {
|
|
|
|
return when(setting.value);
|
|
|
|
}, function () {
|
|
|
|
return when("001");
|
|
|
|
})
|
|
|
|
.then(function (currentVersion) {
|
|
|
|
// Read the file contents
|
|
|
|
return nodefn.call(fs.readFile, req.files.importfile.path)
|
|
|
|
.then(function (fileContents) {
|
|
|
|
var importData;
|
|
|
|
|
|
|
|
// Parse the json data
|
|
|
|
try {
|
|
|
|
importData = JSON.parse(fileContents);
|
|
|
|
} catch (e) {
|
|
|
|
return when.reject(new Error("Failed to parse the import file"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!importData.meta || !importData.meta.version) {
|
|
|
|
return when.reject(new Error("Import data does not specify version"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import for the current version
|
|
|
|
return dataImport(currentVersion, importData);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
req.flash("success", "Data imported");
|
|
|
|
})
|
|
|
|
.otherwise(function (error) {
|
|
|
|
// Notify of an error if it occurs
|
|
|
|
req.flash("error", error.message || error);
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
res.redirect("/ghost/debug");
|
|
|
|
});
|
2013-05-21 05:03:35 +04:00
|
|
|
},
|
2013-06-24 00:55:03 +04:00
|
|
|
'reset': function (req, res) {
|
|
|
|
// Grab the current version so we can get the migration
|
|
|
|
api.settings.read({ key: "currentVersion" })
|
|
|
|
.then(function (setting) {
|
|
|
|
var migration = require("../../shared/data/migration/" + setting.value);
|
|
|
|
|
|
|
|
// Run the downward migration
|
|
|
|
return migration.down();
|
|
|
|
}, function () {
|
|
|
|
// If no version in the DB, assume 001
|
|
|
|
var migration = require("../../shared/data/migration/001");
|
|
|
|
|
|
|
|
// Run the downward migration
|
|
|
|
return migration.down();
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
// Re-initalize the providers (should run the migration up again)
|
|
|
|
return dataProvider.init();
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
req.flash("success", "Database reset");
|
|
|
|
})
|
|
|
|
.otherwise(function (error) {
|
|
|
|
req.flash("error", error.message || error);
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
res.redirect('/ghost/debug');
|
|
|
|
});
|
2013-05-11 20:44:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = adminControllers;
|
2013-06-15 22:11:17 +04:00
|
|
|
}());
|