Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions forge/routes/api/teamMembers.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,15 @@ module.exports = async function (app) {
reply.code(400).send({ code: 'invalid_request', error: 'Invalid permissions object' })
return
}
const effectiveRole = (permissions, applicationId) => {
const override = permissions?.applications?.[applicationId]
return override !== undefined ? override : request.userRole.role
}
const lowered = applications.some(a => effectiveRole(newPermissions, a.hashid) < effectiveRole(request.userRole.permissions, a.hashid))
await app.db.controllers.Team.changeUserTeamPermissions(request.params.teamId, request.params.userId, newPermissions)
if (lowered) {
await app.db.controllers.StorageSession.removeUserFromTeamSessions(request.user, request.team)
}
// TODO: audit log it
reply.send({ status: 'okay' })
}
Expand Down
53 changes: 53 additions & 0 deletions test/unit/forge/routes/api/teamMembers_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,59 @@ describe('Team Members API', function () {
})
secondResponse.statusCode.should.equal(200)
})

describe('Editor sessions on permissions change', function () {
let revokeSpy

beforeEach(function () {
revokeSpy = sinon.spy(app.db.controllers.StorageSession, 'removeUserFromTeamSessions')
})

afterEach(function () {
sinon.restore()
})

async function setPermissions (applications) {
const response = await app.inject({
method: 'PUT',
url: `/api/v1/teams/${TestObjects.ATeam.hashid}/members/${TestObjects.chris.hashid}`,
cookies: { sid: TestObjects.tokens.bob },
payload: { permissions: { applications } }
})
response.statusCode.should.equal(200)
}

it('revokes editor sessions when an application role is lowered', async function () {
await setPermissions({ [TestObjects.application.hashid]: Roles.Viewer })
revokeSpy.calledOnce.should.be.true()
revokeSpy.firstCall.args[0].id.should.equal(TestObjects.chris.id)
revokeSpy.firstCall.args[1].id.should.equal(TestObjects.ATeam.id)
})

it('revokes editor sessions when an application role is set to none', async function () {
await setPermissions({ [TestObjects.application.hashid]: 0 })
revokeSpy.calledOnce.should.be.true()
})

it('does not revoke editor sessions when an application role is raised', async function () {
await setPermissions({ [TestObjects.application.hashid]: Roles.Owner })
revokeSpy.called.should.be.false()
})

it('revokes editor sessions when a higher override is removed', async function () {
await setPermissions({ [TestObjects.application.hashid]: Roles.Owner })
revokeSpy.resetHistory()
await setPermissions({})
revokeSpy.calledOnce.should.be.true()
})

it('does not revoke editor sessions when a lower override is removed', async function () {
await setPermissions({ [TestObjects.application.hashid]: Roles.Viewer })
revokeSpy.resetHistory()
await setPermissions({})
revokeSpy.called.should.be.false()
})
})
})

describe('PAT team scope cleanup on member removal', function () {
Expand Down
Loading