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 all 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', '@password_generator']
arguments: ['@entity_type.manager', '@event_dispatcher', '@messenger', '@password_generator']
35 changes: 34 additions & 1 deletion modules/openy_gc_auth/src/GCUserAuthorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Drupal\openy_gc_auth;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Password\PasswordGeneratorInterface;
use Drupal\openy_gc_auth\Event\GCUserLoginEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand All @@ -12,6 +15,8 @@
*/
class GCUserAuthorizer {

use StringTranslationTrait;

const VIRTUAL_Y_DEFAULT_ROLE = 'virtual_y';

/**
Expand All @@ -28,26 +33,37 @@ class GCUserAuthorizer {
*/
protected $eventDispatcher;

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

/**
* The service for generating passwords.
*
* @var \Drupal\Core\Password\PasswordGeneratorInterface
*/
protected $passwordGenerator;


/**
* 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.
* @param \Drupal\Core\Password\PasswordGeneratorInterface $password_generator
* Service for generating passwords.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, EventDispatcherInterface $event_dispatcher, PasswordGeneratorInterface $password_generator) {
public function __construct(EntityTypeManagerInterface $entityTypeManager, EventDispatcherInterface $event_dispatcher, MessengerInterface $messenger, PasswordGeneratorInterface $password_generator) {
$this->userStorage = $entityTypeManager->getStorage('user');
$this->eventDispatcher = $event_dispatcher;
$this->messenger = $messenger;
$this->passwordGenerator = $password_generator;
}

Expand Down Expand Up @@ -88,6 +104,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)) {
$link = Link::createFromRoute($this->t('Login'),
'user.login',
)->toString();
$this->messenger->addStatus($this->t('Please retry your login from this link. @link', [
'@link' => $link,
]));
return;
}
}
// Instantiate GC login user event.
$event = new GCUserLoginEvent($account, $extra_data);
// Dispatch the event.
Expand Down