I would like to prevent Drupal logins if the SAML response contains a certain attribute AND the Drupal user does not have a certain role. I've tried a couple of approaches and have not had any success. Would anyone be able to describe the recommended way to accomplish this?

Comments

brockfanning created an issue.

roderik’s picture

I'm not sure what you need, so here's my response, in the form of a possible first stab in the direction of what you want, so you can maybe specify what goes wrong there.

I'd probably

  • Copy UserRolesEventSubscriber into a custom module (rename, add to services.yml)
  • Remove basically all the code from onUserSync() except the $account = $event->getAccount(); and ($attributes = )$event->getAttributes() lines
  • Build your own code from there.
  • EDIT: Throw an exception when finding your forbidden roles, to deny login. (Plain \RuntimeException if you want the users to see just a generic message; UserVisibleException if you want the exception message to be shown to the user.)

Note: