Skip to content

Commit

Permalink
Merge pull request #46 from chidimo/ch-release-v1
Browse files Browse the repository at this point in the history
#166231672 Fix flawed logic in user verification controller.
  • Loading branch information
chidimo committed May 24, 2019
2 parents 344dd04 + 4084e45 commit 0008be0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ exclude_patterns:
- test/
- public/
- dist/
- legacyUI/
- UI/
18 changes: 16 additions & 2 deletions controllers/UsersController.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ const UsersController = {
const { id } = req.params;
const clause = `WHERE id=${req.params.id}`;
const column = 'mailverified=true';
update_if_exists(users_model, id, column, clause, res);
const user = await update_if_exists(
users_model, id, column, clause, res);
if (user) {
return res.status(200).json({ data: user });
}
return res.status(404)
.json({ error: `User with id ${id} not found` });
}
catch (e) { return; }
},
Expand All @@ -95,8 +101,15 @@ const UsersController = {
const { id } = req.params;
const clause = `WHERE id=${id}`;
const column = 'status=\'verified\'';
// check requesting user's isAdmin status
try {
update_if_exists(users_model, id, column, clause, res);
const user = await update_if_exists(
users_model, id, column, clause, res);
if (user) {
return res.status(200).json({ data: user });
}
return res.status(404)
.json({ error: `User with id ${id} not found` });
}
catch (e) { return; }
},
Expand All @@ -122,6 +135,7 @@ const UsersController = {
const { id } = req.params;
const { firstname, lastname, phone, home, office } = req.body;
const clause = `WHERE id=${id}`;
// check requesting user's identity
try {

const exists = await check_user_exists(users_model, clause, res);
Expand Down
5 changes: 2 additions & 3 deletions controllers/helpers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@ export const update_if_exists = async (model_instance,
if (exists) {
await model_instance.update(column, clause);
const user = await get_existing_user(model_instance, res, clause);
return res.status(200).json({ data: user });
return user;
}
return res.status(404)
.json({ error: `User with id ${id} not found` });
return false;
}
catch (e) { return; }
};
Expand Down
23 changes: 15 additions & 8 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,45 @@ router.post('/auth/signin',
AuthController.signin
);

router.patch('/users/:id/verify',
AuthenticationMiddleware.verifyToken,
UsersController.verify_user
);
router.get('/users/:id/account-confirmation',
AuthenticationMiddleware.verifyToken,
UsersController.confirm_account
);
router.get('/users',
AuthenticationMiddleware.verifyToken,
UsersController.get_users
);

router.get('/users/:id',
AuthenticationMiddleware.verifyToken,
UsersController.get_user
);

router.patch('/users/:id/verify',
AuthenticationMiddleware.verifyToken,
UsersController.verify_user
);
router.get('/users/:id/account-confirmation',
AuthenticationMiddleware.verifyToken,
UsersController.confirm_account
);

router.get('/users?status=verified',
AuthenticationMiddleware.verifyToken,
UsersController.get_users
);

router.patch('/users/:id/update',
AuthenticationMiddleware.verifyToken,
UsersValidators.updateProfileValidator,
UsersController.update_user_profile
);
router.get('/users/:id/photo/upload/',
AuthenticationMiddleware.verifyToken,
UsersController.get_aws_signed_url
);

router.patch('/users/:id/photo/update',
AuthenticationMiddleware.verifyToken,
UsersController.update_photo_url
);

router.post('/users/:email/reset_password',
UsersValidators.newPasswordValidator,
UsersController.reset_password
Expand Down

0 comments on commit 0008be0

Please sign in to comment.