Skip to content

Commit

Permalink
- add admin support to change user role
Browse files Browse the repository at this point in the history
  • Loading branch information
wholespace214 committed Apr 6, 2022
1 parent ce3d292 commit 14234ca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
22 changes: 22 additions & 0 deletions controllers/users-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,27 @@ const banUser = async (req, res, next) => {
}
};

const updateRole = async (req, res, next) => {
if (!req.user || !req.user.admin) {
return next(new ErrorHandler(403, 'Action forbidden'));
}

const errors = validationResult(req);
if (!errors.isEmpty()) {
return next(new ErrorHandler(400, errors));
}

const { role } = req.body;

try {
await userService.changeUserRole(req.params.userId, role);
return res.status(204).send();
} catch (e) {
console.error(e.message);
return next(new ErrorHandler(500, 'Failed to change user role'));
}
}

const getUserPromoCodes = async (req, res, next) => {
try {
const statuses = req.query?.statuses?.split(',');
Expand Down Expand Up @@ -798,6 +819,7 @@ exports.buyWithFiat = buyWithFiat;
exports.cryptoPayChannel = cryptoPayChannel;
exports.updateUserConsent = updateUserConsent;
exports.banUser = banUser;
exports.updateRole = updateRole;
exports.generateMoonpayUrl = generateMoonpayUrl;
exports.getUserPromoCodes = getUserPromoCodes;
exports.claimPromoCode = claimPromoCode;
Expand Down
6 changes: 6 additions & 0 deletions routes/users/secure-users-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ router.post(
userController.banUser
);

router.post(
'/:userId/update-role',
[check('role').notEmpty()],
userController.updateRole
)

router.get('/promo-codes/all', userController.getUserPromoCodes);

router.post(
Expand Down
6 changes: 6 additions & 0 deletions services/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,12 @@ exports.updateBanDeadline = async (userId, duration = 0, description = null) =>
return user.save();
};

exports.changeUserRole = async (userId, role) => {
const user = await User.findById(userId);
user.admin = role === 'admin';
user.save();
};

exports.searchUsers = async (limit, skip, search, sortField, sortOrder, account) => {
if (account) {
const acc = await new Account().getUserLink(account);
Expand Down

0 comments on commit 14234ca

Please sign in to comment.