Skip to content

Commit

Permalink
GQL-61: As a user, I can read and update provider level group permiss…
Browse files Browse the repository at this point in the history
…ions (#131)

* GQL-61: Updates the groups acl to check if a user has provider/system permission to update and read

* GQL-61: Adds test for permissions acl
  • Loading branch information
dmistry1 authored and jmaeng72 committed Aug 8, 2024
1 parent eb4ec41 commit 25ff41c
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 32 deletions.
9 changes: 6 additions & 3 deletions src/permissions/__tests__/permissions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { race, shield } from 'graphql-shield'
// eslint-disable-next-line no-unused-vars
import permissions from '../index'

import { canReadSystemGroups } from '../acls/canReadSystemGroups'
import { canCreateProviderGroups } from '../acls/canCreateProviderGroups'
import { canCreateSystemGroups } from '../acls/canCreateSystemGroups'
import { canReadSystemGroups } from '../acls/canReadSystemGroups'

import { isLocalMMT } from '../rules/isLocalMMT'

Expand Down Expand Up @@ -35,15 +36,17 @@ describe('permissions', () => {
Mutation: {
createGroup: race(
isLocalMMT,
canCreateSystemGroups
canCreateProviderGroups,
canCreateSystemGroups,
),
deleteGroup: race(
isLocalMMT,
canCreateSystemGroups
),
updateGroup: race(
isLocalMMT,
canCreateSystemGroups
canCreateProviderGroups,
canCreateSystemGroups,
)
}
})
Expand Down
53 changes: 53 additions & 0 deletions src/permissions/acls/__tests__/canCreateProviderGroups.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { canCreateProviderGroups } from '../canCreateProviderGroups'

import * as hasPermission from '../../../utils/hasPermission'
import { forbiddenError } from '../../../utils/forbiddenError'

describe('canCreateProviderGroups', () => {
test('when a tag is provided and the user has access to provider permission', async () => {
vi.spyOn(hasPermission, 'hasPermission').mockResolvedValue(true)

const result = await canCreateProviderGroups.resolve(
null,
{
tag: 'MOCK-PROVIDER'
},
{
edlUsername: 'test-user'
}
)

expect(result).toEqual(true)
})

test('throws a ForbiddenError if the user does not have permission', async () => {
vi.spyOn(hasPermission, 'hasPermission').mockResolvedValue(false)

const result = await canCreateProviderGroups.resolve(
null,
{
tag: 'MOCK-PROVIDER'
},
{
edlUsername: 'test-user'
}
)

expect(result).toEqual(forbiddenError('Not authorized to perform [create] on provider object [GROUP]'))
})

test('when a tag is not provided and the user does not have permission', async () => {
vi.spyOn(hasPermission, 'hasPermission').mockResolvedValue(false)

const result = await canCreateProviderGroups.resolve(
null,
{
tag: 'CMR'
},
{
edlUsername: 'test-user'
}
)
expect(result).toEqual(false)
})
})
19 changes: 18 additions & 1 deletion src/permissions/acls/__tests__/canCreateSystemGroups.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,29 @@ describe('canCreateSystemGroups', () => {

const result = await canCreateSystemGroups.resolve(
null,
{},
{
tag: 'CMR'
},
{
edlUsername: 'test-user'
}
)

expect(result).toEqual(forbiddenError('Not authorized to perform [create] on system object [GROUP]'))
})

test('returns true if the tag is CMR and the user has system permission', async () => {
vi.spyOn(hasPermission, 'hasPermission').mockResolvedValue(true)

const result = await canCreateSystemGroups.resolve(
null,
{
tag: 'CMR'
},
{
edlUsername: 'test-user'
}
)
expect(result).toEqual(true)
})
})
26 changes: 24 additions & 2 deletions src/permissions/acls/__tests__/canReadSystemGroups.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ describe('canReadSystemGroups', () => {

const result = await canReadSystemGroups.resolve(
null,
{},
{
params: {
tags: ['CMR']
}
},
{
edlUsername: 'test-user'
}
Expand All @@ -23,12 +27,30 @@ describe('canReadSystemGroups', () => {

const result = await canReadSystemGroups.resolve(
null,
{},
{
params: {
tags: ['CMR']
}
},
{
edlUsername: 'test-user'
}
)

expect(result).toEqual(forbiddenError('Not authorized to perform [read] on system object [GROUP]'))
})

test('returns true if no tags are provided', async () => {
vi.spyOn(hasPermission, 'hasPermission').mockResolvedValue(true)

const result = await canReadSystemGroups.resolve(
null,
{},
{
edlUsername: 'test-user'
}
)

expect(result).toEqual(true)
})
})
38 changes: 38 additions & 0 deletions src/permissions/acls/canCreateProviderGroups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { rule } from 'graphql-shield'

import { hasPermission } from '../../utils/hasPermission'
import { forbiddenError } from '../../utils/forbiddenError'

/**
* Check to see if the user can create provider groups using
* the cmr permissions api. In order to create provider groups, the user must have the `create`
* permission on the GROUP provider_object.
* @method
* @return {(true|ForbiddenError)}
*/
export const canCreateProviderGroups = rule()(async (parent, params, context) => {
const { edlUsername } = context

const { tag } = params

// If tag, perform check to see if the user has access to the given provider.
if (tag && tag !== 'CMR') {
if (
await hasPermission(
context,
{
permissions: 'create',
permissionOptions: {
provider: tag,
target: 'GROUP',
user_id: edlUsername
}
}
)
) return true

return forbiddenError('Not authorized to perform [create] on provider object [GROUP]')
}

return false
})
31 changes: 19 additions & 12 deletions src/permissions/acls/canCreateSystemGroups.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,25 @@ import { forbiddenError } from '../../utils/forbiddenError'
export const canCreateSystemGroups = rule()(async (parent, params, context) => {
const { edlUsername } = context

if (
await hasPermission(
context,
{
permissions: 'create',
permissionOptions: {
user_id: edlUsername,
system_object: 'GROUP'
const { tag } = params

// If the tag is CMR, perform check to see if the user has access to system group.
if (tag === 'CMR') {
if (
await hasPermission(
context,
{
permissions: 'create',
permissionOptions: {
user_id: edlUsername,
system_object: 'GROUP'
}
}
}
)
) return true
)
) return true

return forbiddenError('Not authorized to perform [create] on system object [GROUP]')
}

return forbiddenError('Not authorized to perform [create] on system object [GROUP]')
return true
})
35 changes: 23 additions & 12 deletions src/permissions/acls/canReadSystemGroups.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,29 @@ import { forbiddenError } from '../../utils/forbiddenError'
export const canReadSystemGroups = rule()(async (parent, params, context) => {
const { edlUsername } = context

if (
await hasPermission(
context,
{
permissions: 'read',
permissionOptions: {
user_id: edlUsername,
system_object: 'GROUP'
const { params: requestedParams = {} } = params

const { tags } = requestedParams

const isSystemPermission = tags?.includes('CMR')

// If the tags includes CMR (SYSTEM) check if the user has access to read system groups
if (isSystemPermission) {
if (
await hasPermission(
context,
{
permissions: 'read',
permissionOptions: {
user_id: edlUsername,
system_object: 'GROUP'
}
}
}
)
) return true
)
) return true

return forbiddenError('Not authorized to perform [read] on system object [GROUP]')
}

return forbiddenError('Not authorized to perform [read] on system object [GROUP]')
return true
})
7 changes: 5 additions & 2 deletions src/permissions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { canReadSystemGroups } from './acls/canReadSystemGroups'
import { canCreateSystemGroups } from './acls/canCreateSystemGroups'

import { isLocalMMT } from './rules/isLocalMMT'
import { canCreateProviderGroups } from './acls/canCreateProviderGroups'

const permissions = shield(
{
Expand All @@ -26,15 +27,17 @@ const permissions = shield(
Mutation: {
createGroup: race(
isLocalMMT,
canCreateSystemGroups
canCreateProviderGroups,
canCreateSystemGroups,
),
deleteGroup: race(
isLocalMMT,
canCreateSystemGroups
),
updateGroup: race(
isLocalMMT,
canCreateSystemGroups
canCreateProviderGroups,
canCreateSystemGroups,
)
}
},
Expand Down

0 comments on commit 25ff41c

Please sign in to comment.