Skip to content

Commit dcaa76b

Browse files
authored
Merge pull request #5921 from FlowFuse/device-access-to-ff-tables
Allow Devices to get FF Tables creds
2 parents 2448555 + d665857 commit dcaa76b

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

forge/ee/routes/tables/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ module.exports = async function (app) {
4141
if (project.Team.hashid !== request.team.hashid) {
4242
return reply.status(401).send({ code: 'unauthorized', error: 'unauthorized' })
4343
}
44+
} else if (request.session.ownerType === 'device') {
45+
const device = await app.db.models.Device.byId(parseInt(request.session.ownerId))
46+
if (!device || device.Team.hashid !== request.team.hashid) {
47+
return reply.status(401).send({ code: 'unauthorized', error: 'unauthorized' })
48+
}
4449
} else {
4550
await app.needsPermission('team:database:list')(request, reply, done)
4651
}

test/unit/forge/ee/routes/tables/index_spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ describe('Tables API', function () {
3232
TestObjects.team = app.team
3333
TestObjects.application = app.application
3434
TestObjects.instance = app.instance
35+
TestObjects.device = await app.factory.createDevice({
36+
name: 'device1',
37+
mode: 'developer'
38+
}, app.team, app.instance)
3539

3640
TestObjects.alice = await app.db.models.User.byUsername('alice')
3741
await login('alice', 'aaPassword')
@@ -121,6 +125,38 @@ describe('Tables API', function () {
121125
dbs[0].should.have.property('name', TestObjects.team.hashid)
122126
})
123127

128+
it('Get Team database list as Instance', async function () {
129+
const projectTokens = await app.instance.refreshAuthTokens()
130+
const response = await app.inject({
131+
method: 'GET',
132+
url: `/api/v1/teams/${TestObjects.team.hashid}/databases`,
133+
headers: {
134+
Authorization: `Bearer ${projectTokens.token}`
135+
}
136+
})
137+
response.statusCode.should.equal(200)
138+
const dbs = response.json()
139+
dbs.should.be.an.Array().and.have.length(1)
140+
dbs[0].should.have.property('id')
141+
dbs[0].should.have.property('name', TestObjects.team.hashid)
142+
})
143+
144+
it('Get Team database list as Device', async function () {
145+
const deviceToken = await app.db.controllers.AccessToken.createTokenForDevice(TestObjects.device)
146+
const response = await app.inject({
147+
method: 'GET',
148+
url: `/api/v1/teams/${TestObjects.team.hashid}/databases`,
149+
headers: {
150+
Authorization: `Bearer ${deviceToken.token}`
151+
}
152+
})
153+
response.statusCode.should.equal(200)
154+
const dbs = response.json()
155+
dbs.should.be.an.Array().and.have.length(1)
156+
dbs[0].should.have.property('id')
157+
dbs[0].should.have.property('name', TestObjects.team.hashid)
158+
})
159+
124160
it('Get Team database by ID', async function () {
125161
const db = (await app.db.models.Table.byTeamId(TestObjects.team.id))[0]
126162
const response = await app.inject({

0 commit comments

Comments
 (0)