Skip to content
Merged
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
6 changes: 6 additions & 0 deletions src/services/ResourceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ async function getRestrictedRoleIds () {
* @param {Array} resources resources of current user for specified challenge id
*/
async function checkAccess (currentUserResources) {
const copilotRoleIds = await getCopilotResourceRoleIds()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ performance]
Consider caching the result of getCopilotResourceRoleIds() if it is expected to be called frequently. This could improve performance by reducing database queries.

const hasCopilotRole = _.some(currentUserResources, r => copilotRoleIds.includes(r.roleId))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[💡 performance]
The use of _.some with includes is correct, but ensure that currentUserResources and copilotRoleIds are not excessively large, as this could impact performance. If they are, consider optimizing the data structure or approach.

if (hasCopilotRole) {
return
}

const list = await prisma.resourceRole.findMany({})
const fullAccessRoles = []
_.each(list, e => {
Expand Down
34 changes: 34 additions & 0 deletions test/unit/createResource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { v4: uuid } = require('uuid')
const service = require('../../src/services/ResourceService')
const ResourceRolePhaseDependencyService = require('../../src/services/ResourceRolePhaseDependencyService')
const prisma = require('../../src/common/prisma').getClient()
const helper = require('../../src/common/helper')
const ResourceRoleService = require('../../src/services/ResourceRoleService')
const { requestBody, user } = require('../common/testData')
const { assertValidationError, assertError, assertResource, getRoleIds, clearDependencies } = require('../common/testHelper')
Expand Down Expand Up @@ -264,6 +265,39 @@ module.exports = describe('Create resource', () => {
await assertResource(ret.id, ret)
})

it('copilot can manage resources without full access flags', async () => {
const originalRole = await helper.getById('ResourceRole', copilotRoleId)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ correctness]
The test case modifies the ResourceRole to have no full access flags and then attempts to create a resource with a user that might not have the necessary permissions. Ensure that the test setup accurately reflects the intended permissions and that the test case is valid under the new role configuration.

await ResourceRoleService.updateResourceRole(user.admin, copilotRoleId, {
name: originalRole.name,
fullReadAccess: false,
fullWriteAccess: false,
isActive: originalRole.isActive,
selfObtainable: originalRole.selfObtainable
})

const entity = resources.createBody('diazz', reviewerRoleId, challengeId2)
let createdResource
try {
createdResource = await service.createResource(user.phead, entity)
should.equal(createdResource.roleId, entity.roleId)
should.equal(createdResource.memberHandle.toLowerCase(), entity.memberHandle.toLowerCase())
await assertResource(createdResource.id, createdResource)
} finally {
if (createdResource && createdResource.id) {
await prisma.resource.deleteMany({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[💡 maintainability]
Consider using delete instead of deleteMany if you are certain that only one resource will be deleted. This can prevent accidental deletion of multiple resources if the query conditions are not as expected.

where: { id: createdResource.id }
})
}
await ResourceRoleService.updateResourceRole(user.admin, copilotRoleId, {
name: originalRole.name,
fullReadAccess: originalRole.fullReadAccess,
fullWriteAccess: originalRole.fullWriteAccess,
isActive: originalRole.isActive,
selfObtainable: originalRole.selfObtainable
})
}
})

it('create resource for user ghostar 1', async () => {
const entity = resources.createBody('ghostar', reviewerRoleId, challengeId2)
const ret = await service.createResource(user.m2m, entity)
Expand Down