Skip to content

Instantly share code, notes, and snippets.

@alanning
Last active May 19, 2016 12:20
Show Gist options
  • Save alanning/7aeac128f065e4571095 to your computer and use it in GitHub Desktop.
Save alanning/7aeac128f065e4571095 to your computer and use it in GitHub Desktop.
Rename existing user roles
// Rename user roles
//
// Usage:
// 1. Make sure your target mongo database is running and accessible.
// 2. At terminal command line:
// `$ mongo dbname rename-roles.js > out.txt`
//
// Configuration:
//
// BASIC
//
// Rename regular roles:
// crisis-response-team => emergency-checkin-notification
// Rename global roles:
// super-admin => acme-admin
//
// ADVANCED
//
// Split role into multiple roles:
// broadcast => broadcast-alert, broadcast-message
//
// NOTE: This script does not currently change the roles in the "roles" collection.
// Only the roles attached to users.
var UPDATE_DB = false,
regularRoles = [
// { pre:'basic', post: 'read' }
// { pre:'broadcast', post: ['broadcast-alert', 'broadcast-message'] }
],
globalRoles = [
// { pre:'law-enforcement', post:'public-safety' }
],
users,
user,
userCount = 0,
email,
roles,
globals,
shouldConvert,
corruptFields,
update,
total = 0,
i;
function _contains (collection, target) {
return -1 !== collection.indexOf(target)
}
print()
print('Renaming roles')
print()
print(' Regular Roles:', JSON.stringify(regularRoles, null, 2))
print(' Global Roles:', JSON.stringify(globalRoles, null, 2))
print()
print()
users = db.users.find({}, {emails:1, roles:1})
while (users.hasNext()) {
user = users.next()
userCount++
shouldConvert = false
corruptFields = []
if (!user || !user.emails || !user.emails.length) {
print('user ', user._id, ' missing email address')
continue
}
email = user.emails[0].address
if (!email) {
print('user ', user._id, ' missing email address')
continue
}
print('user', userCount + ": db.users.findOne({'emails.address':'" +
email + "'}, {services:0})")
print(' roles: ', JSON.stringify(user.roles))
if (!user.roles) {
print(' missing roles - skipping')
continue
}
for (role in user.roles) {
if ('__global_roles__' === role) {
// skip global roles for now
continue
}
roles = user.roles[role]
if (!roles.indexOf) {
corruptFields.push(role)
continue
}
regularRoles.forEach(function (rename) {
i = roles.indexOf(rename.pre)
if (-1 !== i) {
shouldConvert = true
if (Array.isArray(rename.post)) {
roles.splice(i, 1)
rename.post.forEach(function (newRole) {
if (!_contains(roles, newRole)) {
roles.push(newRole)
}
})
} else {
if (_contains(roles, rename.post)) {
// already have target role. just remove pre
roles.splice(i, 1)
} else {
roles.splice(i, 1, rename.post)
}
}
}
})
} // end regular user roles
globals = user.roles['__global_roles__']
if (globalRoles.length > 0 && globals) {
globalRoles.forEach(function (rename) {
i = globals.indexOf(rename.pre)
if (-1 !== i) {
shouldConvert = true
globals.splice(i, 1, rename.post)
}
})
}
// do update, if applicable
if (shouldConvert) {
total++
if (corruptFields.length > 0) {
print(' DATA CORRUPTION: removing corrupt fields: ',
JSON.stringify(corruptFields))
corruptFields.forEach(function (key) {
delete user.roles[key]
})
}
update = {$set: {roles: user.roles}}
print(' update: db.users.update({"emails.address":', email, '}, ',
JSON.stringify(update), ')')
if (UPDATE_DB) {
db.users.update({"emails.address": email}, update)
}
} else if (corruptFields.length > 0) {
// just remove the corrupt fields
update = {$unset: {}}
corruptFields.forEach(function (key) {
update.$unset['roles.'+key] = 1
})
print(' DATA CORRUPTION: removing corrupt fields')
print(' update: db.users.update({"emails.address":', email, '}, ',
JSON.stringify(update), ')')
if (UPDATE_DB) {
db.users.update({"emails.address": email}, update)
}
}
print()
} // end user loop
print()
print('total converted', total)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment