auth.php 800 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * lib/auth.php
  4. *
  5. * Authentication and authorization functions.
  6. */
  7. /**
  8. * Check if user is logged in
  9. */
  10. function isLoggedIn(): bool
  11. {
  12. return isset($_SESSION['user_id']) && !empty($_SESSION['user_id']);
  13. }
  14. /**
  15. * Get current user ID
  16. */
  17. function getCurrentUserId(): ?int
  18. {
  19. return $_SESSION['user_id'] ?? null;
  20. }
  21. /**
  22. * Require user to be logged in, redirect if not
  23. */
  24. function requireLogin(): void
  25. {
  26. if (!isLoggedIn()) {
  27. header('Location: /login/login.php');
  28. exit;
  29. }
  30. }
  31. /**
  32. * Check if user has specific role/permission
  33. */
  34. function hasPermission(string $permission): bool
  35. {
  36. if (!isLoggedIn()) {
  37. return false;
  38. }
  39. // TODO: Implement proper role-based permissions
  40. // For now, just check if user is logged in
  41. return true;
  42. }
  43. ?>