auth.php 4.1 KB

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