Skip to content

Instantly share code, notes, and snippets.

@Tanver-Hasan
Created August 24, 2021 11:07
Show Gist options
  • Save Tanver-Hasan/a4c3ee6341c12950c835fc41fe043fc2 to your computer and use it in GitHub Desktop.
Save Tanver-Hasan/a4c3ee6341c12950c835fc41fe043fc2 to your computer and use it in GitHub Desktop.
function (user, context, callback) {
    console.log("executing rule ");
    if (context.clientID === 'ULw7vqL2WK41DIwc2r4QxP78bISpQ2pN') {
       console.log("Adding claim rule ");
        const namespace = 'https://example.com/auth0-delegated-admin';
        context.idToken[namespace] = {
            roles: (context.authorization || {}).roles
        };
    }
    console.log(context.idToken);
    callback(null, user, context);
}
@Tanver-Hasan
Copy link
Author

Filter Hook


function(ctx, callback) {
  // 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 see all users.
  if (department === 'IT') {
    return callback();
  }

  // Return the lucene query.
  return callback(null, 'app_metadata.Department:"' + department + '"');
}

@Tanver-Hasan
Copy link
Author

Access Hook

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();
}

@Tanver-Hasan
Copy link
Author

In the app_metadata, set the following field for department

"Department": "Informantion Management"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment