Skip to content

Commit

Permalink
Merge pull request #234 from ministryofjustice/permissions/prison-role
Browse files Browse the repository at this point in the history
[P4-648] Set correct app permissions for Prison users
  • Loading branch information
teneightfive authored Sep 10, 2019
2 parents 79caf8a + 5c0d79f commit c135174
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
26 changes: 17 additions & 9 deletions common/lib/user.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { uniq } = require('lodash')

function User({ fullname, roles = [], locations = [] } = {}) {
this.fullname = fullname
this.permissions = this.getPermissions(roles)
Expand All @@ -10,21 +12,27 @@ User.prototype = {

if (roles.includes('ROLE_PECS_POLICE')) {
permissions.push(
...[
'moves:view:by_location',
'moves:download:by_location',
'move:view',
'move:create',
'move:cancel',
]
'moves:view:by_location',
'moves:download:by_location',
'move:view',
'move:create',
'move:cancel'
)
}

if (roles.includes('ROLE_PECS_PRISON')) {
permissions.push(
'moves:view:by_location',
'moves:download:by_location',
'move:view'
)
}

if (roles.includes('ROLE_PECS_SUPPLIER')) {
permissions.push(...['moves:view:all', 'moves:download:all'])
permissions.push('moves:view:all', 'moves:download:all')
}

return permissions
return uniq(permissions)
},
}

Expand Down
58 changes: 35 additions & 23 deletions common/lib/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ describe('User class', function() {
})
})

context('when user has ROLE_PECS_PRISON', function() {
beforeEach(function() {
permissions = user.getPermissions(['ROLE_PECS_PRISON'])
})

it('should contain correct permission', function() {
expect(permissions).to.deep.equal([
'moves:view:by_location',
'moves:download:by_location',
'move:view',
])
})
})

context('when user has ROLE_PECS_SUPPLIER role', function() {
beforeEach(function() {
permissions = user.getPermissions(['ROLE_PECS_SUPPLIER'])
Expand All @@ -110,28 +124,26 @@ describe('User class', function() {
})
})

context(
'when user has both ROLE_PECS_POLICE and ROLE_PECS_SUPPLIER roles',
function() {
beforeEach(function() {
permissions = user.getPermissions([
'ROLE_PECS_POLICE',
'ROLE_PECS_SUPPLIER',
])
})

it('should contain correct permission', function() {
expect(permissions).to.deep.equal([
'moves:view:by_location',
'moves:download:by_location',
'move:view',
'move:create',
'move:cancel',
'moves:view:all',
'moves:download:all',
])
})
}
)
context('when user has all roles', function() {
beforeEach(function() {
permissions = user.getPermissions([
'ROLE_PECS_POLICE',
'ROLE_PECS_PRISON',
'ROLE_PECS_SUPPLIER',
])
})

it('should contain correct permission', function() {
expect(permissions).to.deep.equal([
'moves:view:by_location',
'moves:download:by_location',
'move:view',
'move:create',
'move:cancel',
'moves:view:all',
'moves:download:all',
])
})
})
})
})

0 comments on commit c135174

Please sign in to comment.