Skip to content

Commit

Permalink
fix(cloud-roles): improve roles fetch throttling
Browse files Browse the repository at this point in the history
and expose the roles with rules as part of the profile
  • Loading branch information
emirotin committed Jun 9, 2018
1 parent 704c4f5 commit 888e8c8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
18 changes: 11 additions & 7 deletions packages/core/botpress/src/cloud/index.js
Expand Up @@ -6,10 +6,10 @@ import _ from 'lodash'

module.exports = ({ projectLocation, botfile, logger }) => {
let certificate = null
let roles = null
let rolesPromise = null

setInterval(() => (certificate = null), ms('5 minutes'))
setInterval(() => (roles = null), ms('5 minutes'))
setInterval(() => (rolesPromise = null), ms('5 minutes'))

function _readCloudfile() {
const filePath = path.resolve(projectLocation, 'bp-cloud.json')
Expand Down Expand Up @@ -84,13 +84,13 @@ module.exports = ({ projectLocation, botfile, logger }) => {
return
}

if (roles) {
return roles
if (rolesPromise) {
return rolesPromise
}

const { endpoint, token, botId } = getPairingInfo()

roles = await axios
rolesPromise = axios
.get(`${endpoint}/api/bots/${botId}/roles`, {
headers: {
Authorization: `Bearer bot__${token}`
Expand All @@ -106,11 +106,15 @@ module.exports = ({ projectLocation, botfile, logger }) => {
return null
})

return roles
return rolesPromise
}

async function getUserRoles(roleNames) {
await _getRemoteRoles()
if (!isPaired()) {
return null
}

const roles = await _getRemoteRoles()
return roleNames.reduce((acc, roleName) => {
const role = _.find(roles, { name: roleName })
acc[roleName] = role && role.rules
Expand Down
3 changes: 2 additions & 1 deletion packages/core/botpress/src/server/non-secured.js
Expand Up @@ -24,6 +24,7 @@ module.exports = (bp, app) => {
})

app.get('/api/my-account', async (req, res) => {
res.send(req.user)
const roles = await bp.cloud.getUserRoles(req.user.roles)
res.send({ ...req.user, roles })
})
}

0 comments on commit 888e8c8

Please sign in to comment.