1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-08-06 12:02:45 +03:00

implement social auto registration feature

This commit is contained in:
Dan Brown
2018-07-29 20:28:49 +01:00
committed by Ibrahim Ennafaa
parent 01260d95f3
commit fe6dfcedf9
4 changed files with 50 additions and 7 deletions

View File

@@ -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;
}
}
}
}

View File

@@ -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) {