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'),
|
|
|
|
_ = require('underscore'),
|
|
|
|
fs = require('fs'),
|
2013-05-16 15:21:13 +04:00
|
|
|
api = require('../../shared/api'),
|
2013-05-11 20:44:25 +04:00
|
|
|
|
|
|
|
ghost = new Ghost(),
|
|
|
|
adminNavbar,
|
|
|
|
adminControllers;
|
|
|
|
|
|
|
|
// TODO: combine path/navClass to single "slug(?)" variable with no prefix
|
|
|
|
adminNavbar = {
|
|
|
|
dashboard: {
|
|
|
|
name: 'Dashboard',
|
|
|
|
navClass: 'dashboard',
|
|
|
|
key: 'admin.navbar.dashboard',
|
|
|
|
defaultString: 'dashboard',
|
|
|
|
path: ''
|
|
|
|
},
|
|
|
|
blog: {
|
|
|
|
name: 'Content',
|
|
|
|
navClass: 'content',
|
|
|
|
key: 'admin.navbar.blog',
|
|
|
|
defaultString: 'blog',
|
|
|
|
path: '/blog'
|
|
|
|
},
|
|
|
|
add: {
|
|
|
|
name: 'New Post',
|
|
|
|
navClass: 'editor',
|
|
|
|
key: 'admin.navbar.editor',
|
|
|
|
defaultString: 'editor',
|
|
|
|
path: '/editor'
|
|
|
|
},
|
|
|
|
settings: {
|
|
|
|
name: 'Settings',
|
|
|
|
navClass: 'settings',
|
|
|
|
key: 'admin.navbar.settings',
|
|
|
|
defaultString: 'settings',
|
|
|
|
path: '/settings'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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-21 05:03:35 +04:00
|
|
|
console.log(req.body);
|
|
|
|
api.users.find({email: req.body.email, pw: req.body.password}).then(function (user) {
|
|
|
|
if (user) {
|
|
|
|
console.log('user found: ', user);
|
|
|
|
req.session.user = "ghostadmin";
|
|
|
|
res.redirect(req.query.redirect || '/ghost/');
|
|
|
|
} else {
|
|
|
|
res.redirect('/ghost/login/');
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
},
|
|
|
|
'register': function (req, res) {
|
|
|
|
res.render('register', {
|
|
|
|
bodyClass: 'ghost-login',
|
|
|
|
hideNavbar: true,
|
|
|
|
adminNav: setSelected(adminNavbar, 'login')
|
|
|
|
});
|
|
|
|
},
|
|
|
|
'doRegister': function (req, res) {
|
|
|
|
// console.log(req.body);
|
|
|
|
if (req.body.email !== '' && req.body.password.length > 5) {
|
|
|
|
// console.log('okay, this is happening');
|
|
|
|
api.users.add({email: req.body.email, password: req.body.password}).then(function (user) {
|
|
|
|
console.log('user added', user);
|
|
|
|
res.redirect('/ghost/login/');
|
|
|
|
|
|
|
|
});
|
2013-05-24 10:19:19 +04:00
|
|
|
} else {
|
|
|
|
req.flash('error', "The password is too short. Have at least 6 characters in there");
|
|
|
|
res.redirect('/ghost/register/');
|
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',
|
|
|
|
adminNav: setSelected(adminNavbar, 'blog'),
|
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')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'blog': function (req, res) {
|
2013-05-16 15:21:13 +04:00
|
|
|
api.posts.browse()
|
|
|
|
.then(function (posts) {
|
|
|
|
res.render('blog', {
|
|
|
|
bodyClass: 'manage',
|
|
|
|
adminNav: setSelected(adminNavbar, 'blog'),
|
2013-05-17 00:56:26 +04:00
|
|
|
posts: posts.toJSON()
|
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) {
|
|
|
|
settings = settings.toJSON();
|
|
|
|
settings = _.object(_.pluck(settings, 'key'), _.pluck(settings, 'value'));
|
|
|
|
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
|
|
|
});
|
|
|
|
},
|
|
|
|
'dbdelete': function (req, res) {
|
|
|
|
fs.writeFile(__dirname + '/../ghost/data/datastore.db', '', function (error) {
|
|
|
|
if (error) {
|
|
|
|
req.flash('error', error);
|
|
|
|
} else {
|
|
|
|
req.flash('success', 'Everything got deleted');
|
|
|
|
}
|
|
|
|
res.redirect('/ghost/debug');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
'dbpopulate': function (req, res) {
|
|
|
|
ghost.dataProvider().populateData(function (error) {
|
|
|
|
if (error) {
|
|
|
|
req.flash('error', error);
|
|
|
|
} else {
|
|
|
|
req.flash('success', 'Data populated');
|
|
|
|
}
|
|
|
|
res.redirect('/ghost/debug');
|
|
|
|
});
|
2013-05-21 05:03:35 +04:00
|
|
|
},
|
|
|
|
'newUser': function (req, res) {
|
|
|
|
ghost.dataProvider().addNewUser(req, function (error) {
|
|
|
|
if (error) {
|
|
|
|
req.flash('error', error);
|
|
|
|
} else {
|
|
|
|
req.flash('success', 'User Added');
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2013-05-11 20:44:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = adminControllers;
|
|
|
|
}());
|