Controller.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\User;
  4. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  5. use Illuminate\Foundation\Bus\DispatchesJobs;
  6. use Illuminate\Foundation\Validation\ValidatesRequests;
  7. use Illuminate\Routing\Controller as BaseController;
  8. use Illuminate\Support\Facades\Auth;
  9. class Controller extends BaseController
  10. {
  11. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  12. /**
  13. * Check if user has permissions
  14. * Abort 403 if the user doesn't have the required permission
  15. *
  16. * @param string $permission
  17. * @return void
  18. */
  19. public function checkPermission(string $permission)
  20. {
  21. /** @var User $user */
  22. $user = Auth::user();
  23. if (!$user->can($permission)) {
  24. abort(403, __('User does not have the right permissions.'));
  25. }
  26. }
  27. /**
  28. * Check if user has permissions
  29. *
  30. * @param string $permission
  31. * @return bool
  32. */
  33. public function can(string $permission): bool
  34. {
  35. /** @var User $user */
  36. $user = Auth::user();
  37. return $user->can($permission);
  38. }
  39. }