diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index c0e26738..54f14c11 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\AuthenticatesUsers; +use App\User; class LoginController extends Controller { @@ -25,7 +26,7 @@ class LoginController extends Controller * * @var string */ - protected $redirectTo = '/home'; + protected $redirectTo = '/'; /** * Create a new controller instance. @@ -38,7 +39,13 @@ class LoginController extends Controller } public function index() { - $data =[]; + $data['users'] = User::all(); return view('userselect', $data); } + + public function setUser(User $user) + { + session(['current_user' => $user]); + return redirect()->route('dash'); + } } diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index d9d3ee69..04c8100d 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -17,15 +17,16 @@ class Controller extends BaseController public function __construct() { - $this->user = $this->user(); + $this->middleware(function ($request, $next) { + $this->user = $this->user(); + //print_r($this->user); + return $next($request); + }); + } public function user() { - if (Auth::check()) { // if logged in, set this user - return Auth::user(); - } else { // not logged in, get first user - return User::first(); - } + return User::currentUser(); } } diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php new file mode 100644 index 00000000..a3af7dd8 --- /dev/null +++ b/app/Http/Controllers/HomeController.php @@ -0,0 +1,28 @@ +middleware('auth'); + } + + /** + * Show the application dashboard. + * + * @return \Illuminate\Http\Response + */ + public function index() + { + return view('home'); + } +} diff --git a/app/Item.php b/app/Item.php index 14b85591..781cab7c 100644 --- a/app/Item.php +++ b/app/Item.php @@ -5,12 +5,23 @@ namespace App; use Illuminate\Database\Eloquent\Model; use Symfony\Component\ClassLoader\ClassMapGenerator; use Illuminate\Database\Eloquent\SoftDeletes; - +use Illuminate\Database\Eloquent\Builder; +use App\User; class Item extends Model { use SoftDeletes; + protected static function boot() + { + parent::boot(); + + static::addGlobalScope('user_id', function (Builder $builder) { + $current_user = User::currentUser(); + $builder->where('user_id', $current_user->id); + }); + } + // protected $fillable = [ 'title', 'url', 'colour', 'icon', 'description', 'pinned', 'order', 'type', 'user_id' diff --git a/app/Setting.php b/app/Setting.php index 0b8f76af..11edd04e 100644 --- a/app/Setting.php +++ b/app/Setting.php @@ -170,7 +170,8 @@ class Setting extends Model if(isset($usersetting) && !empty($usersetting)) { $value = $usersetting->pivot->value; } else { // if not get default from base setting - $user->settings()->save($find, ['value' => $find->value]); + //$user->settings()->save($find, ['value' => $find->value]); + $user->settings()->updateExistingPivot($find->id, ['value' => $find->value]); $value = $find->value; } @@ -267,11 +268,7 @@ class Setting extends Model public static function user() { - if (Auth::check()) { // if logged in, set this user - return Auth::user(); - } else { // not logged in, get first user - return User::first(); - } + return User::currentUser(); } diff --git a/app/User.php b/app/User.php index e991e964..cce5315e 100644 --- a/app/User.php +++ b/app/User.php @@ -43,4 +43,17 @@ class User extends Authenticatable return $this->belongsToMany('App\Setting')->withPivot('value'); } + public static function currentUser() + { + $current_user = session('current_user'); + if ($current_user) { // if logged in, set this user + return $current_user; + } else { // not logged in, get first user + $user = User::first(); + session(['current_user' => $user]); + return $user; + } + + } + } diff --git a/database/app.sqlite-journal b/database/app.sqlite-journal deleted file mode 100644 index b2147813..00000000 Binary files a/database/app.sqlite-journal and /dev/null differ diff --git a/resources/views/auth/login.blade.php b/resources/views/auth/login.blade.php new file mode 100644 index 00000000..07924ce6 --- /dev/null +++ b/resources/views/auth/login.blade.php @@ -0,0 +1,69 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
+
Login
+ +
+
+ {{ csrf_field() }} + +
+ + +
+ + + @if ($errors->has('email')) + + {{ $errors->first('email') }} + + @endif +
+
+ +
+ + +
+ + + @if ($errors->has('password')) + + {{ $errors->first('password') }} + + @endif +
+
+ +
+
+
+ +
+
+
+ +
+
+ + + + Forgot Your Password? + +
+
+
+
+
+
+
+
+@endsection diff --git a/resources/views/auth/passwords/email.blade.php b/resources/views/auth/passwords/email.blade.php new file mode 100644 index 00000000..ad38245b --- /dev/null +++ b/resources/views/auth/passwords/email.blade.php @@ -0,0 +1,47 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
+
Reset Password
+ +
+ @if (session('status')) +
+ {{ session('status') }} +
+ @endif + +
+ {{ csrf_field() }} + +
+ + +
+ + + @if ($errors->has('email')) + + {{ $errors->first('email') }} + + @endif +
+
+ +
+
+ +
+
+
+
+
+
+
+
+@endsection diff --git a/resources/views/auth/passwords/reset.blade.php b/resources/views/auth/passwords/reset.blade.php new file mode 100644 index 00000000..84ec0101 --- /dev/null +++ b/resources/views/auth/passwords/reset.blade.php @@ -0,0 +1,70 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
+
Reset Password
+ +
+
+ {{ csrf_field() }} + + + +
+ + +
+ + + @if ($errors->has('email')) + + {{ $errors->first('email') }} + + @endif +
+
+ +
+ + +
+ + + @if ($errors->has('password')) + + {{ $errors->first('password') }} + + @endif +
+
+ +
+ +
+ + + @if ($errors->has('password_confirmation')) + + {{ $errors->first('password_confirmation') }} + + @endif +
+
+ +
+
+ +
+
+
+
+
+
+
+
+@endsection diff --git a/resources/views/auth/register.blade.php b/resources/views/auth/register.blade.php new file mode 100644 index 00000000..38eef838 --- /dev/null +++ b/resources/views/auth/register.blade.php @@ -0,0 +1,77 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
+
Register
+ +
+
+ {{ csrf_field() }} + +
+ + +
+ + + @if ($errors->has('name')) + + {{ $errors->first('name') }} + + @endif +
+
+ +
+ + +
+ + + @if ($errors->has('email')) + + {{ $errors->first('email') }} + + @endif +
+
+ +
+ + +
+ + + @if ($errors->has('password')) + + {{ $errors->first('password') }} + + @endif +
+
+ +
+ + +
+ +
+
+ +
+
+ +
+
+
+
+
+
+
+
+@endsection diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php new file mode 100644 index 00000000..d8437bf8 --- /dev/null +++ b/resources/views/home.blade.php @@ -0,0 +1,23 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
+
Dashboard
+ +
+ @if (session('status')) +
+ {{ session('status') }} +
+ @endif + + You are logged in! +
+
+
+
+
+@endsection diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 90499785..c87c2b96 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -71,7 +71,7 @@ @endif - Switch User + Switch User @yield('content')
diff --git a/resources/views/userselect.blade.php b/resources/views/userselect.blade.php index 98b98fc3..3633003b 100644 --- a/resources/views/userselect.blade.php +++ b/resources/views/userselect.blade.php @@ -2,6 +2,8 @@ @section('content') -here +@foreach($users as $user) + {{ $user->name }} +@endforeach @endsection \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index ee82f37d..6d9503af 100644 --- a/routes/web.php +++ b/routes/web.php @@ -11,6 +11,7 @@ | */ +Route::get('/userselect/{user}', 'Auth\LoginController@setUser')->name('user.set'); Route::get('/userselect', 'Auth\LoginController@index')->name('user.select'); Route::get('/', 'ItemController@dash')->name('dash'); @@ -58,4 +59,7 @@ Route::group([ Route::patch('edit/{id}', 'SettingsController@update'); -}); \ No newline at end of file +}); +Auth::routes(); + +Route::get('/home', 'HomeController@index')->name('home');