I'm having an issue with writing a redirect based on whether or not the current user's role matches the one listed or not
I've been trying to find solutions related to getting the current path for Drupal 8. I'm not sure if that is one of the issues or not.
class FacultyStaffRedirectSubscriber implements EventSubscriberInterface {
public function checkForRedirection(GetResponseEvent $event) {
$request = \Drupal::request();
$requestUrl = \Drupal::service('path.current')->getPath();
$current_user = \Drupal::currentUser();
$roles = $current_user->getRoles();
if ($requestUrl == '/faculty-staff') {
if (
!in_array('faculty', $roles) ||
!in_array('staff', $roles)
) {
$path = '/404';
$response = new RedirectResponse($path);
$response->send();
return;
}
}
}
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array('checkForRedirection');
return $events;
}
}
What I'm wanting is to redirect a user to 404 if they attempt to access the path without having the proper role assigned to them. Currently, nothing happens at all and logged in users can see the page.