Skip to content

Commit

Permalink
WIP – create/delete user databases when accounts created/deleted
Browse files Browse the repository at this point in the history
* * *

This commit was sponsored by &yet.

Our friends at &yet have been supporters of Hoodie since its earliest days. <3
And they have availability for new projects! We think they’re some of the best
people you can hire anywhere, but you can read what other people have to say
about them and their work here:

https://andyet.com/case-studies/
  • Loading branch information
gr2m committed Dec 23, 2015
1 parent f5262be commit 4a43d5e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
58 changes: 54 additions & 4 deletions lib/core-modules-glue-code/user-databases.js
Expand Up @@ -3,13 +3,63 @@ module.exports = {
remove: removeUserDatabase
}

var async = require('async')
var log = require('npmlog')
var request = require('request')

function addUserDatabase (server, account) {
function addUserDatabase (config, server, account) {
log.info('Account created for %s (id: %s)', account.username, account.id)
log.error('user/%s not created: not yet implemented (/lib/core-modules-glue-code/user-databases.js)', account.id)

async.series([
createDatabase.bind(null, config, account),
createSecurity.bind(null, config, account)
], function (error) {
if (error) {
log.error('user/%s not created: %s', account.id, error)
return
}

log.info('Database user/%s created for %s', account.id, account.username)
})
}
function removeUserDatabase (server, account) {
function removeUserDatabase (config, server, account) {
log.info('Account removed for %s (id: %s)', account.username, account.id)
log.error('user/%s not deleted: not yet implemented (/lib/core-modules-glue-code/user-databases.js)', account.id)

deleteDatabase(config, account, function (error) {
if (error) {
log.error('user/%s not deleted: %s', account.id, error)
return
}

log.info('Database user/%s deleted for %s', account.id, account.username)
})
}

function createDatabase (config, account, callback) {
var url = config.dbUrl + '/user%2f' + account.id
request.put(url, callback)
}

function createSecurity (config, account, callback) {
var url = config.dbUrl + '/user%2f' + account.id + '/_security'
var security = {
admins: {
names: [],
roles: []
},
members: {
names: [],
roles: ['id:' + account.id]
}
}
request({
method: 'PUT',
url: url,
body: security
}, callback)
}

function deleteDatabase (config, account, callback) {
var url = config.dbUrl + '/user%2f' + account.id
request.del(url, callback)
}
4 changes: 2 additions & 2 deletions lib/index.js
Expand Up @@ -61,8 +61,8 @@ module.exports = function (options, callback) {

log.error('Databases not created/deleted for accounts: server.plugins.account.api.accounts.on not yet implemented')
server.plugins.account.api.accounts.on = function () {}
server.plugins.account.api.accounts.on('add', userDatabases.add.bind(null, server))
server.plugins.account.api.accounts.on('remove', userDatabases.remove.bind(null, server))
server.plugins.account.api.accounts.on('add', userDatabases.add.bind(null, config, server))
server.plugins.account.api.accounts.on('remove', userDatabases.remove.bind(null, config, server))

callback(null, server, config)
})
Expand Down

0 comments on commit 4a43d5e

Please sign in to comment.