This commit is contained in:
James Cole 2016-03-12 14:18:28 +01:00
parent a0fd4b505a
commit 2f19ff314b
4 changed files with 85 additions and 6 deletions

View File

@ -6,7 +6,7 @@ namespace FireflyIII\Http\Controllers\Auth;
use Auth;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Role;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Foundation\Auth\ThrottlesLogins;
@ -94,13 +94,14 @@ class AuthController extends Controller
/**
* Handle a registration request for the application.
*
* @param UserRepositoryInterface $repository
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
* @throws FireflyException
* @throws \Illuminate\Foundation\Validation\ValidationException
*/
public function register(Request $request)
public function register(UserRepositoryInterface $repository, Request $request)
{
$validator = $this->validator($request->all());
@ -146,12 +147,10 @@ class AuthController extends Controller
Session::flash('gaEventAction', 'new-registration');
// first user ever?
if (User::count() == 1) {
$admin = Role::where('name', 'owner')->first();
Auth::user()->attachRole($admin);
if ($repository->count() == 1) {
$repository->attachRole(Auth::user(), 'owner');
}
return redirect($this->redirectPath());
}
throw new FireflyException('The authenticated user object is invalid.');

View File

@ -86,6 +86,7 @@ class FireflyServiceProvider extends ServiceProvider
$this->app->bind('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface', 'FireflyIII\Repositories\Category\SingleCategoryRepository');
$this->app->bind('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface', 'FireflyIII\Repositories\Currency\CurrencyRepository');
$this->app->bind('FireflyIII\Support\Search\SearchInterface', 'FireflyIII\Support\Search\Search');
$this->app->bind('FireflyIII\Repositories\User\UserRepositoryInterface', 'FireflyIII\Repositories\User\UserRepository');
// CSV import
$this->app->bind('FireflyIII\Helpers\Csv\WizardInterface', 'FireflyIII\Helpers\Csv\Wizard');

View File

@ -0,0 +1,45 @@
<?php
/**
* UserRepository.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Repositories\User;
use FireflyIII\Models\Role;
use FireflyIII\User;
/**
* Class UserRepository
*
* @package FireflyIII\Repositories\User
*/
class UserRepository implements UserRepositoryInterface
{
/**
* @param User $user
* @param string $role
*
* @return bool
*/
public function attachRole(User $user, string $role): bool
{
$admin = Role::where('name', 'owner')->first();
$user->attachRole($admin);
return true;
}
/**
* @return int
*/
public function count(): int
{
return User::count();
}
}

View File

@ -0,0 +1,34 @@
<?php
/**
* UserRepositoryInterface.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Repositories\User;
use FireflyIII\User;
/**
* Interface UserRepositoryInterface
*
* @package FireflyIII\Repositories\User
*/
interface UserRepositoryInterface
{
/**
* @param User $user
* @param string $role
*
* @return bool
*/
public function attachRole(User $user, string $role): bool;
/**
* @return int
*/
public function count(): int;
}