ldap-users.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. require_once '../includes/config.php';
  3. require_once '../includes/database.php';
  4. require_once '../includes/auth.php';
  5. require_once '../includes/ldap.php';
  6. $auth = new Auth();
  7. $auth->requireAuth();
  8. if (!LDAP_ENABLED) {
  9. header('Location: index.php');
  10. exit;
  11. }
  12. $ldap = new LDAPAuth();
  13. $db = Database::getInstance();
  14. $query = $_GET['q'] ?? '';
  15. $users = [];
  16. if ($query) {
  17. $users = $ldap->searchUsers($query);
  18. }
  19. // Handle user import
  20. if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['import_users'])) {
  21. $selectedUsers = $_POST['users'] ?? [];
  22. foreach ($selectedUsers as $username) {
  23. $userInfo = $ldap->getUserInfo($username);
  24. if ($userInfo) {
  25. $auth->createLDAPUser($username, $userInfo);
  26. }
  27. }
  28. header('Location: ldap-users.php?imported=' . count($selectedUsers));
  29. exit;
  30. }
  31. $user = $auth->getUser();
  32. ?>
  33. <!DOCTYPE html>
  34. <html lang="en">
  35. <head>
  36. <meta charset="UTF-8">
  37. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  38. <title>LDAP Users - <?php echo SITE_TITLE; ?></title>
  39. <link rel="stylesheet" href="../css/style.css">
  40. </head>
  41. <body>
  42. <div class="admin-layout">
  43. <header class="admin-header">
  44. <div class="header-content">
  45. <h1><?php echo SITE_TITLE; ?></h1>
  46. <nav class="admin-nav">
  47. <a href="index.php" class="nav-link">Dashboard</a>
  48. <a href="publications.php" class="nav-link">Publications</a>
  49. <a href="categories.php" class="nav-link">Categories</a>
  50. <a href="ldap-users.php" class="nav-link active">LDAP Users</a>
  51. <a href="logout.php" class="nav-link">Logout</a>
  52. </nav>
  53. <div class="user-info">
  54. Welcome, <?php echo htmlspecialchars($user['username']); ?>
  55. </div>
  56. </div>
  57. </header>
  58. <main class="admin-main">
  59. <h2>LDAP Directory Users</h2>
  60. <?php if (isset($_GET['imported'])): ?>
  61. <div class="alert alert-success">
  62. <?php echo (int)$_GET['imported']; ?> users imported successfully.
  63. </div>
  64. <?php endif; ?>
  65. <div class="ldap-search">
  66. <form method="get" class="search-form">
  67. <input type="text" name="q" placeholder="Search LDAP users..."
  68. value="<?php echo htmlspecialchars($query); ?>">
  69. <button type="submit" class="btn btn-primary">Search</button>
  70. </form>
  71. <?php if ($ldap->testConnection()): ?>
  72. <p class="ldap-status status-ok">LDAP connection: OK</p>
  73. <?php else: ?>
  74. <p class="ldap-status status-error">LDAP connection: Failed</p>
  75. <?php endif; ?>
  76. </div>
  77. <?php if ($query && !empty($users)): ?>
  78. <?php if (count($users) > 0): ?>
  79. <form method="post" class="ldap-import-form">
  80. <div class="table-container">
  81. <table class="admin-table">
  82. <thead>
  83. <tr>
  84. <th>
  85. <input type="checkbox" id="selectAll" onchange="toggleAllCheckboxes()">
  86. </th>
  87. <th>Username</th>
  88. <th>Name</th>
  89. <th>Email</th>
  90. <th>Actions</th>
  91. </tr>
  92. </thead>
  93. <tbody>
  94. <?php foreach ($users as $ldapUser): ?>
  95. <?php
  96. // Check if user already exists
  97. $existingUser = $db->fetch("SELECT id FROM users WHERE username = ?", [$ldapUser['username']]);
  98. ?>
  99. <tr class="<?php echo $existingUser ? 'user-exists' : ''; ?>">
  100. <td>
  101. <input type="checkbox" name="users[]"
  102. value="<?php echo htmlspecialchars($ldapUser['username']); ?>"
  103. <?php echo $existingUser ? 'disabled' : ''; ?>>
  104. </td>
  105. <td><?php echo htmlspecialchars($ldapUser['username']); ?></td>
  106. <td><?php echo htmlspecialchars($ldapUser['name'] ?? 'N/A'); ?></td>
  107. <td><?php echo htmlspecialchars($ldapUser['email'] ?? 'N/A'); ?></td>
  108. <td>
  109. <?php if ($existingUser): ?>
  110. <span class="status-badge status-published">Already Imported</span>
  111. <?php else: ?>
  112. <button type="button" class="btn btn-sm"
  113. onclick="importSingleUser('<?php echo htmlspecialchars($ldapUser['username']); ?>')">
  114. Import
  115. </button>
  116. <?php endif; ?>
  117. </td>
  118. </tr>
  119. <?php endforeach; ?>
  120. </tbody>
  121. </table>
  122. </div>
  123. <div class="form-actions">
  124. <button type="submit" name="import_users" class="btn btn-primary">
  125. Import Selected Users
  126. </button>
  127. <a href="index.php" class="btn btn-secondary">Back to Dashboard</a>
  128. </div>
  129. </form>
  130. <?php endif; ?>
  131. <?php elseif ($query): ?>
  132. <p>No users found matching "<?php echo htmlspecialchars($query); ?>"</p>
  133. <?php else: ?>
  134. <p>Enter a search query to find LDAP users available for import.</p>
  135. <?php endif; ?>
  136. </main>
  137. </div>
  138. <script>
  139. function toggleAllCheckboxes() {
  140. const selectAll = document.getElementById('selectAll');
  141. const checkboxes = document.querySelectorAll('input[name="users[]"]:not(:disabled)');
  142. checkboxes.forEach(checkbox => {
  143. checkbox.checked = selectAll.checked;
  144. });
  145. }
  146. function importSingleUser(username) {
  147. const form = document.createElement('form');
  148. form.method = 'post';
  149. form.innerHTML = '<input type="hidden" name="users[]" value="' + username + '"><input type="hidden" name="import_users" value="1">';
  150. document.body.appendChild(form);
  151. form.submit();
  152. }
  153. </script>
  154. <style>
  155. .ldap-search {
  156. background: white;
  157. padding: 1.5rem;
  158. border-radius: 0.5rem;
  159. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  160. margin-bottom: 2rem;
  161. }
  162. .ldap-status {
  163. margin-top: 1rem;
  164. padding: 0.5rem;
  165. border-radius: 0.25rem;
  166. font-weight: 500;
  167. }
  168. .status-ok {
  169. background-color: #d4edda;
  170. color: #155724;
  171. }
  172. .status-error {
  173. background-color: #f8d7da;
  174. color: #721c24;
  175. }
  176. .user-exists {
  177. background-color: #f8f9fa;
  178. opacity: 0.7;
  179. }
  180. .ldap-import-form {
  181. background: white;
  182. padding: 1.5rem;
  183. border-radius: 0.5rem;
  184. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  185. }
  186. </style>
  187. </body>
  188. </html>