From 96c57465f3225670615bfc82c2736351e0d5ef50 Mon Sep 17 00:00:00 2001 From: Jonnie Spratley Date: Sat, 10 Jan 2015 16:20:42 -0800 Subject: [PATCH] fixed files --- Gruntfile.js | 4 +- app/scripts/directives/cms-widget.js | 15 +- app/scripts/directives/cms-widgets.js | 15 +- config/config.json | 15 +- package.json | 4 +- routes/cms-auth.js | 74 +++++---- routes/cms-passport.js | 215 +++++++++++++++++--------- routes/cms-proxy.js | 8 +- routes/cms-router.js | 59 ++----- routes/cms-server.js | 25 ++- routes/rest.js | 5 +- www/layout.ejs | 28 +--- 12 files changed, 254 insertions(+), 213 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 74fbbac..929dda2 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -6,7 +6,7 @@ var fs = require('fs'), cmsRouter = require('./routes/cms-router.js'), config = JSON.parse(fs.readFileSync('./config/config.json')), LIVERELOAD_PORT = 35729, - SERVER_PORT = 9000; + SERVER_PORT = process.env.PORT || 9000; var startNodeServer = function(){ @@ -678,7 +678,7 @@ module.exports = function (grunt) { 'usemin' ]); - grunt.registerTask('docs', ['coffee', 'ngdocs', 'connect:docs', 'watch:ngdocs']); + grunt.registerTask('docs', ['coffee', 'ngdocs', 'connect:docs', 'watch:scripts']); grunt.registerTask('default', ['newer:jshint', 'test', 'build']); grunt.registerTask('heroku:production', 'build'); diff --git a/app/scripts/directives/cms-widget.js b/app/scripts/directives/cms-widget.js index 6dbfb71..2ab0dfd 100644 --- a/app/scripts/directives/cms-widget.js +++ b/app/scripts/directives/cms-widget.js @@ -17,9 +17,10 @@ */ -angular.module('angularCmsApp').directive("cmsWidget", function() { +angular.module('angularCmsApp').directive('cmsWidget', function() { + 'use strict'; return { - restrict: "E", + restrict: 'ECA', replace: true, transclude: true, scope: { @@ -29,22 +30,22 @@ angular.module('angularCmsApp').directive("cmsWidget", function() { icon: "@" }, require: '?^cmsWidgets', - template: '
\n
\n
\n \n {{title}}\n
\n
\n
\n
', + template: '
\n
\n \n {{title}}\n
\n
\n
\n
', link: function(scope, element, attrs, ctrl) { var opened, toggle, widgetTitle; toggle = function() { var opened; opened = !opened; - return element.find("section").slideToggle(function() { - return element.toggleClass((opened ? "closed" : "opened")); + element.find('section').slideToggle(function() { + element.toggleClass((opened ? 'closed' : 'opened')); }); }; - widgetTitle = element.find("header"); + widgetTitle = element.find('header'); widgetTitle.css({ cursor: 'move' }); opened = true; - return widgetTitle.bind("click", toggle); + widgetTitle.bind('click', toggle); } }; }); diff --git a/app/scripts/directives/cms-widgets.js b/app/scripts/directives/cms-widgets.js index ec40e7a..8a76da0 100644 --- a/app/scripts/directives/cms-widgets.js +++ b/app/scripts/directives/cms-widgets.js @@ -9,7 +9,7 @@ This is the container for widgets. @example - + HTML example come here ==> `
` @@ -17,9 +17,9 @@
*/ -'use strict'; + angular.module('angularCmsApp').directive('cmsWidgets', function() { - var postLink; + 'use strict'; return { restrict: 'EMA', replace: true, @@ -35,13 +35,12 @@ angular.module('angularCmsApp').directive('cmsWidgets', function() { } return widgets.push(widget); }; - return $scope.select = function(widget) { + $scope.select = function(widget) { angular.forEach(widgets, function(widget) { - return widget.selected = false; + widget.selected = false; }); - return widget.selected = true; + widget.selected = true; }; - }, - link: postLink = function(scope, element, attrs) {} + } }; }); diff --git a/config/config.json b/config/config.json index 60183df..e1a192c 100644 --- a/config/config.json +++ b/config/config.json @@ -1,5 +1,5 @@ { - "baseurl":"http://js2coffee.org", + "baseurl":"http://localhost:8181", "sitetitle":"angular-cms", "sitedesc":"This is the description", "sitebrand":"/images/angular-cms-brand.png", @@ -17,14 +17,13 @@ "key": "./config/apache.key", "cert":"./config/apache.crt" }, - "db":{ "name":"angular-cms", - "username":"amadmin", - "password":"fred", - "host":"localhost", - "port":27017, - "url":"localhost/angular-cms" + "username":"angularcms-dev", + "password":"angularcms-dev", + "host":"paulo.mongohq.com:10089", + "port":31711, + "url":"angularcms-dev:angularcms-dev@paulo.mongohq.com:10089/app19632340" }, "email":{ "username":"angular.cms@gmail.com", @@ -34,7 +33,7 @@ "hostname":"localhost", "port":5001 }, - "staticDir":"www", + "staticDir":"dist", "publicDir":"www", "uploadsTmpDir":".tmp", "uploadsDestDir":"www/cms-content/uploads", diff --git a/package.json b/package.json index 0bca2d1..2cb2cb3 100755 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "emailjs": "~0.3.6", "express": "~4.0.0", "express-session": "^1.9.3", + "finalhandler": "^0.3.3", "fs-extra": "^0.12.0", "http-proxy": "^1.3.0", "jps-ds": "~0.0.2", @@ -59,6 +60,7 @@ "promised-io": "~0.3.4", "q": "^1.1.2", "request": "~2.34.0", + "serve-static": "^1.8.0", "socket.io": "~0.9.16", "websocket": "^1.0.14" }, @@ -133,6 +135,6 @@ }, "scripts": { "test": "grunt test", - "start": "node web.js" + "start": "node server.js" } } diff --git a/routes/cms-auth.js b/routes/cms-auth.js index 520ce11..cf52120 100644 --- a/routes/cms-auth.js +++ b/routes/cms-auth.js @@ -1,19 +1,13 @@ -var bodyParser = require('body-parser'), - mongoose = require('mongoose'), - util = require('util'), - User = require('./models/user'), - session = require('express-session'), - crypto = require('crypto'), - bcrypt = require('bcrypt-nodejs'); +var bodyParser = require('body-parser'), mongoose = require('mongoose'), util = require('util'), User = require('./models/user'), session = require('express-session'), +crypto = require('crypto'), +bcrypt = require('bcrypt-nodejs'); - -var cmsAuth = function (config, app) { +var cmsAuth = function(config, app) { console.warn('cms-auth'); - //### hashPassword //Hash password using basic sha1 hash. - var hashPassword = function (pass, salt) { + var hashPassword = function(pass, salt) { return bcrypt.hashSync(pass); }; @@ -25,7 +19,7 @@ var cmsAuth = function (config, app) { * @param res * @param next */ - login: function (req, res, next) { + login : function(req, res, next) { var query = {}; if (req.body.username) { query.username = req.body.username; @@ -35,7 +29,9 @@ var cmsAuth = function (config, app) { } query.password = hashPassword(req.body.password, query.username); console.warn('trying to login', query); - User.findOne({username: query.username}, function (err, data) { + User.findOne({ + username : query.username + }, function(err, data) { if (err) { return res.jsonp(400, err); } @@ -44,10 +40,14 @@ var cmsAuth = function (config, app) { req.session.user = data; return res.json(200, data); } else { - return res.json(404, {message: 'Wrong username/password!'}); + return res.json(404, { + message : 'Wrong username/password!' + }); } } catch (error) { - return res.json(404, {message: error}); + return res.json(404, { + message : error + }); } }); }, @@ -57,7 +57,7 @@ var cmsAuth = function (config, app) { * @param res * @param next */ - register: function (req, res, next) { + register : function(req, res, next) { var data = req.body; //TODO: Need to make this externalized. @@ -76,21 +76,30 @@ var cmsAuth = function (config, app) { console.warn('trying to register', data); - //Try and find user - User.find({username: data.username}, function (err, u) { - console.log('found user', err, util.inspect(u, {colors: true})); + User.find({ + username : data.username + }, function(err, u) { + console.log('found user', err, util.inspect(u, { + colors : true + })); var user = new User(data); if (err) { - res.jsonp(400, {message: 'Problem registering!'}); + res.jsonp(400, { + message : 'Problem registering!' + }); } if (u.length) { - res.jsonp(400, {message: 'Username already exists!'}); + res.jsonp(400, { + message : 'Username already exists!' + }); } else { - user.save(function (er, ok) { + user.save(function(er, ok) { if (er) { - return res.jsonp(400, {message: 'Problem registering!'}); + return res.jsonp(400, { + message : 'Problem registering!' + }); } else { return res.jsonp(201, ok); } @@ -99,21 +108,26 @@ var cmsAuth = function (config, app) { }); }, - session: function (req, res, next) { + session : function(req, res, next) { var user = req.session; if (req.session && req.session.user) { - user = req.session.user + user = req.session.user; } - console.warn(util.inspect(user, {colors: true})); - return res.send({message: 'Your session', data: user}); + console.warn(util.inspect(user, { + colors : true + })); + return res.send({ + message : 'Your session', + data : user + }); } }; //Always users table app.use(session({ - secret: 'angular-cms', - resave: true, - saveUninitialized: true + secret : 'angular-cms', + resave : true, + saveUninitialized : true })); app.get(config.apiBase + '/login', bodyParser.json(), cmsAuth.login); app.post(config.apiBase + '/login', bodyParser.json(), cmsAuth.login); diff --git a/routes/cms-passport.js b/routes/cms-passport.js index b4a51aa..4778e30 100644 --- a/routes/cms-passport.js +++ b/routes/cms-passport.js @@ -1,21 +1,9 @@ -var passport = require('passport'), - BasicStrategy = require('passport-http').BasicStrategy, - LocalStrategy = require('passport-local').Strategy, - GoogleStrategy = require('passport-google').Strategy, - express = require('express'), - path = require('path'), - q = require('q'), - flash = require('connect-flash'), - User = require('./models/user'), - cookieParser = require('cookie-parser'), - bodyParser = require('body-parser'), - session = require('express-session'), - mongoose = require('mongoose'), - util = require('util'), - User = require('./models/user'), - session = require('express-session'), - crypto = require('crypto'), - bcrypt = require('bcrypt-nodejs'); +var passport = require('passport'), BasicStrategy = require('passport-http').BasicStrategy, LocalStrategy = require('passport-local').Strategy, GoogleStrategy = require('passport-google').Strategy, express = require('express'), path = require('path'), q = require('q'), flash = require('connect-flash'), User = require('./models/user'), cookieParser = require('cookie-parser'), bodyParser = require('body-parser'), session = require('express-session'), mongoose = require('mongoose'), util = require('util'), User = require('./models/user'), session = require('express-session'), crypto = require('crypto'), bcrypt = require('bcrypt-nodejs'); + +//Hash password using basic sha1 hash. +var hashPassword = function(pass, salt) { + return bcrypt.hashSync(pass); +}; /** * I handle ensuring a user is authenticated. * @param req @@ -23,7 +11,7 @@ var passport = require('passport'), * @param next * @returns {*} */ -var ensureAuthenticated = function (req, res, next) { +var ensureAuthenticated = function(req, res, next) { if (req.isAuthenticated()) { return next(); } @@ -36,7 +24,7 @@ var ensureAuthenticated = function (req, res, next) { * @param config - Configuration settings * @param app - Express app */ -var cmsPassport = function (config, app) { +var cmsPassport = function(config, app) { var user = new User(); @@ -44,34 +32,98 @@ var cmsPassport = function (config, app) { throw new Error('Must provide express application!'); } - /* - + /** + * I handle finding or creating a user. */ - var findOrCreate = function (u, done) { + var findOrCreate = function(u, done) { console.log('find', u); for (var i = 0; i < u.emails.length; i++) { - var email = u.emails[i].value - User.findOne({email: email}, function (err, user) { + var email = u.emails[i].value; + + User.findOne({ + email : email + }, function(err, user) { if (err) { return done(err); } + if (!user) { console.warn('create user', u); return done(null, false); + } else { + user.google = u; + user.update( { username: user.username }, user, function(){ + console.warn('update user', u); + done(null, user); + } ); + } - return done(null, user); + }); } }; + var registerUser = function(req, res, next) { + var data = req.body; + + //TODO: Need to make this externalized. + if (req.body.username) { + data.username = req.body.username; + } + if (req.body.email) { + data.email = req.body.email; + } + + data.password = hashPassword(req.body.password, data.username); + data.created_at = new Date(); + data.updated_at = new Date(); + data.active = false; + data.groups = ['public']; + + console.warn('trying to register', data); + + User.find({ + username : data.username + }, function(err, u) { + console.log('found user', err, util.inspect(u, { + colors : true + })); + + var user = new User(data); + + if (err) { + res.jsonp(400, { + message : 'Problem registering!' + }); + } + + if (u.length) { + res.jsonp(400, { + message : 'Username already exists!' + }); + } else { + user.save(function(er, ok) { + if (er) { + return res.jsonp(400, { + message : 'Problem registering!' + }); + } else { + return res.jsonp(201, ok); + } + }); + } + + }); + }; + /** * I handle serializing a user. * @param user * @param done */ - var serializeUser = function (user, done) { + var serializeUser = function(user, done) { done(null, user._id); }; @@ -80,8 +132,8 @@ var cmsPassport = function (config, app) { * @param id * @param done */ - var deserializeUser = function (id, done) { - User.findById(id, function (err, user) { + var deserializeUser = function(id, done) { + User.findById(id, function(err, user) { done(err, user); }); }; @@ -90,9 +142,11 @@ var cmsPassport = function (config, app) { passport.deserializeUser(deserializeUser); - var strategy = function (username, password, done) { + var strategy = function(username, password, done) { console.warn('find user', username, password); - User.findOne({username: username}, function (err, user) { + User.findOne({ + username : username + }, function(err, user) { if (err) { return done(err); } @@ -110,86 +164,101 @@ var cmsPassport = function (config, app) { passport.use(new LocalStrategy(strategy)); passport.use(new GoogleStrategy({ - returnURL: config.host + config.port + '/auth/google/return', - realm: config.host + config.port - }, - function (identifier, profile, done) { - console.warn('googleCallback', profile); - profile.openId = identifier; - findOrCreate(profile, function (err, user) { - done(err, user); - }); - } - )); + returnURL : config.host + ':' + config.port + '/auth/google/return', + realm : config.host + ':' + config.port + }, function(identifier, profile, done) { + console.warn('googleCallback', profile); + profile.openId = identifier; + findOrCreate(profile, function(err, user) { + done(err, user); + }); + })); + /** + * TODO: Handle configuring passport + * + */ app.use(express.static(path.resolve(config.publicDir))); - app.set('views', path.resolve(config.staticDir)); + app.set('views', path.resolve(config.publicDir)); app.set('view engine', 'ejs'); app.engine('ejs', require('ejs-locals')); + app.use(cookieParser()); - app.use(bodyParser.urlencoded({extended: false})); + app.use(bodyParser.urlencoded({ + extended : false + })); app.use(bodyParser.json()); app.use(session({ - secret: 'angular-cms', - resave: true, - saveUninitialized: true + secret : 'angular-cms', + resave : true, + saveUninitialized : true })); app.use(passport.initialize()); app.use(passport.session()); app.use(flash()); - - app.get('/api/me', passport.authenticate('basic', {session: false}), function (req, res) { + app.get('/api/me', passport.authenticate('basic', { + session : false + }), function(req, res) { res.json(req.user); }); - app.all('*', function (req, res, next) { + app.all('*', function(req, res, next) { req.config = JSON.stringify(config); console.warn('cmsAuth', req.params); next(); }); - app.get('/', function (req, res) { - res.render('index', {user: req.user, message: 'Please login', status: 'info'}); + app.get('/', function(req, res) { + res.render('index', { + user : req.user, + message : 'Please login', + status : 'info' + }); }); - app.get('/account', ensureAuthenticated, function (req, res) { + app.get('/account', ensureAuthenticated, function(req, res) { res.render('account', { - user: req.user, - config: req.config + user : req.user, + config : req.config }); }); - app.get('/login', function (req, res) { - res.render('login', {user: req.user, message: 'Please login', status: 'warning'}); + app.get('/login', function(req, res) { + res.render('login', { + user : req.user, + message : 'Please login', + status : 'warning' + }); }); - app.post('/login', - passport.authenticate('local', { - successRedirect: '/account', - failureRedirect: '/login', - failureFlash: false - }) - ); + app.get('/register', function(req, res) { + res.render('register'); + }); - app.get('/auth/user', ensureAuthenticated, function (req, res) { + app.post('/register', registerUser); + + app.post('/login', passport.authenticate('local', { + successRedirect : '/account', + failureRedirect : '/login', + failureFlash : false + })); + + app.get('/auth/user', ensureAuthenticated, function(req, res) { res.json(200, req.user); }); - app.get('/auth/logout', function (req, res) { + app.get('/auth/logout', function(req, res) { req.logout(); res.redirect(options.apiBase); }); app.get('/auth/google', passport.authenticate('google')); - app.get('/auth/google/return', - passport.authenticate('google', { - successRedirect: '/', - failureRedirect: '/login' - })); - + app.get('/auth/google/return', passport.authenticate('google', { + successRedirect : '/account', + failureRedirect : '/login' + })); }; - module.exports = cmsPassport; diff --git a/routes/cms-proxy.js b/routes/cms-proxy.js index 26fee55..49c938e 100644 --- a/routes/cms-proxy.js +++ b/routes/cms-proxy.js @@ -4,7 +4,7 @@ var fs = require('fs'), module.exports = function (config, app) { - console.warn( 'cms-proxy', 'initialized' ); + console.warn( 'cms-proxy', 'initialized', config.proxy ); /** * @TODO - HTTPS Key and Cert @@ -20,7 +20,7 @@ module.exports = function (config, app) { * This object holds options used for creating a proxy server. */ var options = { - port: null, + port: 8080, host: { hostname: 'localhost', port: 8181 @@ -73,5 +73,7 @@ module.exports = function (config, app) { //Start the proxy server - proxyServer.listen(config.proxy.port); + proxyServer.listen(config.proxy.port, function(){ + console.log('cms-proxy server listening on ', config.proxy.port); + }); }; diff --git a/routes/cms-router.js b/routes/cms-router.js index 5f6ae25..4322d2c 100644 --- a/routes/cms-router.js +++ b/routes/cms-router.js @@ -1,64 +1,27 @@ -var mongoose = require('mongoose'), - http = require('http'), - util = require('util'); +var http = require('http'), util = require('util'); module.exports.mount = function(config, app) { 'use strict'; - //Connect to database - mongoose.connect(config.db.url); - try { - - } - catch (err) { - throw new Error('Unable to connect to MongoDB at ' + config.db.url); - } - - - - // CONNECTION EVENTS - // When successfully connected - mongoose.connection.on('connected', function() { - console.log('Mongoose default connection open to ', config.db); - }); - - // If the connection throws an error - mongoose.connection.on('error', function(err) { - console.log('Mongoose default connection error: ' + err); - }); - - // When the connection is disconnected - mongoose.connection.on('disconnected', function() { - console.log('Mongoose default connection disconnected'); - }); - - // If the Node process ends, close the Mongoose connection - process.on('SIGINT', function() { - mongoose.connection.close(function() { - console.log('Mongoose default connection disconnected through app termination'); - process.exit(0); - }); - }); - - var server = http.createServer(app); + var serverPort = process.env.PORT || config.port; + var serverHost = process.env.IP || config.host; + require('./cms-db')(config); require('./cms-auth')(config, app); require('./cms-passport')(config, app); + require('./cms-upload')(config, app); require('./cms-rest')(config, app); require('./cms-proxy')(config, app); - require('./cms-upload')(config, app); - require('./cms-sockets')(config, server); require('./cms-server')(config, app); - - - var serverPort = process.env.PORT || config.port; - var serverHost = process.env.IP || config.host; + require('./cms-sockets')(config, server); + + + + + server.listen(config.port, function() { util.log('App listening on port: ' + config.port + ''.verbose); - util.log(util.inspect(config, { - colors: true - })); }); }; diff --git a/routes/cms-server.js b/routes/cms-server.js index 9e8cef7..4011cac 100644 --- a/routes/cms-server.js +++ b/routes/cms-server.js @@ -1,5 +1,7 @@ var express = require('express'), path = require('path'), + serveStatic = require('serve-static'), + finalhandler = require('finalhandler'), bodyParser = require( 'body-parser' ); @@ -11,24 +13,33 @@ module.exports = function (config, app) { var options = { dotfiles: 'ignore', etag: false, - extensions: ['png', 'html', 'jpeg', 'jpg', 'gif', 'css'], + extensions: [ + 'js', + 'png', + 'html', 'jpeg', 'jpg', 'gif', + 'css' + ], index: true, maxAge: '1d', redirect: false, setHeaders: function (res, path) { - res.set('x-timestamp', Date.now()) + res.set('x-timestamp', Date.now()); } }; - - - router.use(express.static(config.staticDir, options)); - app.all('/', function(req, res, next) { + + + app.use(express.static(config.publicDir, options)); + app.use(express.static(config.staticDir, options)); + + + router.all('/', function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Headers', 'X-Requested-With'); next(); + console.log('cms-server', req.method); }); - app.get('/', function(res, req, next){ + router.get('/', function(res, req, next){ req.send(config.publicDir + path.sep + 'index.html'); next(); }); diff --git a/routes/rest.js b/routes/rest.js index 4c5445b..3c7d3f9 100755 --- a/routes/rest.js +++ b/routes/rest.js @@ -28,7 +28,7 @@ var sio = require('socket.io'); var Deferred = require("promised-io/promise").Deferred; var when = require("promised-io/promise"); var bodyParser = require('body-parser'); -var busboy = require('connect-busboy'); //middleware for form/file upload +var busboy = require('connect-busboy'); var markdown = require("markdown").markdown; @@ -39,9 +39,10 @@ var MESSAGES = { USER_REGISTRATION_EXISTS: 'User already in exists.' }; +var config = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../config/config.json'))); var DS = require('jps-ds').DS; var _ds = new DS({ - host: 'angularcms:angularcms@paulo.mongohq.com:10089/app19632340', + host: config.db.url, //host: 'localhost/angular-cms', models: { 'groups': { diff --git a/www/layout.ejs b/www/layout.ejs index cffc13a..638b652 100644 --- a/www/layout.ejs +++ b/www/layout.ejs @@ -7,31 +7,11 @@ - - - +
- <%- body %> - - +
+ <%- body %> +