diff --git a/app/Http/Controllers/UserSwitchController.php b/app/Http/Controllers/UserSwitchController.php new file mode 100644 index 0000000..9d34630 --- /dev/null +++ b/app/Http/Controllers/UserSwitchController.php @@ -0,0 +1,75 @@ +middleware('auth'); + } + + /** + * Is the user authorised to switch to another user + * + * @param User $o + * @return bool + */ + private function switch_authorised(User $o): bool + { + return Auth::user()->admin; + } + + /** + * Are we currently in a switch session + * + * @return bool + */ + private function switch_session(): bool + { + return ! Session::get('orig_user'); + } + + /** + * Switch the user to another user + * + * @param User $o + * @return RedirectResponse + */ + public function user_switch_start(User $o): RedirectResponse + { + if ($this->switch_session() AND $this->switch_authorised($o)) { + Session::put('orig_user',Auth::id()); + Auth::login($o); + + } else { + abort(404,'Not found'); + } + + return Redirect::to(self::redirect); + } + + /** + * Return the user back to the original user + * + * @return RedirectResponse + */ + public function user_switch_stop(): RedirectResponse + { + if ($id = Session::pull('orig_user')) { + $uo = User::find($id); + Auth::login($uo); + } + + return Redirect::to(self::redirect); + } +} \ No newline at end of file diff --git a/app/Models/User.php b/app/Models/User.php index b88d6c8..55dc333 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -8,6 +8,8 @@ use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Passport\HasApiTokens; +use App\Traits\UserSwitch; + /** * Class User * @@ -24,7 +26,7 @@ use Laravel\Passport\HasApiTokens; */ class User extends Authenticatable implements MustVerifyEmail { - use HasFactory,Notifiable,HasApiTokens; + use HasFactory,Notifiable,HasApiTokens,UserSwitch; /** * The attributes that are mass assignable. diff --git a/app/Traits/UserSwitch.php b/app/Traits/UserSwitch.php new file mode 100644 index 0000000..a6dc00f --- /dev/null +++ b/app/Traits/UserSwitch.php @@ -0,0 +1,31 @@ +admin ?? FALSE; + } +} \ No newline at end of file diff --git a/public/oldschool/css/main.css b/public/oldschool/css/main.css index 7d1287a..a64bd81 100644 --- a/public/oldschool/css/main.css +++ b/public/oldschool/css/main.css @@ -189,6 +189,7 @@ sup.success:after { display:inline-block; padding:8px 0 7px } +#nav-menu ul li.switched, #nav-menu ul li a:active, #nav-menu ul li a:hover, #nav-menu ul li a:focus { diff --git a/resources/views/layouts/partials/topmenu.blade.php b/resources/views/layouts/partials/topmenu.blade.php index 6de5455..dfeb503 100644 --- a/resources/views/layouts/partials/topmenu.blade.php +++ b/resources/views/layouts/partials/topmenu.blade.php @@ -15,8 +15,15 @@