Skip to content

Commit

Permalink
pencilblue#509 Updates from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
brianhyder committed Jul 5, 2015
1 parent d30f2ed commit ac1867c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 100 deletions.
8 changes: 4 additions & 4 deletions include/service/entities/user_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ module.exports = function(pb) {

var self = this;
var dao = new pb.DAO();
dao.loadById(userId, 'user', function(err, author){
dao.loadById(userId, TYPE, function(err, author){
if (util.isError(err)) {
return callback(err, null);
}
Expand Down Expand Up @@ -127,7 +127,7 @@ module.exports = function(pb) {
where: pb.DAO.getIdInWhere(Object.keys(authorIds))
};
var dao = new pb.DAO();
dao.q('user', opts, function(err, authors) {
dao.q(TYPE, opts, function(err, authors) {
if (util.isError(err)) {
return cb(err);
}
Expand Down Expand Up @@ -212,7 +212,7 @@ module.exports = function(pb) {
}
};
var dao = new pb.DAO();
dao.q('user', opts, function(err, data){
dao.q(TYPE, opts, function(err, data){
if (util.isError(err)) {
return cb(err, null);
}
Expand Down Expand Up @@ -411,7 +411,7 @@ module.exports = function(pb) {

//set exclusion. This would be if we are editing a user
if (ValidationService.isId(options.exclusionId, true)) {
where[DAO.getIdField()] = DAO.getNotIdField(id);
where[DAO.getIdField()] = DAO.getNotIdField(options.exclusionId);
}

var opts = {
Expand Down
112 changes: 56 additions & 56 deletions plugins/pencilblue/controllers/actions/admin/users/edit_user.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,78 +18,78 @@
module.exports = function(pb) {

//pb dependencies
var util = pb.util;
var util = pb.util;
var UserService = pb.UserService;
var BaseApiController = pb.BaseApiController;

/**
* Edits a user
* @class EditUser
* @constructor
* @extends
*/
function EditUser(){}
util.inherits(EditUser, pb.BaseController);
util.inherits(EditUser, BaseApiController);

/**
* Initializes the controller
* @method init
* @param {Object} context
* @param {Function} cb
*/
EditUser.prototype.init = function(context, cb) {
var self = this;
var init = function(err) {

/**
*
* @property service
* @type {UserService}
*/
self.service = new UserService(self.getServiceContext());

cb(err, true);
};
EditUser.super_.prototype.init.apply(this, [context, init]);
};

/**
* @method render
* @param {Function} cb
*/
EditUser.prototype.render = function(cb) {
var self = this;
var vars = this.pathVars;
var post = this.body || {};


this.getJSONPostParams(function(err, post) {
var message = self.hasRequiredParams(post, self.getRequiredFields());
if(message) {
cb({
code: 400,
content: pb.BaseController.apiResponse(pb.BaseController.API_ERROR, message)
});
return;
}
if(!pb.security.isAuthorized(self.session, {admin_level: post.admin})) {
return cb({
code: 400,
content: pb.BaseController.apiResponse(pb.BaseController.API_ERROR, self.ls.get('INSUFFICIENT_CREDENTIALS'))
});
}

if(!pb.security.isAuthorized(self.session, {admin_level: post.admin})) {
cb({
code: 400,
content: pb.BaseController.apiResponse(pb.BaseController.API_ERROR, self.ls.get('INSUFFICIENT_CREDENTIALS'))
});
return;
self.service.save(post, function(err, obj) {
if (util.isError(err)) {
return cb(err);
}

var dao = new pb.DAO();
dao.loadById(vars.id, 'user', function(err, user) {
if(util.isError(err) || user === null) {
cb({
code: 400,
content: pb.BaseController.apiResponse(pb.BaseController.API_ERROR, self.ls.get('INVALID_UID'))
});
return;
}

delete post[pb.DAO.getIdField()];
pb.DocumentCreator.update(post, user);

pb.users.isUserNameOrEmailTaken(user.username, user.email, vars.id, function(err, isTaken) {
if(util.isError(err) || isTaken) {
cb({
code: 400,
content: pb.BaseController.apiResponse(pb.BaseController.API_ERROR, self.ls.get('EXISTING_USERNAME'))
});
return;
}

dao.save(user, function(err, result) {
if(util.isError(err)) {
cb({
code: 500,
content: pb.BaseController.apiResponse(pb.BaseController.API_ERROR, self.ls.get('ERROR_SAVING'))
});
return;
}

cb({content: pb.BaseController.apiResponse(pb.BaseController.API_SUCCESS, self.ls.get('USER_EDITED'))});
});
});

//set the locale for the session the user being modified is the
//authenticated user
if (self.session.authentication.user_id === vars.id) {
self.session.locale = obj.locale;
}console.log(self.session.authentication.user_id);console.log(vars.id);

cb({
content: {
data: obj
},
code: 201
});
});
};

EditUser.prototype.getRequiredFields = function() {
return ['username', 'email', 'admin'];
};

//exports
return EditUser;
}
48 changes: 8 additions & 40 deletions plugins/pencilblue/controllers/actions/admin/users/new_user.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = function(pb) {
/**
*
* @property service
* @type {TopicService}
* @type {UserService}
*/
self.service = new UserService(self.getServiceContext());

Expand All @@ -49,50 +49,22 @@ module.exports = function(pb) {
NewUser.super_.prototype.init.apply(this, [context, init]);
};

/**
*
* @method render
* @param {Function} cb
*/
NewUser.prototype.render = function(cb) {
var self = this;

var post = this.body;
var message = self.hasRequiredParams(post, self.getRequiredFields());
if(message) {
cb({
code: 400,
content: pb.BaseController.apiResponse(pb.BaseController.API_ERROR, message)
});
return;
}
var post = this.body || {};

if(!pb.security.isAuthorized(self.session, {admin_level: post.admin})) {
cb({
return cb({
code: 400,
content: pb.BaseController.apiResponse(pb.BaseController.API_ERROR, self.ls.get('INSUFFICIENT_CREDENTIALS'))
});
return;
}

// var user = pb.DocumentCreator.create('user', post);
// pb.users.isUserNameOrEmailTaken(user.username, user.email, post.id, function(err, isTaken) {
// if(util.isError(err) || isTaken) {
// cb({
// code: 400,
// content: pb.BaseController.apiResponse(pb.BaseController.API_ERROR, self.ls.get('EXISTING_USERNAME'))
// });
// return;
// }
//
// var dao = new pb.DAO();
// dao.save(user, function(err, result) {
// if(util.isError(err)) {
// cb({
// code: 500,
// content: pb.BaseController.apiResponse(pb.BaseController.API_ERROR, self.ls.get('ERROR_SAVING'))
// });
// return;
// }
//
// cb({content: pb.BaseController.apiResponse(pb.BaseController.API_SUCCESS, self.ls.get('USER_CREATED'), result)});
// });
// });
self.service.save(post, function(err, obj) {
if (util.isError(err)) {
return cb(err);
Expand All @@ -107,10 +79,6 @@ module.exports = function(pb) {
});
};

NewUser.prototype.getRequiredFields = function() {
return ['username', 'email', 'password', 'confirm_password', 'admin'];
};

//exports
return NewUser;
};
2 changes: 2 additions & 0 deletions plugins/pencilblue/include/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,8 @@ module.exports = function Routes(pb){
auth_required: true,
access_level: pb.SecurityService.ACCESS_EDITOR,
controller: path.join(pb.config.docRoot, 'plugins', 'pencilblue', 'controllers', 'actions', 'admin', 'users', 'edit_user.js'),
content_type: 'application/json',
request_body: ['application/json']
},
{
method: 'delete',
Expand Down

0 comments on commit ac1867c

Please sign in to comment.