Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added redirection check for administrator and site owner roles. #351

Closed
wants to merge 17 commits into from
Closed
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion modules/openy_gc_auth/src/GCUserAuthorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\openy_gc_auth\Event\GCUserLoginEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
* User Authorizer class.
Expand Down Expand Up @@ -72,11 +73,22 @@ public function authorizeUser($name, $email, array $extra_data = []) {
$account->save();
}
}
// List of roles to redirect user login page.
$userRolesArray = [
'administrator',
'virtual_ymca_editor',
];
// Redirecting user login page.
foreach ($userRolesArray as $role) {
if ($account->hasRole($role)) {
$response = new RedirectResponse('/user/login', 301);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add this code above:
$loginUrl = Url::fromRoute('user.login');
and replace this line:

Suggested change
$response = new RedirectResponse('/user/login', 301);
$response = new RedirectResponse($loginUrl, 301);

It will be a more stable approach. I had many projects in the past when we were renaming user login/register routes to avoid spam bots attacks. In your case, it is hard-coded.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anpolimus I have updated the code as per you suggestion, Can you please review.

$response->send();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to add Drupal message which is saying that you have to login as real user, sin you are an administrator.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anpolimus I have added Drupal message, Can you please review.

}
}
// Instantiate GC login user event.
$event = new GCUserLoginEvent($account, $extra_data);
// Dispatch the event.
$this->eventDispatcher->dispatch(GCUserLoginEvent::EVENT_NAME, $event);

user_login_finalize($account);

}
Expand Down