I have a requirement where user can login with google, but should not allow him to register using the google account.

I have seen in the code we are validating registration should be done from "admin_only" setting.

Its better to have a social auth setting to force user to register in the system like in account settings form
"Who can register accounts?
. Register & Login
. Login Only".

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sriharsha.uppuluri created an issue. See original summary.

sriharsha.uppuluri’s picture

Issue summary: View changes
sriharsha.uppuluri’s picture

Status: Active » Needs review
FileSize
3.35 KB

Attaching the patch

sriharsha.uppuluri’s picture

FileSize
2.87 KB
denutkarsh’s picture

Version: 8.x-1.0-beta2 » 8.x-1.x-dev

I guess all the feature request should go to 8.x-1.x-dev .

gvso’s picture

Title: Restrict user from registration using social auth » Restrict user to only register or log in
Status: Needs review » Needs work

I like this idea, but I think we should allow users to login, register, or both (default).

  1. +++ b/config/install/social_auth.settings.yml
    @@ -3,3 +3,4 @@ post_login: /user
     disabled_roles: []
    +user_register: 'register_login'
    

    With the changes, this might become an array. Also, we need a hook_update function in social_auth.install

  2. +++ b/config/schema/social_auth.schema.yml
    @@ -5,6 +5,9 @@ social_auth.settings:
    +      label: 'Can you register using google sign in button.'
         redirect_user_form:
    

    The label should be the same as the form field title.

  3. +++ b/src/Form/SocialAuthSettingsForm.php
    @@ -92,6 +92,16 @@ class SocialAuthSettingsForm extends ConfigFormBase {
    +      '#type' => 'radios',
    ...
    +      '#options' => array(
    +        'register_login' => $this->t('Register & Login'),
    +        'login' => $this->t('Login Only'),
    

    It would be better if we allow checkboxes with "Register" and "Login"

  4. +++ b/src/Form/SocialAuthSettingsForm.php
    @@ -92,6 +92,16 @@ class SocialAuthSettingsForm extends ConfigFormBase {
    +      '#title' => $this->t('Who can register accounts?'),
    

    What about "What can users do?"

denutkarsh’s picture

Status: Needs work » Needs review
FileSize
4.13 KB
3.98 KB

Here is the new patch, It still needs hook_update_N() implementation but let's see if everything else is okay. Sorry, wasn't able to test it out, but it should work.

denutkarsh’s picture

FileSize
1.17 KB
4.65 KB

Forgot some change in the previous patch. Here is the new Patch, (also added hook_update_N() )

gvso’s picture

Status: Needs review » Needs work
  1. +++ b/social_auth.install
    @@ -39,3 +39,14 @@ function social_auth_update_8002(&$sandbox) {
    +  $config->set('user_allowed', array('register', 'login'))
    +    ->save();
    

    Since this is short, ->save() should be in the same line. We need to explain what the update does too.

  2. +++ b/social_auth.install
    @@ -39,3 +39,14 @@ function social_auth_update_8002(&$sandbox) {
    +  $config->save();
    

    You don't need this here since you have already saved the configuration in the previous line.

For this new configuration, we need to change the warning 'Failed to create user. User registration is disabled in Drupal account settings' for something like 'Failed to create user. User registration is disabled.'

tameeshb’s picture

Status: Needs work » Needs review
FileSize
327 bytes
4.62 KB

Applied changes from #9 on patch #8, please review! :)

denutkarsh’s picture

Status: Needs review » Needs work

We need to explain what the update does too.

Looks like you missed this

gvso’s picture

Thanks @tameeshb,

We need to change the warning 'Failed to create user. User registration is disabled in Drupal account settings' in SocialAuthUserManager too

tameeshb’s picture

Status: Needs work » Needs review
FileSize
5.21 KB
715 bytes

Not very sure about what do i write for this:

We need to explain what the update does too.

Modified SocialAuthUserManager.php to change warning message.

himanshu-dixit’s picture

/**
* Implements hook_update_N().
*
* Default Value for new key user_allowed
*
* This update creates a new key user_allowed which
* has it's default value set to both register and login.
*/

Maybe this

gvso’s picture

Status: Needs review » Needs work

Maybe

/**
* Implements hook_update_N().
*
* Sets default value for new user_allowed key.
*
* user_allowed allows site builders to determine if
* users can only login, register, or both. This update
* creates the new user_allowed key with a default
* value set to both register and login.
*/

tameeshb’s picture

Status: Needs work » Needs review
FileSize
5.47 KB
552 bytes

Added documentation from #15

gvso’s picture

Status: Needs review » Needs work
+++ b/src/Form/SocialAuthSettingsForm.php
@@ -92,6 +92,16 @@ class SocialAuthSettingsForm extends ConfigFormBase {
+      '#type' => 'radios',
+      '#title' => $this->t('What can users do?'),
+      '#default_value' => $social_auth_config->get('user_allowed'),
+      '#options' => array(
+        'register' => $this->t('Register Only'),
+        'login' => $this->t('Login Only'),
+      ),

These should be checkboxes so that the user can select both behaviors if they want.

Also, this version of hook_update_N tries to use all the spaces up to 80 cols and removes the unnecessary $config variable

/**
 * Implements hook_update_N().
 *
 * Sets default value for new user_allowed key.
 *
 * user_allowed allows site builders to determine if users can only login,
 * register, or both. This update creates the new user_allowed key with a
 * default value set to both register and login.
 */
function social_auth_update_8003(&$sandbox) {
  // Sets and saves new user_allowed value.
  \Drupal::configFactory()->getEditable('social_auth.settings')
    ->set('user_allowed', array('register', 'login'))
    ->save();
}

Please update the project from the remote git repository. It seems you haven't done that before creating the last patch

denutkarsh’s picture

Assigned: Unassigned » denutkarsh

I thought i already used checkboxes :( Would provide the new patch today with testing

denutkarsh’s picture

Assigned: denutkarsh » gvso
Status: Needs work » Needs review
FileSize
5.5 KB

Here is the new patch rerolled and also implements changes in #17. IDK if i should attach the interdiff if it's a reroll. Here are the couple of things that i changed

  1. Changed user_register to user_allowed in SocialAuthSettingsForm.php according to .settings.yml
  2. Changed this if (!in_array('login', $this->configFactory->get('social_auth.settings')->get('user_allowed'),)) {
    to
    if (!in_array('login', $this->configFactory->get('social_auth.settings')->get('user_allowed'), true)) { .
    IDK why the previous code wasn't working.
  3. All changes mentioned in #17
  4. Some Documentation changes.

This time i tested this patch, with Social Auth Twitter and it works great. The only problem i noticed is that when the user registration is blocked, two error messages appear which i don't think is cool. I will open an issue for that. Assigning gvso to review this patch.

gvso’s picture

Status: Needs review » Needs work
+++ b/src/Form/SocialAuthSettingsForm.php
@@ -92,6 +92,16 @@ class SocialAuthSettingsForm extends ConfigFormBase {
+        'register' => $this->t('Register Only'),
+        'login' => $this->t('Login Only'),
+      ),

I'd change these to just "Register" and "Login"

What's the issue with the messages? Can't that issue be fixed in this patch?

denutkarsh’s picture

Status: Needs work » Needs review
FileSize
5.49 KB
619 bytes

I have attached the new patch following the suggestion in #20 .

What's the issue with the messages? Can't that issue be fixed in this patch?

drupal_set_message() is called in createUser when the registration is disabled.

if ($this->registrationBlocked()) {
      $this->loggerFactory
        ->get($this->getPluginId())
        ->warning('Failed to create user. User registration is disabled. Name: @name, email: @email.', array('@name' => $name, '@email' => $email));

      drupal_set_message($this->t('User registration is disabled, please contact the administrator.'), 'error');

      return FALSE;
    }

But since createUser() doesn't redirect to login page after this message is set the createUser() returns FALSE and then the following code is followed,

    $drupal_user = $this->createUser($name, $email);
    // If the new user could be registered.
    if ($drupal_user) {
      // Download profile picture for the newly created user.
      if ($picture_url) {
        $this->setProfilePic($drupal_user, $picture_url, $id);
      }
      // Authenticates and redirect new user.
      return $this->authenticateNewUser($drupal_user);
    }

    drupal_set_message($this->t('You could not be authenticated, please contact the administrator'), 'error');
    $this->nullifySessionKeys();
    return $this->redirect('user.login'); 

This way 2 messages are set with drupal_set_message 'User registration is disabled, please contact the administrator.' and 'You could not be authenticated, please contact the administrator' . I am not sure if we should fix that in this issue since we'll have to change createUser() function to maybe return RedirectResponse . What do you think?

gvso’s picture

Status: Needs review » Needs work

Since registrationBlocked() is modified by the patch, let's fix that issue here. Plus, I'd change registrationBlocked() for isRegistrationDisabled() to make it more consistent with other methods.

denutkarsh’s picture

Status: Needs work » Needs review
FileSize
3.04 KB
7.32 KB

Here is the new patch!

gvso’s picture

Ok, everything works fine. Thanks for the patch.

My only question is if we should allow to login the user who just registered. Currently, the user who registers can't login if the settings is set to allow registration but not login.

@sriharsha.uppuluri, could you help us finding the best approach for this?

himanshu-dixit’s picture

FileSize
7.32 KB

@gvso I think the current approach is fine. I tried to apply the patch on my local drupal installation but the patch needed rerolling. Here is the rerolled patch.

gvso’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev
Issue tags: +Needs reroll

We need to reroll this patch to make it work with 2.x

gvso’s picture

Status: Needs review » Needs work
rpayanm’s picture

Status: Needs work » Needs review
FileSize
6.65 KB
gvso’s picture

Status: Needs review » Needs work

I think, we should change the checkboxes for radio buttons "Register and login" and "Login only". I think it woudn't make much sense to allow user to register but no login since a password would be needed in that case.

  • gvso committed 6722f0e on 8.x-2.x
    Issue #2854156 by denutkarsh, tameeshb, sriharsha.uppuluri, himanshu-...
gvso’s picture

Status: Needs work » Fixed

Made the above change. Thanks everyone!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.