*/
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 = auth()->user();
+ if (!$user) $user = User::getDefault();
- // Share variables with controllers
- $this->currentUser = $user;
- $this->signedIn = auth()->check();
+ // Share variables with views
+ view()->share('signedIn', auth()->check());
+ view()->share('currentUser', $user);
+
+ // Share variables with controllers
+ $this->currentUser = $user;
+ $this->signedIn = auth()->check();
+
+ return $next($request);
+ });
}
/**
protected function showPermissionError()
{
Session::flash('error', trans('errors.permission'));
- $response = request()->wantsJson() ? response()->json(['error' => trans('errors.permissionJson')], 403) : redirect('/', 403);
+ $response = request()->wantsJson() ? response()->json(['error' => trans('errors.permissionJson')], 403) : redirect('/');
throw new HttpResponseException($response);
}
*/
protected function checkOwnablePermission($permission, Ownable $ownable)
{
- $permissionBaseName = strtolower($permission) . '-';
- if (userCan($permissionBaseName . 'all')) return true;
- if (userCan($permissionBaseName . 'own') && $ownable->createdBy->id === $this->currentUser->id) return true;
- $this->showPermissionError();
+ if (userCan($permission, $ownable)) return true;
+ return $this->showPermissionError();
}
/**
return true;
}
+ /**
+ * Send back a json error message.
+ * @param string $messageText
+ * @param int $statusCode
+ * @return mixed
+ */
+ protected function jsonError($messageText = "", $statusCode = 500)
+ {
+ return response()->json(['message' => $messageText], $statusCode);
+ }
+
}