Skip to content
This repository has been archived by the owner on Jan 29, 2022. It is now read-only.

Commit

Permalink
Add npm command to change a user's role
Browse files Browse the repository at this point in the history
This avoids us having to connect directly to the DB to change a user's role.
Now we can simply run "heroku run npm run update_user_role foo@example.com
admin".
  • Loading branch information
vitorbaptista committed May 4, 2017
1 parent 32ce083 commit 2f65ff5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"rollback": "knex migrate:rollback",
"dev": "gulp dev",
"prestart": "gulp build",
"start": "node --optimize_for_size --max_old_space_size=460 --gc_interval=100 server.js"
"start": "node --optimize_for_size --max_old_space_size=460 --gc_interval=100 server.js",
"update_user_role": "node tools/update_user_role.js"
},
"repository": {
"type": "git",
Expand Down
38 changes: 38 additions & 0 deletions tools/update_user_role.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* eslint no-console: 0 */

'use strict';

const User = require('../models/user');

function updateUserRole(email, role) {
return User.where({ email })
.save({ role }, { patch: true, require: true });
}

if (require.main === module) {
const argv = process.argv;
if (argv.length < 3 || argv.length > 4) {
console.error(`Usage: node ${__filename} <email> [role]`);
process.exit(-1);
}

const email = argv[2];
const role = argv[3] || null;

updateUserRole(email, role)
.then(() => {
console.log(`Updated user "${email}" to role "${role}"`);
process.exit(0);
})
.catch((err) => {
if (err.message === 'No Rows Updated') {
console.error(`Could not find an user with email "${email}"`);
} else if (err.constraint === 'users_role_check') {
console.error(`Role "${role}" is invalid`);
} else {
console.error(err);
}

process.exit(-1);
});
}

0 comments on commit 2f65ff5

Please sign in to comment.