From fe6dfcedf9692128c0f394657dbdfac7cc0cf2c9 Mon Sep 17 00:00:00 2001
From: Dan Brown <ssddanbrown@googlemail.com>
Date: Sun, 29 Jul 2018 20:28:49 +0100
Subject: [PATCH 1/4] implement social auto registration feature

---
 app/Repos/UserRepo.php                   | 14 +++++-----
 app/Services/SocialAuthService.php       | 34 ++++++++++++++++++++++++
 resources/lang/en/settings.php           |  4 ++-
 resources/views/settings/index.blade.php |  5 ++++
 4 files changed, 50 insertions(+), 7 deletions(-)

diff --git a/app/Repos/UserRepo.php b/app/Repos/UserRepo.php
index d113b676a..b828a582b 100644
--- a/app/Repos/UserRepo.php
+++ b/app/Repos/UserRepo.php
@@ -76,14 +76,15 @@ class UserRepo
         return $query->paginate($count);
     }
 
-    /**
+        /**
      * Creates a new user and attaches a role to them.
      * @param array $data
+     * @param boolean autoVerifyEmail
      * @return User
      */
-    public function registerNew(array $data)
+    public function registerNew(array $data, $autoVerifyEmail=false)
     {
-        $user = $this->create($data);
+        $user = $this->create($data, $autoVerifyEmail);
         $this->attachDefaultRole($user);
 
         // Get avatar from gravatar and save
@@ -143,13 +144,14 @@ class UserRepo
      * @param array $data
      * @return User
      */
-    public function create(array $data)
+    public function create(array $data, $autoVerifyEmail)
     {
+
         return $this->user->forceCreate([
             'name'     => $data['name'],
             'email'    => $data['email'],
             'password' => bcrypt($data['password']),
-            'email_confirmed' => false
+            'email_confirmed' => $autoVerifyEmail
         ]);
     }
 
@@ -259,4 +261,4 @@ class UserRepo
             return false;
         }
     }
-}
+}
\ No newline at end of file
diff --git a/app/Services/SocialAuthService.php b/app/Services/SocialAuthService.php
index dac6b7773..6b4c221b0 100644
--- a/app/Services/SocialAuthService.php
+++ b/app/Services/SocialAuthService.php
@@ -109,6 +109,40 @@ class SocialAuthService
             return redirect()->intended('/');
         }
 
+        // When a user is not logged in and no matching SocialAccount exists,
+        // If the auto social registration is enabled, attach the social account, create new user and log him in.
+        if (!$isLoggedIn && $socialAccount === null && setting('autosocialregistration-confirmation')) {
+
+            // Fill social account
+            $socialAccount = $this->fillSocialAccount($socialDriver, $socialUser);
+
+            // Create an array of the user data to create a new user instance
+            $userData = [
+                'name' => $socialUser->getName(),
+                'email' => $socialUser->getEmail(),
+                'password' => str_random(30)
+            ];
+
+            // Check domain if domain restriction setting is set
+            if (setting('registration-restrict')) {
+                $restrictedEmailDomains = explode(',', str_replace(' ', '', setting('registration-restrict')));
+                $userEmailDomain = $domain = substr(strrchr($socialUser->getEmail(), "@"), 1);
+                if (!in_array($userEmailDomain, $restrictedEmailDomains)) {
+                    throw new SocialSignInException(trans('auth.registration_email_domain_invalid'), '/login');
+                }
+            }
+
+            // Register new user with autoVerifyEmail set to true and attach the social account
+            $newUser = $this->userRepo->registerNew($userData, true);
+            $newUser->socialAccounts()->save($socialAccount);
+            $newUser->save();
+
+            // Log him in
+            auth()->login($newUser);
+
+            return redirect()->intended('/');
+        }
+
         // When a user is logged in but the social account does not exist,
         // Create the social account and attach it to the user & redirect to the profile page.
         if ($isLoggedIn && $socialAccount === null) {
diff --git a/resources/lang/en/settings.php b/resources/lang/en/settings.php
index d6fbb6107..824301522 100755
--- a/resources/lang/en/settings.php
+++ b/resources/lang/en/settings.php
@@ -44,6 +44,8 @@ return [
 
     'reg_settings' => 'Registration Settings',
     'reg_allow' => 'Allow registration?',
+    'reg_auto_social_allow'  => 'Allow auto social registration?',
+    'reg_auto_social_allow_desc' => 'If the social user doesn\'t exist, automatically sign him up. Domain restriction is respected if set. Email is also automatically validated for this kind of social registration.',
     'reg_default_role' => 'Default user role after registration',
     'reg_confirm_email' => 'Require email confirmation?',
     'reg_confirm_email_desc' => 'If domain restriction is used then email confirmation will be required and the below value will be ignored.',
@@ -148,7 +150,7 @@ return [
         'it' => 'Italian',
         'ru' => 'Русский',
         'zh_CN' => '简体中文',
-	    'zh_TW' => '繁體中文'
+        'zh_TW' => '繁體中文'
     ]
     ///////////////////////////////////
 ];
diff --git a/resources/views/settings/index.blade.php b/resources/views/settings/index.blade.php
index 64017e6e0..bcaf2f4d7 100644
--- a/resources/views/settings/index.blade.php
+++ b/resources/views/settings/index.blade.php
@@ -126,6 +126,11 @@
                                 @endforeach
                             </select>
                         </div>
+                        <div class="form-group">
+                            <label for="setting-autosocialregistration-confirmation">{{ trans('settings.reg_auto_social_allow') }}</label>
+                            <p class="small">{{ trans('settings.reg_auto_social_allow_desc') }}</p>
+                            @include('components.toggle-switch', ['name' => 'setting-autosocialregistration-confirmation', 'value' => setting('autosocialregistration-confirmation')])
+                        </div>
                         <div class="form-group">
                             <label for="setting-registration-confirmation">{{ trans('settings.reg_confirm_email') }}</label>
                             <p class="small">{{ trans('settings.reg_confirm_email_desc') }}</p>

From b224a2c8a0862ca4d91389bba4d98cfaef3127b8 Mon Sep 17 00:00:00 2001
From: Ibrahim Ennafaa <ibrahim@ennafaa.com>
Date: Thu, 16 Aug 2018 21:52:16 +0000
Subject: [PATCH 2/4] attempt to fix unit test error for admin creation

---
 app/Repos/UserRepo.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/app/Repos/UserRepo.php b/app/Repos/UserRepo.php
index b828a582b..232c5315a 100644
--- a/app/Repos/UserRepo.php
+++ b/app/Repos/UserRepo.php
@@ -144,7 +144,7 @@ class UserRepo
      * @param array $data
      * @return User
      */
-    public function create(array $data, $autoVerifyEmail)
+    public function create(array $data, $autoVerifyEmail=false)
     {
 
         return $this->user->forceCreate([

From 572e75b7831c67f59a873e455565216441a3f2d8 Mon Sep 17 00:00:00 2001
From: Ibrahim Ennafaa <ibrahim.ennafaa@teads.tv>
Date: Mon, 20 Aug 2018 21:19:25 -0400
Subject: [PATCH 3/4] Update UserRepo.php

---
 app/Repos/UserRepo.php | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/app/Repos/UserRepo.php b/app/Repos/UserRepo.php
index 232c5315a..b31f0334d 100644
--- a/app/Repos/UserRepo.php
+++ b/app/Repos/UserRepo.php
@@ -76,7 +76,7 @@ class UserRepo
         return $query->paginate($count);
     }
 
-        /**
+     /**
      * Creates a new user and attaches a role to them.
      * @param array $data
      * @param boolean autoVerifyEmail
@@ -261,4 +261,4 @@ class UserRepo
             return false;
         }
     }
-}
\ No newline at end of file
+}

From d2f5313f92c857c4ff71aea5cdb206662be20f3e Mon Sep 17 00:00:00 2001
From: Ibrahim Ennafaa <ibrahim.ennafaa@teads.tv>
Date: Tue, 21 Aug 2018 12:44:42 -0400
Subject: [PATCH 4/4] add missing @param in method comment

---
 app/Repos/UserRepo.php | 1 +
 1 file changed, 1 insertion(+)

diff --git a/app/Repos/UserRepo.php b/app/Repos/UserRepo.php
index b31f0334d..6defe8aa5 100644
--- a/app/Repos/UserRepo.php
+++ b/app/Repos/UserRepo.php
@@ -142,6 +142,7 @@ class UserRepo
     /**
      * Create a new basic instance of user.
      * @param array $data
+     * @param boolean $autoVerifyEmail
      * @return User
      */
     public function create(array $data, $autoVerifyEmail=false)