Skip to content

Commit

Permalink
Changed to resolve in UI router. Split the controllers.
Browse files Browse the repository at this point in the history
  • Loading branch information
trainerbill committed Aug 18, 2015
2 parents dced51b + 793187c commit 21fa7dc
Show file tree
Hide file tree
Showing 29 changed files with 600 additions and 286 deletions.
10 changes: 5 additions & 5 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var getGlobbedPaths = function (globPatterns, excludes) {
// The output array
var output = [];

// If glob pattern is array so we use each pattern in a recursive way, otherwise we use glob
// If glob pattern is array then we use each pattern in a recursive way, otherwise we use glob
if (_.isArray(globPatterns)) {
globPatterns.forEach(function (globPattern) {
output = _.union(output, getGlobbedPaths(globPattern, excludes));
Expand Down Expand Up @@ -49,7 +49,7 @@ var getGlobbedPaths = function (globPatterns, excludes) {
};

/**
* Validate NODE_ENV existance
* Validate NODE_ENV existence
*/
var validateEnvironmentVariable = function () {
var environmentFiles = glob.sync('./config/env/' + process.env.NODE_ENV + '.js');
Expand Down Expand Up @@ -127,10 +127,10 @@ var initGlobalConfigFiles = function (config, assets) {
config.files.server.policies = getGlobbedPaths(assets.server.policies);

// Setting Globbed js files
config.files.client.js = getGlobbedPaths(assets.client.lib.js, 'public/').concat(getGlobbedPaths(assets.client.js, ['client/', 'public/']));
config.files.client.js = getGlobbedPaths(assets.client.lib.js, 'public/').concat(getGlobbedPaths(assets.client.js, ['public/']));

// Setting Globbed css files
config.files.client.css = getGlobbedPaths(assets.client.lib.css, 'public/').concat(getGlobbedPaths(assets.client.css, ['client/', 'public/']));
config.files.client.css = getGlobbedPaths(assets.client.lib.css, 'public/').concat(getGlobbedPaths(assets.client.css, ['public/']));

// Setting Globbed test files
config.files.client.tests = getGlobbedPaths(assets.client.tests);
Expand All @@ -140,7 +140,7 @@ var initGlobalConfigFiles = function (config, assets) {
* Initialize global configuration
*/
var initGlobalConfig = function () {
// Validate NDOE_ENV existance
// Validate NODE_ENV existence
validateEnvironmentVariable();

// Get the default assets
Expand Down
7 changes: 7 additions & 0 deletions config/env/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ module.exports = {
},
port: process.env.PORT || 3000,
templateEngine: 'swig',
// Session details
// session expiration is set by default to 24 hours
sessionExpiration: 24 * (60 * 1000),
// sessionSecret should be changed for security measures and concerns
sessionSecret: 'MEAN',
// sessionKey is set to the generic sessionId key used by PHP applications
// for obsecurity reasons
sessionKey: 'sessionId',
sessionCollection: 'sessions',
logo: 'modules/core/img/brand/logo.png',
favicon: 'modules/core/img/brand/favicon.ico'
Expand Down
3 changes: 2 additions & 1 deletion config/env/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ module.exports = {
}
}
},
livereload: true
livereload: true,
seedDB: process.env.MONGO_SEED || false
};
3 changes: 2 additions & 1 deletion config/env/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ module.exports = {
pass: process.env.MAILER_PASSWORD || 'MAILER_PASSWORD'
}
}
}
},
seedDB: process.env.MONGO_SEED || false
};
3 changes: 2 additions & 1 deletion config/env/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ module.exports = {
pass: process.env.MAILER_PASSWORD || 'MAILER_PASSWORD'
}
}
}
},
seedDB: process.env.MONGO_SEED || false
};
5 changes: 5 additions & 0 deletions config/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ var config = require('../config'),
// Initialize Models
mongoose.loadModels();

//SeedDB
if (config.seedDB) {
require('./seed');
}

module.exports.loadModels = function loadModels() {
mongoose.loadModels();
};
Expand Down
6 changes: 5 additions & 1 deletion config/lib/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ module.exports.initSession = function (app, db) {
saveUninitialized: true,
resave: true,
secret: config.sessionSecret,
cookie: {
maxAge: config.sessionExpiration
},
key: config.sessionKey,
store: new MongoStore({
mongooseConnection: db.connection,
collection: config.sessionCollection
Expand Down Expand Up @@ -158,7 +162,7 @@ module.exports.initModulesClientRoutes = function (app) {

// Globbing static routing
config.folders.client.forEach(function (staticPath) {
app.use(staticPath.replace('/client', ''), express.static(path.resolve('./' + staticPath)));
app.use(staticPath, express.static(path.resolve('./' + staticPath)));
});
};

Expand Down
85 changes: 85 additions & 0 deletions config/lib/seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict';

var mongoose = require('mongoose'),
chalk = require('chalk'),
crypto = require('crypto'),
User = mongoose.model('User');

console.log(chalk.bold.red('Warning: Database seeding is turned on'));

//If production only seed admin if it does not exist
if (process.env.NODE_ENV === 'production') {
//Add Local Admin
User.find({username: 'admin'}, function (err, users) {
if (users.length === 0) {
var password = crypto.randomBytes(64).toString('hex').slice(1, 20);
var user = new User({
username: 'admin',
password: password,
provider: 'local',
email: 'admin@localhost.com',
firstName: 'Admin',
lastName: 'Local',
displayName: 'Admin Local',
roles: ['user', 'admin']
});
// Then save the user
user.save(function (err) {
if (err) {
console.log('Failed to add local admin');
} else {
console.log(chalk.bold.red('Local admin added with password set to ' + password));
}
});
} else {
console.log('Admin user exists');
}
});
} else {
//Add Local User
User.find({username: 'user'}).remove(function () {
var password = crypto.randomBytes(64).toString('hex').slice(1, 20);
var user = new User({
username: 'user',
password: password,
provider: 'local',
email: 'user@localhost.com',
firstName: 'User',
lastName: 'Local',
displayName: 'User Local',
roles: ['user']
});
// Then save the user
user.save(function (err) {
if (err) {
console.log('Failed to add local user');
} else {
console.log(chalk.bold.red('Local user added with password set to ' + password));
}
});
});


//Add Local Admin
User.find({username: 'admin'}).remove(function () {
var password = crypto.randomBytes(64).toString('hex').slice(1, 20);
var user = new User({
username: 'admin',
password: password,
provider: 'local',
email: 'admin@localhost.com',
firstName: 'Admin',
lastName: 'Local',
displayName: 'Admin Local',
roles: ['user', 'admin']
});
// Then save the user
user.save(function (err) {
if (err) {
console.log('Failed to add local admin');
} else {
console.log(chalk.bold.red('Local admin added with password set to ' + password));
}
});
});
}
7 changes: 6 additions & 1 deletion config/lib/socket.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,15 @@ module.exports = function (app, db) {
// Use the 'cookie-parser' module to parse the request cookies
cookieParser(config.sessionSecret)(socket.request, {}, function (err) {
// Get the session id from the request cookies
var sessionId = socket.request.signedCookies['connect.sid'];
var sessionId = socket.request.signedCookies ? socket.request.signedCookies[config.sessionKey] : undefined;

if (!sessionId) return next(new Error('sessionId was not found in socket.request'), false);

// Use the mongoStorage instance to get the Express session information
mongoStore.get(sessionId, function (err, session) {
if (err) return next(err, false);
if (!session) return next(new Error('session was not found for ' + sessionId), false);

// Set the Socket.io session information
socket.request.session = session;

Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function (karmaConfig) {
moduleName: 'mean',

cacheIdFromPath: function (filepath) {
return filepath.replace('/client', '');
return filepath;
},
},

Expand Down
6 changes: 4 additions & 2 deletions modules/articles/client/config/articles.client.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ angular.module('articles').run(['Menus',
Menus.addMenuItem('topbar', {
title: 'Articles',
state: 'articles',
type: 'dropdown'
type: 'dropdown',
roles: ['*']
});

// Add the dropdown list item
Expand All @@ -19,7 +20,8 @@ angular.module('articles').run(['Menus',
// Add the dropdown create item
Menus.addSubMenuItem('topbar', 'articles', {
title: 'Create Articles',
state: 'articles.create'
state: 'articles.create',
roles: ['user']
});
}
]);
73 changes: 63 additions & 10 deletions modules/articles/client/config/articles.client.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,84 @@ angular.module('articles').config(['$stateProvider',
.state('articles', {
abstract: true,
url: '/articles',
template: '<ui-view/>',
controller: 'ArticlesController'
template: '<ui-view/>'
})
.state('articles.list', {
url: '',
templateUrl: 'modules/articles/views/list-articles.client.view.html',
controller: 'ArticlesListController'
templateUrl: 'modules/articles/client/views/list-articles.client.view.html',
controller: 'ArticlesListController',
resolve:{
articles: function (Articles, $stateParams, $state) {
return Articles.query().$promise.then(
function (articles) {
//success
return articles;
},
function () {
//fail
$state.go('not-found');
}
);
}
}
})
.state('articles.create', {
url: '/create',
templateUrl: 'modules/articles/views/create-article.client.view.html',
controller: 'ArticlesCreateController',
templateUrl: 'modules/articles/client/views/create-article.client.view.html',
controller: 'ArticlesController',
data: {
roles: ['user', 'admin']
},
resolve: {
article: function () {
return { mock: 'mock' };
}
}
})
.state('articles.view', {
url: '/:articleId',
templateUrl: 'modules/articles/views/view-article.client.view.html',
controller: 'ArticlesViewController'
templateUrl: 'modules/articles/client/views/view-article.client.view.html',
controller: 'ArticlesController',
resolve: {
article: function (Articles, $stateParams, $state) {
return Articles.get({
articleId: $stateParams.articleId
}).$promise.then(
function (article) {
//success
return article;
},
function () {
//fail
$state.go('not-found');
}
);
}
}
})
.state('articles.edit', {
url: '/:articleId/edit',
templateUrl: 'modules/articles/views/edit-article.client.view.html',
controller: 'ArticlesEditController',
templateUrl: 'modules/articles/client/views/edit-article.client.view.html',
controller: 'ArticlesController',
resolve: {
article: function (Articles, $stateParams, $state) {
return Articles.get({
articleId: $stateParams.articleId
}).$promise.then(
function (article) {
//Auth Check
if (article === undefined || typeof article !== 'object' || article._id === undefined) {
$state.go('unauthorized');
}
return article;
},
function () {
//fail
$state.go('not-found');
}
);
}
},
data: {
roles: ['user', 'admin']
}
Expand Down
Loading

0 comments on commit 21fa7dc

Please sign in to comment.