Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Adding Service Layer Implementation #164

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 23 additions & 28 deletions app/controllers/articles.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@
var mongoose = require('mongoose'),
errorHandler = require('./errors'),
Article = mongoose.model('Article'),
_ = require('lodash');
_ = require('lodash'),
articleService = require('../services/articles.server.service');

/**
* Create a article
*/
exports.create = function(req, res) {
var article = new Article(req.body);
article.user = req.user;
articleService.create(req.body, req.user, function(err, article){
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
}

article.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(article);
}
res.jsonp(article);
});
};

Expand All @@ -37,60 +35,57 @@ exports.read = function(req, res) {
* Update a article
*/
exports.update = function(req, res) {
var article = req.article;

article = _.extend(article, req.body);

article.save(function(err) {
articleService.update(req.article, req.body, function(err, article){
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(article);
}

res.jsonp(article);
});
};

/**
* Delete an article
*/
exports.delete = function(req, res) {
var article = req.article;

article.remove(function(err) {
articleService.delete(req.article, function(err, article){
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(article);
}

res.jsonp(article);
});
};

/**
* List of Articles
*/
exports.list = function(req, res) {
Article.find().sort('-created').populate('user', 'displayName').exec(function(err, articles) {
articleService.list(req.user, function(err, articles){
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(articles);
}

res.jsonp(articles);
});
};

/**
* Article middleware
*/
exports.articleByID = function(req, res, next, id) {
Article.findById(id).populate('user', 'displayName').exec(function(err, article) {
if (err) return next(err);
if (!article) return next(new Error('Failed to load article ' + id));
articleService.articleById(id, function(err, article){
if (err) {
return next(err);
}

req.article = article;
next();
});
Expand Down
214 changes: 72 additions & 142 deletions app/controllers/users/users.authentication.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,57 @@
/**
* Module dependencies.
*/
var _ = require('lodash'),
errorHandler = require('../errors'),
mongoose = require('mongoose'),
var errorHandler = require('../errors'),
passport = require('passport'),
User = mongoose.model('User');
userAuthenticationService = require('../../services/users.authentication.server.service');

/**
* Signup
*/
exports.signup = function(req, res) {
// For security measurement we remove the roles from the req.body object
delete req.body.roles;

// Init Variables
var user = new User(req.body);
var message = null;

// Add missing user fields
user.provider = 'local';
user.displayName = user.firstName + ' ' + user.lastName;

// Then save the user
user.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
// Remove sensitive data before login
user.password = undefined;
user.salt = undefined;

req.login(user, function(err) {
if (err) {
res.status(400).send(err);
} else {
res.jsonp(user);
}
});
}
// Signup user throughout service
userAuthenticationService.signup(req.body, function(err, user){
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
}

// login user
req.login(user, function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
}

return res.jsonp(user);
});
});
};

/**
* Signin after passport authentication
*/
exports.signin = function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err || !user) {
res.status(400).send(info);
} else {
// Remove sensitive data before login
user.password = undefined;
user.salt = undefined;

req.login(user, function(err) {
if (err) {
res.status(400).send(err);
} else {
res.jsonp(user);
}
});
}
})(req, res, next);
passport.authenticate('local',
function(err, user, info) {
userAuthenticationService.authenticate(err, user, info, function (err, user) {
if (err || !user) {
return res.status(400).send(info);
}

req.login(user, function (err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
}

return res.jsonp(user);
});
});
})(req, res, next);
};

/**
Expand Down Expand Up @@ -102,105 +89,48 @@ exports.oauthCallback = function(strategy) {
*/
exports.saveOAuthUserProfile = function(req, providerUserProfile, done) {
if (!req.user) {
// Define a search query fields
var searchMainProviderIdentifierField = 'providerData.' + providerUserProfile.providerIdentifierField;
var searchAdditionalProviderIdentifierField = 'additionalProvidersData.' + providerUserProfile.provider + '.' + providerUserProfile.providerIdentifierField;

// Define main provider search query
var mainProviderSearchQuery = {};
mainProviderSearchQuery.provider = providerUserProfile.provider;
mainProviderSearchQuery[searchMainProviderIdentifierField] = providerUserProfile.providerData[providerUserProfile.providerIdentifierField];

// Define additional provider search query
var additionalProviderSearchQuery = {};
additionalProviderSearchQuery[searchAdditionalProviderIdentifierField] = providerUserProfile.providerData[providerUserProfile.providerIdentifierField];

// Define a search query to find existing user with current provider profile
var searchQuery = {
$or: [mainProviderSearchQuery, additionalProviderSearchQuery]
};

User.findOne(searchQuery, function(err, user) {
if (err) {
return done(err);
} else {
if (!user) {
var possibleUsername = providerUserProfile.username || ((providerUserProfile.email) ? providerUserProfile.email.split('@')[0] : '');

User.findUniqueUsername(possibleUsername, null, function(availableUsername) {
user = new User({
firstName: providerUserProfile.firstName,
lastName: providerUserProfile.lastName,
username: availableUsername,
displayName: providerUserProfile.displayName,
email: providerUserProfile.email,
provider: providerUserProfile.provider,
providerData: providerUserProfile.providerData
});

// And save the user
user.save(function(err) {
return done(err, user);
});
});
} else {
return done(err, user);
}
}
});
userAuthenticationService.saveNewOAuthUserProfile(
providerUserProfile,
function(err, user){
return done(err, user);
}
);
} else {
// User is already logged in, join the provider data to the existing user
var user = req.user;

// Check if user exists, is not signed in using this provider, and doesn't have that provider data already configured
if (user.provider !== providerUserProfile.provider && (!user.additionalProvidersData || !user.additionalProvidersData[providerUserProfile.provider])) {
// Add the provider data to the additional provider data field
if (!user.additionalProvidersData) user.additionalProvidersData = {};
user.additionalProvidersData[providerUserProfile.provider] = providerUserProfile.providerData;

// Then tell mongoose that we've updated the additionalProvidersData field
user.markModified('additionalProvidersData');

// And save the user
user.save(function(err) {
return done(err, user, '/#!/settings/accounts');
});
} else {
return done(new Error('User is already connected using this provider'), user);
}
userAuthenticationService.saveExistingOAuthUserProfile(
req.user,
providerUserProfile,
function(err, user){
if (err){
return done(err, user);
}

return done(null, user, '/#!/settings/accounts');
}
);
}
};

/**
* Remove OAuth provider
*/
exports.removeOAuthProvider = function(req, res, next) {
var user = req.user;
var provider = req.param('provider');

if (user && provider) {
// Delete the additional provider
if (user.additionalProvidersData[provider]) {
delete user.additionalProvidersData[provider];

// Then tell mongoose that we've updated the additionalProvidersData field
user.markModified('additionalProvidersData');
}

user.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
req.login(user, function(err) {
if (err) {
res.status(400).send(err);
} else {
res.jsonp(user);
}
});
}
});
}
userAuthenticationService.removeOAuthProvider(req.user, provider, function(err, user){
if (err){
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
}

req.login(user, function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
}

return res.jsonp(user);
});
});
};
19 changes: 8 additions & 11 deletions app/controllers/users/users.authorization.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@
* Module dependencies.
*/
var _ = require('lodash'),
mongoose = require('mongoose'),
User = mongoose.model('User');
userAuthorizationService = require('../../services/users.authorization.server.service');

/**
* User middleware
*/
exports.userByID = function(req, res, next, id) {
User.findOne({
_id: id
}).exec(function(err, user) {
if (err) return next(err);
if (!user) return next(new Error('Failed to load User ' + id));
req.profile = user;
next();
});
userAuthorizationService.userByID(id, function(err, user){
if (err) return next(err);
if (!user) return next(new Error('Failed to load User ' + id));
req.profile = user;
next();
});
};

/**
Expand All @@ -42,7 +39,7 @@ exports.hasAuthorization = function(roles) {

return function(req, res, next) {
_this.requiresLogin(req, res, function() {
if (_.intersection(req.user.roles, roles).length) {
if (req.user.hasRoles(roles)) {
return next();
} else {
return res.status(403).send({
Expand Down
Loading