function(ctx, callback) {
if (ctx.payload.action === 'delete:user') {
return callback(new Error('You are not allowed to delete users.'));
}
// Get the department from the current user's metadata.
var department = ctx.request.user.app_metadata && ctx.request.user.app_metadata.department;
if (!department || !department.length) {
return callback(new Error('The current user is not part of any department.'));
}
// The IT department can access all users.
if (department === 'IT') {
return callback();
}
ctx.log('Verifying access:', ctx.payload.user.app_metadata.department, department);
if (!ctx.payload.user.app_metadata.department || ctx.payload.user.app_metadata.department !== department) {
return callback(new Error('You can only access users within your own department.'));
}
return callback();
}