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
Show file tree
Hide file tree
Changes from 12 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
2 changes: 1 addition & 1 deletion modules/openy_gc_auth/openy_gc_auth.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ services:

openy_gc_auth.user_authorizer:
class: Drupal\openy_gc_auth\GCUserAuthorizer
arguments: ['@entity_type.manager', '@event_dispatcher']
arguments: ['@entity_type.manager', '@event_dispatcher', '@messenger']
29 changes: 27 additions & 2 deletions modules/openy_gc_auth/src/GCUserAuthorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace Drupal\openy_gc_auth;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Drupal\openy_gc_auth\Event\GCUserLoginEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
* User Authorizer class.
Expand All @@ -27,17 +30,27 @@ class GCUserAuthorizer {
*/
protected $eventDispatcher;

/**
* The Messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;

/**
* GCUserAuthorizer constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity Type Manager.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* Event dispatcher.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, EventDispatcherInterface $event_dispatcher) {
public function __construct(EntityTypeManagerInterface $entityTypeManager, EventDispatcherInterface $event_dispatcher, MessengerInterface $messenger) {
$this->userStorage = $entityTypeManager->getStorage('user');
$this->eventDispatcher = $event_dispatcher;
$this->messenger = $messenger;
}

/**
Expand Down Expand Up @@ -72,11 +85,23 @@ 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)) {
$loginUrl = Url::fromRoute('user.login')->toString();
$this->messenger->addMessage($this->t('Please retry your login on this form.'));
Copy link

Choose a reason for hiding this comment

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

This line is throwing an error:

Error: Call to undefined method Drupal\openy_gc_auth\GCUserAuthorizer::t() in Drupal\openy_gc_auth\GCUserAuthorizer->authorizeUser() (line 97 of /.../docroot/modules/contrib/openy_gated_content/modules/openy_gc_auth/src/GCUserAuthorizer.php)

We're reviewing for a fix.

Copy link

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

Updated the code as per suggestions, Please review.

return new RedirectResponse($loginUrl, 302);
}
}
// 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