-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
219 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module.exports = findAccount | ||
|
||
var findIdInRoles = require('../../utils/find-id-in-roles') | ||
var getAllAccounts = require('../../utils/account/get-all') | ||
|
||
function findAccount (state, options) { | ||
return new Promise(function (resolve, reject) { | ||
getAllAccounts({ | ||
couchUrl: state.url | ||
}, function (error, response) { | ||
if (error) { | ||
return reject(error) | ||
} | ||
|
||
resolve(response.rows.map(toAccount.bind(null, options))) | ||
}) | ||
}) | ||
} | ||
|
||
function toAccount (options, row) { | ||
var account = { | ||
username: row.doc.name, | ||
id: findIdInRoles(row.doc.roles) | ||
} | ||
|
||
if (options.include === 'profile') { | ||
account.profile = row.doc.profile | ||
} | ||
|
||
return account | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module.exports = accountRoutes | ||
module.exports.attributes = { | ||
name: 'account-routes-accounts' | ||
} | ||
|
||
var getApi = require('../api') | ||
var joiFailAction = require('../utils/joi-fail-action') | ||
var serialiseAccount = require('../utils/account/serialise') | ||
var toBearerToken = require('../utils/to-bearer-token') | ||
var validations = require('../utils/validations') | ||
|
||
function accountRoutes (server, options, next) { | ||
var couchUrl = options.couchdb.url | ||
var prefix = options.prefix || '' | ||
var api = getApi({ url: couchUrl }) | ||
var accounts = api.accounts | ||
var serialise = serialiseAccount.bind(null, { | ||
baseUrl: server.info.uri + prefix | ||
}) | ||
|
||
var getAccountsRoute = { | ||
method: 'GET', | ||
path: prefix + '/accounts', | ||
config: { | ||
auth: false, | ||
validate: { | ||
headers: validations.bearerTokenHeader, | ||
failAction: joiFailAction | ||
} | ||
}, | ||
handler: function (request, reply) { | ||
var sessionId = toBearerToken(request) | ||
|
||
return accounts.findAll({ | ||
bearerToken: sessionId, | ||
include: request.query.include | ||
}) | ||
|
||
.then(function (accounts) { | ||
return accounts.map(serialise) | ||
}) | ||
|
||
.then(reply) | ||
|
||
.catch(reply) | ||
} | ||
} | ||
|
||
server.route([ | ||
getAccountsRoute | ||
]) | ||
|
||
next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module.exports = getAllAccounts | ||
|
||
var Boom = require('boom') | ||
|
||
function getAllAccounts (options, callback) { | ||
var request = require('request').defaults({ | ||
json: true, | ||
baseUrl: options.couchUrl, | ||
timeout: 10000 // 10 seconds | ||
}) | ||
|
||
request.get({ | ||
url: '/_users/_all_docs?startkey=%22org.couchdb.user%3A%22&enkey=%22org.couchdb.user%3A%E9%A6%99%22', | ||
headers: { | ||
cookie: 'AuthSession=' + options.bearerToken | ||
} | ||
}, function (error, response, body) { | ||
if (error) { | ||
return callback(Boom.wrap(error)) | ||
} | ||
|
||
if (response.statusCode >= 400) { | ||
return callback(Boom.create(response.statusCode, fixErrorMessage(body.reason))) | ||
} | ||
callback(null, body) | ||
}) | ||
} | ||
|
||
function fixErrorMessage (message) { | ||
if (message === 'Only admins can access _all_docs of system databases.') { | ||
return 'Only admins can access /users' | ||
} | ||
|
||
return message | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module.exports = findCustomRoles | ||
|
||
function findCustomRoles (roles) { | ||
return roles.filter(isntInteralRole) | ||
} | ||
|
||
function isntInteralRole (role) { | ||
if (role === '_admin') { | ||
return false | ||
} | ||
|
||
if (role.substr(0, 3) === 'id:') { | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = hasAdminRole | ||
|
||
function hasAdminRole (roles) { | ||
return roles.indexOf('_admin') !== -1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.