From f3b7ab0c8ad5605962288f4a80aba9406e86b2cf Mon Sep 17 00:00:00 2001 From: Gaurav Mahlawat Date: Thu, 23 Nov 2023 18:22:16 +0530 Subject: [PATCH 1/3] social-login-redirect: Redirection added for social auth users --- web/modules/custom/ct_user/ct_user.module | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/web/modules/custom/ct_user/ct_user.module b/web/modules/custom/ct_user/ct_user.module index ba61133a..1b766441 100644 --- a/web/modules/custom/ct_user/ct_user.module +++ b/web/modules/custom/ct_user/ct_user.module @@ -6,6 +6,7 @@ */ use Drupal\Core\Routing\RouteMatchInterface; +use Symfony\Component\HttpFoundation\RedirectResponse; /** * Implements hook_help(). @@ -22,3 +23,47 @@ function ct_user_help($route_name, RouteMatchInterface $route_match) { default: } } + +/** + * Implements hook_form_alter(). + */ +function ct_user_form_alter(&$form, $form_state, $form_id) { + // Check if the form is the user login form. + if ($form_id == 'user_login_form') { + // Add a custom validation function to the form. + $form['#validate'][] = 'ct_user_user_login_form_validate'; + } +} + +/** + * Custom validation function for the user login form. + */ +function ct_user_user_login_form_validate(&$form, $form_state) { + // Get the name value from the form. + $name = $form_state->getValue('name'); + + // Get the id of the user. + $ids = \Drupal::entityQuery('user') + ->condition('name', $name) + ->range(0, 1) + ->accessCheck() + ->execute(); + + if ($ids) { + // Check if there is a social auth profile. + $socialIds = \Drupal::entityQuery("social_auth") + ->accessCheck(FALSE) + ->condition("user_id", reset($ids)) + ->execute(); + } + + // If the user has no social auth profile, allow the login to continue. + if (empty($socialIds)) { + return; + } + + // If the user has social auth profile, redirect them to /user/login/google. + $response = new RedirectResponse('/user/login/google'); + $response->send(); + user_logout(); +} From 4e8818885b7204dce263193b93e61618a08528f4 Mon Sep 17 00:00:00 2001 From: Gaurav Mahlawat Date: Mon, 27 Nov 2023 12:26:51 +0530 Subject: [PATCH 2/3] social-login-redirect: Conditions updated for checking user id and social auth profile --- web/modules/custom/ct_user/ct_user.module | 27 ++++++++++++++--------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/web/modules/custom/ct_user/ct_user.module b/web/modules/custom/ct_user/ct_user.module index 1b766441..3a501f6a 100644 --- a/web/modules/custom/ct_user/ct_user.module +++ b/web/modules/custom/ct_user/ct_user.module @@ -46,24 +46,29 @@ function ct_user_user_login_form_validate(&$form, $form_state) { $ids = \Drupal::entityQuery('user') ->condition('name', $name) ->range(0, 1) - ->accessCheck() + ->accessCheck(FALSE) ->execute(); - if ($ids) { + // Check if there is a User ID available. + if (empty($ids)) { + return; + } + else { // Check if there is a social auth profile. $socialIds = \Drupal::entityQuery("social_auth") ->accessCheck(FALSE) ->condition("user_id", reset($ids)) ->execute(); - } - // If the user has no social auth profile, allow the login to continue. - if (empty($socialIds)) { - return; + // If the user has no social auth profile, allow the login to continue. + if (empty($socialIds)) { + return; + } + else { + // If the user has social auth profile, redirect them to /user/login/google. + $response = new RedirectResponse('/user/login/google'); + $response->send(); + user_logout(); + } } - - // If the user has social auth profile, redirect them to /user/login/google. - $response = new RedirectResponse('/user/login/google'); - $response->send(); - user_logout(); } From fa8d967138001bc579e64ac9a273eebaea01c9ab Mon Sep 17 00:00:00 2001 From: Gaurav Mahlawat Date: Mon, 27 Nov 2023 20:16:48 +0530 Subject: [PATCH 3/3] social-login-redirect: Else condition removed --- web/modules/custom/ct_user/ct_user.module | 29 ++++++++++------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/web/modules/custom/ct_user/ct_user.module b/web/modules/custom/ct_user/ct_user.module index 3a501f6a..eb09522c 100644 --- a/web/modules/custom/ct_user/ct_user.module +++ b/web/modules/custom/ct_user/ct_user.module @@ -53,22 +53,19 @@ function ct_user_user_login_form_validate(&$form, $form_state) { if (empty($ids)) { return; } - else { - // Check if there is a social auth profile. - $socialIds = \Drupal::entityQuery("social_auth") - ->accessCheck(FALSE) - ->condition("user_id", reset($ids)) - ->execute(); - // If the user has no social auth profile, allow the login to continue. - if (empty($socialIds)) { - return; - } - else { - // If the user has social auth profile, redirect them to /user/login/google. - $response = new RedirectResponse('/user/login/google'); - $response->send(); - user_logout(); - } + // Check if there is a social auth profile. + $socialIds = \Drupal::entityQuery("social_auth") + ->accessCheck(FALSE) + ->condition("user_id", reset($ids)) + ->execute(); + + // If the user has no social auth profile, allow the login to continue. + if (empty($socialIds)) { + return; } + + // If the user has social auth profile, redirect them to /user/login/google. + $response = new RedirectResponse('/user/login/google'); + $response->send(); }