use Oxbow\Exceptions\UserRegistrationException;
use Oxbow\Repos\UserRepo;
use Oxbow\Services\EmailConfirmationService;
-use Oxbow\Services\Facades\Setting;
use Oxbow\Services\SocialAuthService;
-use Oxbow\User;
+use Oxbow\SocialAccount;
use Validator;
use Oxbow\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
*/
public function __construct(SocialAuthService $socialAuthService, EmailConfirmationService $emailConfirmationService, UserRepo $userRepo)
{
- $this->middleware('guest', ['only' => ['getLogin', 'postLogin', 'getRegister']]);
+ $this->middleware('guest', ['only' => ['getLogin', 'postLogin', 'getRegister', 'postRegister']]);
$this->socialAuthService = $socialAuthService;
$this->emailConfirmationService = $emailConfirmationService;
$this->userRepo = $userRepo;
/**
* Get a validator for an incoming registration request.
- *
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
]);
}
- /**
- * Create a new user instance after a valid registration.
- *
- * @param array $data
- * @return User
- */
- protected function create(array $data)
- {
- return User::create([
- 'name' => $data['name'],
- 'email' => $data['email'],
- 'password' => bcrypt($data['password']),
- ]);
- }
-
protected function checkRegistrationAllowed()
{
- if(!\Setting::get('registration-enabled')) {
+ if (!\Setting::get('registration-enabled')) {
throw new UserRegistrationException('Registrations are currently disabled.', '/login');
}
}
/**
* Show the application registration form.
- *
* @return \Illuminate\Http\Response
*/
public function getRegister()
/**
* Handle a registration request for the application.
- *
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
* @throws UserRegistrationException
);
}
- if(\Setting::get('registration-restrict')) {
+ $userData = $request->all();
+ return $this->registerUser($userData);
+ }
+
+ /**
+ * Register a new user after a registration callback.
+ * @param $socialDriver
+ * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
+ * @throws UserRegistrationException
+ */
+ protected function socialRegisterCallback($socialDriver)
+ {
+ $socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver);
+ $socialAccount = $this->socialAuthService->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)
+ ];
+ return $this->registerUser($userData, $socialAccount);
+ }
+
+ /**
+ * The registrations flow for all users.
+ * @param array $userData
+ * @param bool|false|SocialAccount $socialAccount
+ * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
+ * @throws UserRegistrationException
+ * @throws \Oxbow\Exceptions\ConfirmationEmailException
+ */
+ protected function registerUser(array $userData, $socialAccount = false)
+ {
+ if (\Setting::get('registration-restrict')) {
$restrictedEmailDomains = explode(',', str_replace(' ', '', \Setting::get('registration-restrict')));
- $userEmailDomain = $domain = substr(strrchr($request->get('email'), "@"), 1);
- if(!in_array($userEmailDomain, $restrictedEmailDomains)) {
+ $userEmailDomain = $domain = substr(strrchr($userData['email'], "@"), 1);
+ if (!in_array($userEmailDomain, $restrictedEmailDomains)) {
throw new UserRegistrationException('That email domain does not have access to this application', '/register');
}
}
- $newUser = $this->create($request->all());
- $newUser->attachRoleId(\Setting::get('registration-role'), 1);
+ $newUser = $this->userRepo->registerNew($userData);
+ if ($socialAccount) {
+ $newUser->socialAccounts()->save($socialAccount);
+ }
- if(\Setting::get('registration-confirmation') || \Setting::get('registration-restrict')) {
+ if (\Setting::get('registration-confirmation') || \Setting::get('registration-restrict')) {
$newUser->email_confirmed = false;
$newUser->save();
$this->emailConfirmationService->sendConfirmation($newUser);
}
auth()->login($newUser);
+ session()->flash('success', 'Thanks for signing up! You are now registered and signed in.');
return redirect($this->redirectPath());
}
/**
* Show the application login form.
- *
* @return \Illuminate\Http\Response
*/
public function getLogin()
*/
public function getSocialLogin($socialDriver)
{
+ session()->put('social-callback', 'login');
return $this->socialAuthService->startLogIn($socialDriver);
}
+ /**
+ * Redirect to the social site for authentication initended to register.
+ * @param $socialDriver
+ * @return mixed
+ */
+ public function socialRegister($socialDriver)
+ {
+ $this->checkRegistrationAllowed();
+ session()->put('social-callback', 'register');
+ return $this->socialAuthService->startRegister($socialDriver);
+ }
+
/**
* The callback for social login services.
- *
* @param $socialDriver
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws SocialSignInException
*/
public function socialCallback($socialDriver)
{
- return $this->socialAuthService->handleCallback($socialDriver);
+ if (session()->has('social-callback')) {
+ $action = session()->pull('social-callback');
+ if ($action == 'login') {
+ return $this->socialAuthService->handleLoginCallback($socialDriver);
+ } elseif ($action == 'register') {
+ return $this->socialRegisterCallback($socialDriver);
+ }
+ } else {
+ throw new SocialSignInException('No action defined', '/login');
+ }
+ return redirect()->back();
}
/**
}
-
/**
* Update the specified settings in storage.
*
use Illuminate\Support\Facades\Hash;
use Oxbow\Http\Requests;
+use Oxbow\Repos\UserRepo;
use Oxbow\Services\SocialAuthService;
use Oxbow\User;
{
protected $user;
+ protected $userRepo;
/**
* UserController constructor.
* @param $user
*/
- public function __construct(User $user)
+ public function __construct(User $user, UserRepo $userRepo)
{
$this->user = $user;
+ $this->userRepo = $userRepo;
parent::__construct();
}
$this->checkPermissionOr('user-delete', function () use ($id) {
return $this->currentUser->id == $id;
});
- $user = $this->user->findOrFail($id);
+ $user = $this->userRepo->getById($id);
// Delete social accounts
+ if($this->userRepo->isOnlyAdmin($user)) {
+ session()->flash('error', 'You cannot delete the only admin');
+ return redirect($user->getEditUrl());
+ }
$user->socialAccounts()->delete();
$user->delete();
return redirect('/users');
Route::get('/register/confirm/awaiting', 'Auth\AuthController@showAwaitingConfirmation');
Route::post('/register/confirm/resend', 'Auth\AuthController@resendConfirmation');
Route::get('/register/confirm/{token}', 'Auth\AuthController@confirmEmail');
+Route::get('/register/service/{socialDriver}', 'Auth\AuthController@socialRegister');
Route::post('/register', 'Auth\AuthController@postRegister');
// Password reset link request routes...
--- /dev/null
+<?php namespace Oxbow\Providers;
+
+
+use Illuminate\Support\ServiceProvider;
+
+class SocialiteServiceProvider extends ServiceProvider
+{
+ /**
+ * Indicates if loading of the provider is deferred.
+ *
+ * @var bool
+ */
+ protected $defer = true;
+
+ /**
+ * Register the service provider.
+ *
+ * @return void
+ */
+ public function register()
+ {
+ $this->app->bindShared('Laravel\Socialite\Contracts\Factory', function ($app) {
+ return new SocialiteManager($app);
+ });
+ }
+
+ /**
+ * Get the services provided by the provider.
+ *
+ * @return array
+ */
+ public function provides()
+ {
+ return ['Laravel\Socialite\Contracts\Factory'];
+ }
+}
\ No newline at end of file
<?php namespace Oxbow\Repos;
+use Oxbow\Role;
use Oxbow\User;
class UserRepo
{
protected $user;
+ protected $role;
/**
* UserRepo constructor.
* @param $user
*/
- public function __construct(User $user)
+ public function __construct(User $user, Role $role)
{
$this->user = $user;
+ $this->role = $role;
}
-
- public function getByEmail($email) {
+ /**
+ * @param string $email
+ * @return User|null
+ */
+ public function getByEmail($email)
+ {
return $this->user->where('email', '=', $email)->first();
}
+ /**
+ * @param int $id
+ * @return User
+ */
public function getById($id)
{
return $this->user->findOrFail($id);
}
+
+ /**
+ * Creates a new user and attaches a role to them.
+ * @param array $data
+ * @return User
+ */
+ public function registerNew(array $data)
+ {
+ $user = $this->create($data);
+ $roleId = \Setting::get('registration-role');
+
+ if ($roleId === false) {
+ $roleId = $this->role->getDefault()->id;
+ }
+
+ $user->attachRoleId($roleId);
+ return $user;
+ }
+
+ /**
+ * Checks if the give user is the only admin.
+ * @param User $user
+ * @return bool
+ */
+ public function isOnlyAdmin(User $user)
+ {
+ if ($user->role->name != 'admin') {
+ return false;
+ }
+
+ $adminRole = $this->role->where('name', '=', 'admin')->first();
+ if (count($adminRole->users) > 1) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Create a new basic instance of user.
+ * @param array $data
+ * @return User
+ */
+ public function create(array $data)
+ {
+ return $this->user->create([
+ 'name' => $data['name'],
+ 'email' => $data['email'],
+ 'password' => bcrypt($data['password'])
+ ]);
+ }
}
\ No newline at end of file
*/
public function sendConfirmation(User $user)
{
- if($user->email_confirmed) {
+ if ($user->email_confirmed) {
throw new ConfirmationEmailException('Email has already been confirmed, Try logging in.', '/login');
}
$this->deleteConfirmationsByUser($user);
}
// If more than a day old
- if(Carbon::now()->subDay()->gt($emailConfirmation->created_at)) {
+ if (Carbon::now()->subDay()->gt($emailConfirmation->created_at)) {
$this->sendConfirmation($emailConfirmation->user);
throw new UserRegistrationException('The confirmation token has expired, A new confirmation email has been sent.', '/register/confirm');
}
<?php namespace Oxbow\Services;
+use GuzzleHttp\Exception\ClientException;
use Laravel\Socialite\Contracts\Factory as Socialite;
use Oxbow\Exceptions\SocialDriverNotConfigured;
use Oxbow\Exceptions\SocialSignInException;
+use Oxbow\Exceptions\UserRegistrationException;
+use Oxbow\Http\Controllers\Auth\AuthController;
use Oxbow\Repos\UserRepo;
use Oxbow\SocialAccount;
use Oxbow\User;
$this->socialAccount = $socialAccount;
}
+
/**
* Start the social login path.
- * @param $socialDriver
+ * @param string $socialDriver
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* @throws SocialDriverNotConfigured
*/
}
/**
- * Get a user from socialite after a oAuth callback.
- *
+ * Start the social registration process
+ * @param string $socialDriver
+ * @return \Symfony\Component\HttpFoundation\RedirectResponse
+ * @throws SocialDriverNotConfigured
+ */
+ public function startRegister($socialDriver)
+ {
+ $driver = $this->validateDriver($socialDriver);
+ return $this->socialite->driver($driver)->redirect();
+ }
+
+ /**
+ * Handle the social registration process on callback.
* @param $socialDriver
- * @return User
+ * @return \Laravel\Socialite\Contracts\User
+ * @throws SocialDriverNotConfigured
+ * @throws UserRegistrationException
+ */
+ public function handleRegistrationCallback($socialDriver)
+ {
+ $driver = $this->validateDriver($socialDriver);
+
+ // Get user details from social driver
+ $socialUser = $this->socialite->driver($driver)->user();
+
+ // Check social account has not already been used
+ if ($this->socialAccount->where('driver_id', '=', $socialUser->getId())->exists()) {
+ throw new UserRegistrationException('This ' . $socialDriver . ' account is already in use, Try logging in via the ' . $socialDriver . ' option.', '/login');
+ }
+
+ if($this->userRepo->getByEmail($socialUser->getEmail())) {
+ $email = $socialUser->getEmail();
+ throw new UserRegistrationException('The email '. $email.' is already in use. If you already have an account you can connect your ' . $socialDriver .' account from your profile settings.', '/login');
+ }
+
+ return $socialUser;
+ }
+
+ /**
+ * Handle the login process on a oAuth callback.
+ * @param $socialDriver
+ * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws SocialDriverNotConfigured
* @throws SocialSignInException
*/
- public function handleCallback($socialDriver)
+ public function handleLoginCallback($socialDriver)
{
$driver = $this->validateDriver($socialDriver);
// Otherwise let the user know this social account is not used by anyone.
$message = 'This ' . $socialDriver . ' account is not linked to any users. Please attach it in your profile settings';
- if(\Setting::get('registration-enabled')) {
- $message .= 'or, If you do not yet have an account, You can register an account using the ' . $socialDriver . ' option';
+ if (\Setting::get('registration-enabled')) {
+ $message .= ' or, If you do not yet have an account, You can register an account using the ' . $socialDriver . ' option';
}
throw new SocialSignInException($message . '.', '/login');
}
+
private function logUserIn($user)
{
auth()->login($user);
}
/**
- * @param $socialDriver
- * @param $socialUser
+ * @param string $socialDriver
+ * @param \Laravel\Socialite\Contracts\User $socialUser
+ * @return SocialAccount
*/
- private function fillSocialAccount($socialDriver, $socialUser)
+ public function fillSocialAccount($socialDriver, $socialUser)
{
$this->socialAccount->fill([
'driver' => $socialDriver,
'driver_id' => $socialUser->getId(),
'avatar' => $socialUser->getAvatar()
]);
+ return $this->socialAccount;
}
/**
<div class="form-group">
<label for="password">Password</label>
- @include('form/password', ['name' => 'password'])
+ @include('form/password', ['name' => 'password', 'placeholder' => 'Must be over 5 characters'])
</div>
<div class="from-group">