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

Commit

Permalink
BUG: fix admin access (blocking user PUT)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhutchison committed Jul 28, 2015
1 parent 460ef53 commit 839f805
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
6 changes: 6 additions & 0 deletions modules/users/server/controllers/admin.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ exports.list = function (req, res) {
* User middleware
*/
exports.userByID = function (req, res, next, id) {
if (!mongoose.Types.ObjectId.isValid(id)) {
return res.status(400).send({
message: 'User is invalid'
});
}

User.findById(id, '-salt -password').exec(function (err, user) {
if (err) return next(err);
if (!user) return next(new Error('Failed to load user ' + id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ var _ = require('lodash'),
/**
* User middleware
*/
exports.userByID = function(req, res, next, id) {
exports.userByID = function (req, res, next, id) {
if (!mongoose.Types.ObjectId.isValid(id)) {
return res.status(400).send({
message: 'User is invalid'
});
}

User.findOne({
_id: id
}).exec(function(err, user) {
}).exec(function (err, user) {
if (err) return next(err);
if (!user) return next(new Error('Failed to load User ' + id));
req.profile = user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var acl = require('acl');
acl = new acl(new acl.memoryBackend());

/**
* Invoke Articles Permissions
* Invoke Admin Permissions
*/
exports.invokeRolesPolicies = function () {
acl.allow([{
Expand Down
17 changes: 10 additions & 7 deletions modules/users/server/routes/admin.server.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
/**
* Module dependencies.
*/
var adminPolicy = require('../policies/admin.server.policies'),
var adminPolicy = require('../policies/admin.server.policy'),
admin = require('../controllers/admin.server.controller');

module.exports = function (app) {
// User route registration first. Ref: #713
require('./users.server.routes.js')(app);

// Users collection routes
app.route('/api/users').all(adminPolicy.isAllowed)
.get(admin.list);
app.route('/api/users')
.get(adminPolicy.isAllowed, admin.list);

// Single user routes
app.route('/api/users/:userId').all(adminPolicy.isAllowed)
.get(admin.read)
.put(admin.update)
.delete(admin.delete);
app.route('/api/users/:userId')
.get(adminPolicy.isAllowed, admin.read)
.put(adminPolicy.isAllowed, admin.update)
.delete(adminPolicy.isAllowed, admin.delete);

// Finish by binding the user middleware
app.param('userId', admin.userByID);
Expand Down
2 changes: 1 addition & 1 deletion modules/users/server/routes/users.server.routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

module.exports = function(app) {
module.exports = function (app) {
// User Routes
var users = require('../controllers/users.server.controller');

Expand Down

0 comments on commit 839f805

Please sign in to comment.