auth.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Authentication Class
  4. * Handles user login, logout, and session management
  5. */
  6. require_once 'database.php';
  7. class Auth {
  8. private $db;
  9. private $ldap;
  10. public function __construct() {
  11. $this->db = Database::getInstance();
  12. if (LDAP_ENABLED) {
  13. require_once 'ldap.php';
  14. $this->ldap = new LDAPAuth();
  15. }
  16. }
  17. public function login($username, $password) {
  18. // First, try to find user in database
  19. $sql = "SELECT * FROM users WHERE username = ?";
  20. $user = $this->db->fetch($sql, [$username]);
  21. if (!$user) {
  22. return false;
  23. }
  24. // Check authentication based on user's auth type
  25. if ($user['auth_type'] === 'ldap' && LDAP_ENABLED) {
  26. // LDAP authentication
  27. if ($this->ldap->authenticate($username, $password)) {
  28. // Get LDAP user info and update database
  29. $ldapUserInfo = $this->ldap->getUserInfo($username);
  30. if ($ldapUserInfo) {
  31. $this->updateUserFromLDAP($user['id'], $ldapUserInfo);
  32. }
  33. $this->createSession($user);
  34. $this->updateLastLogin($user['id']);
  35. return true;
  36. }
  37. } elseif ($user['auth_type'] === 'local') {
  38. // Local authentication
  39. if ($user['password'] && password_verify($password, $user['password'])) {
  40. $this->createSession($user);
  41. $this->updateLastLogin($user['id']);
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. public function logout() {
  48. session_destroy();
  49. unset($_SESSION);
  50. }
  51. public function isLoggedIn() {
  52. return isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true;
  53. }
  54. public function requireAuth() {
  55. if (!$this->isLoggedIn()) {
  56. header('Location: login.php');
  57. exit;
  58. }
  59. }
  60. public function getUser() {
  61. if ($this->isLoggedIn()) {
  62. return [
  63. 'id' => $_SESSION['user_id'],
  64. 'username' => $_SESSION['username'],
  65. 'role' => $_SESSION['role']
  66. ];
  67. }
  68. return null;
  69. }
  70. public function isAdmin() {
  71. return $this->isLoggedIn() && $_SESSION['role'] === 'admin';
  72. }
  73. private function createSession($user) {
  74. $_SESSION['user_id'] = $user['id'];
  75. $_SESSION['username'] = $user['username'];
  76. $_SESSION['role'] = $user['role'];
  77. $_SESSION['auth_type'] = $user['auth_type'];
  78. $_SESSION['logged_in'] = true;
  79. }
  80. private function updateLastLogin($userId) {
  81. $sql = "UPDATE users SET last_login = NOW() WHERE id = ?";
  82. $this->db->query($sql, [$userId]);
  83. }
  84. private function updateUserFromLDAP($userId, $ldapUserInfo) {
  85. $updateData = [];
  86. if (isset($ldapUserInfo['email'])) {
  87. $updateData['email'] = $ldapUserInfo['email'];
  88. }
  89. if (isset($ldapUserInfo['ldap_dn'])) {
  90. $updateData['ldap_dn'] = $ldapUserInfo['ldap_dn'];
  91. }
  92. if (!empty($updateData)) {
  93. $this->db->update('users', $updateData, 'id = ?', [$userId]);
  94. }
  95. }
  96. public function createLDAPUser($username, $ldapUserInfo) {
  97. // Check if user already exists
  98. $sql = "SELECT id FROM users WHERE username = ?";
  99. $existing = $this->db->fetch($sql, [$username]);
  100. if ($existing) {
  101. return $existing['id'];
  102. }
  103. // Create new user from LDAP
  104. $userData = [
  105. 'username' => $username,
  106. 'email' => $ldapUserInfo['email'] ?? '',
  107. 'role' => 'editor', // Default role for LDAP users
  108. 'auth_type' => 'ldap',
  109. 'ldap_dn' => $ldapUserInfo['ldap_dn'] ?? ''
  110. ];
  111. return $this->db->insert('users', $userData);
  112. }
  113. public function getAuthType() {
  114. return $_SESSION['auth_type'] ?? 'local';
  115. }
  116. }
  117. // Initialize session
  118. if (session_status() === PHP_SESSION_NONE) {
  119. session_start();
  120. }