Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Admins have access to any permission they can assign.
adminPermissions = permissions
.filter(function(p) {
return p.assignScope.indexOf(USER_ROLES.ADMIN) > -1
})
.map(function(p) {
return {
state: PERMISSION_STATES.INCLUDED,
childId: p._id
}
})
// Initial Admin role permissions
promises.push(
RestHapi.addMany(
models.role,
roles[1]._id,
models.permission,
'permissions',
adminPermissions,
Log
)
)
await Promise.all(promises)
promises = []
// Initial Super Admin role permissions
promises.push(
RestHapi.addMany(
models.role,
childId: p._id
}
})
let userPermissions = userBasePermissionNames.map(function(permissionName) {
return {
state: PERMISSION_STATES.INCLUDED,
childId: permissions.find(function (p) {
return p.name === permissionName
})._id
}
}).concat(userDocumentPermissions).concat(userImagePermissions)
// EXPL: initial User role permissions
promises.push(RestHapi.addMany(models.role, roles[0]._id, models.permission, 'permissions', userPermissions, Log))
return Q.all(promises)
.then(function (result) {
promises = []
// EXPL: Admins have access to any permission they can assign.
adminPermissions = permissions.filter(function(p) {
return p.assignScope.indexOf(USER_ROLES.ADMIN) > -1
}).map(function(p) {
return {
state: PERMISSION_STATES.INCLUDED,
childId: p._id
}
})
// EXPL: Initial Admin role permissions
.then(function (result) {
promises = []
// EXPL: Admins have access to any permission they can assign.
adminPermissions = permissions.filter(function(p) {
return p.assignScope.indexOf(USER_ROLES.ADMIN) > -1
}).map(function(p) {
return {
state: PERMISSION_STATES.INCLUDED,
childId: p._id
}
})
// EXPL: Initial Admin role permissions
promises.push(RestHapi.addMany(models.role, roles[1]._id, models.permission, 'permissions', adminPermissions, Log))
return Q.all(promises)
})
.then(function (result) {
.filter(function(p) {
// We start with permissions Admins can assign so that they will also be able to assign the group
return p.assignScope.indexOf(USER_ROLES.ADMIN) > -1
})
.filter(function(p) {
return p.name.includes('create')
})
.map(function(p) {
return {
state: PERMISSION_STATES.FORBIDDEN,
childId: p._id
}
})
promises.push(
RestHapi.addMany(
models.group,
groups[1]._id,
models.permission,
'permissions',
createForbiddenPermission,
Log
)
)
await Promise.all(promises)
promises = []
// Super User group permissions
let includedPermissions = permissionNames
.filter(function(permissionName) {
return permissionName !== 'root'
RestHapi.addMany(
models.role,
roles[1]._id,
models.permission,
'permissions',
adminPermissions,
Log
)
)
await Promise.all(promises)
promises = []
// Initial Super Admin role permissions
promises.push(
RestHapi.addMany(
models.role,
roles[2]._id,
models.permission,
'permissions',
[
{
state: PERMISSION_STATES.INCLUDED,
childId: permissions.find(function(p) {
return p.name === 'root'
})._id
}
],
Log
)
)
// Super User group permissions
let includedPermissions = permissionNames
.filter(function(permissionName) {
return permissionName !== 'root'
})
.map(function(permissionName) {
return {
state: PERMISSION_STATES.INCLUDED,
childId: permissions.find(function(p) {
return p.name === permissionName
})._id
}
})
promises.push(
RestHapi.addMany(
models.group,
groups[2]._id,
models.permission,
'permissions',
includedPermissions,
Log
)
)
await Promise.all(promises)
promises = []
// Assign groups to users
promises.push(
RestHapi.addMany(
models.user,
promises = []
//EXPL: Read Only group permissions
let readOnlyExcludedPermissions = permissions.filter(function(p) {
//EXPL: We start with permissions Admins can assign so that they will also be able to assign the group
return p.assignScope.indexOf(USER_ROLES.ADMIN) > -1
}).filter(function(p) {
return !(p.name.includes('read') || p.name.includes ('get'))
}).map(function(p) {
return {
state: PERMISSION_STATES.EXCLUDED,
childId: p._id
}
})
promises.push(RestHapi.addMany(models.group, groups[0]._id, models.permission, 'permissions', readOnlyExcludedPermissions, Log))
return Q.all(promises)
})
.then(function (result) {
promises = []
//EXPL: Editor group permissions
let createForbiddenPermission = permissions.filter(function(p) {
//EXPL: We start with permissions Admins can assign so that they will also be able to assign the group
return p.assignScope.indexOf(USER_ROLES.ADMIN) > -1
}).filter(function(p) {
return p.name.includes('create')
}).map(function(p) {
return {
state: PERMISSION_STATES.FORBIDDEN,
childId: p._id
}
})
promises.push(RestHapi.addMany(models.group, groups[1]._id, models.permission, 'permissions', createForbiddenPermission, Log))
return Q.all(promises)
})
.then(function (result) {
.then(function (result) {
promises = []
//EXPL: Super User group permissions
let includedPermissions = permissionNames.filter(function(permissionName) {
return permissionName !== 'root'
}).map(function(permissionName) {
return {
state: PERMISSION_STATES.INCLUDED,
childId: permissions.find(function (p) {
return p.name === permissionName
})._id
}
})
promises.push(RestHapi.addMany(models.group, groups[2]._id, models.permission, 'permissions', includedPermissions, Log))
return Q.all(promises)
})
.then(function (result) {
.then(function (result) {
promises = []
//EXPL: Assign groups to users
promises.push(RestHapi.addMany(models.user, users[1]._id, models.group, 'groups', [groups[0]._id], Log))
promises.push(RestHapi.addMany(models.user, users[3]._id, models.group, 'groups', [groups[0]._id], Log))
promises.push(RestHapi.addMany(models.user, users[4]._id, models.group, 'groups', [groups[1]._id], Log))
promises.push(RestHapi.addMany(models.user, users[5]._id, models.group, 'groups', [groups[2]._id], Log))
return Q.all(promises)
})
})