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') +