*/
public function __construct()
{
- // Get a user instance for the current user
- $user = auth()->user();
- if (!$user) $user = User::getDefault();
+ $this->middleware(function ($request, $next) {
- // Share variables with views
- view()->share('signedIn', auth()->check());
- view()->share('currentUser', $user);
+ // Get a user instance for the current user
+ $user = user();
- // Share variables with controllers
- $this->currentUser = $user;
- $this->signedIn = auth()->check();
+ // Share variables with controllers
+ $this->currentUser = $user;
+ $this->signedIn = auth()->check();
+
+ // Share variables with views
+ view()->share('signedIn', $this->signedIn);
+ view()->share('currentUser', $user);
+
+ return $next($request);
+ });
}
/**
*/
protected function showPermissionError()
{
- Session::flash('error', trans('errors.permission'));
- $response = request()->wantsJson() ? response()->json(['error' => trans('errors.permissionJson')], 403) : redirect('/');
+ if (request()->wantsJson()) {
+ $response = response()->json(['error' => trans('errors.permissionJson')], 403);
+ } else {
+ $response = redirect('/');
+ session()->flash('error', trans('errors.permission'));
+ }
+
throw new HttpResponseException($response);
}
*/
protected function checkPermission($permissionName)
{
- if (!$this->currentUser || !$this->currentUser->can($permissionName)) {
+ if (!user() || !user()->can($permissionName)) {
$this->showPermissionError();
}
return true;