ldap-users.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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><a href="/index.php"><?php echo SITE_TITLE; ?></a></h1>
  46. <nav class="admin-nav">
  47. <a href="index.php" class="nav-link"><?php echo t('admin_nav_dashboard'); ?></a>
  48. <a href="publications.php" class="nav-link"><?php echo t('admin_nav_publications'); ?></a>
  49. <a href="categories.php" class="nav-link"><?php echo t('admin_nav_categories'); ?></a>
  50. <a href="users.php" class="nav-link"><?php echo t('manage_users'); ?></a>
  51. <?php if (LDAP_ENABLED): ?>
  52. <a href="ldap-users.php" class="nav-link active"><?php echo t('admin_nav_ldap_users'); ?></a>
  53. <?php endif; ?>
  54. <a href="logout.php" class="nav-link"><?php echo t('admin_nav_logout'); ?></a>
  55. </nav>
  56. <div class="user-info">
  57. Welcome, <?php echo htmlspecialchars($user['username']); ?>
  58. </div>
  59. </div>
  60. </header>
  61. <main class="admin-main">
  62. <h2>LDAP Directory Users</h2>
  63. <?php if (isset($_GET['imported'])): ?>
  64. <div class="alert alert-success">
  65. <?php echo (int)$_GET['imported']; ?> users imported successfully.
  66. </div>
  67. <?php endif; ?>
  68. <div class="ldap-search">
  69. <form method="get" class="search-form">
  70. <input type="text" name="q" placeholder="Search LDAP users..."
  71. value="<?php echo htmlspecialchars($query); ?>">
  72. <button type="submit" class="btn btn-primary">Search</button>
  73. </form>
  74. <?php if ($ldap->testConnection()): ?>
  75. <p class="ldap-status status-ok">LDAP connection: OK</p>
  76. <?php else: ?>
  77. <p class="ldap-status status-error">LDAP connection: Failed</p>
  78. <?php endif; ?>
  79. </div>
  80. <?php if ($query && !empty($users)): ?>
  81. <?php if (count($users) > 0): ?>
  82. <form method="post" class="ldap-import-form">
  83. <div class="table-container">
  84. <table class="admin-table">
  85. <thead>
  86. <tr>
  87. <th>
  88. <input type="checkbox" id="selectAll" onchange="toggleAllCheckboxes()">
  89. </th>
  90. <th>Username</th>
  91. <th>Name</th>
  92. <th>Email</th>
  93. <th>Actions</th>
  94. </tr>
  95. </thead>
  96. <tbody>
  97. <?php foreach ($users as $ldapUser): ?>
  98. <?php
  99. // Check if user already exists
  100. $existingUser = $db->fetch("SELECT id FROM users WHERE username = ?", [$ldapUser['username']]);
  101. ?>
  102. <tr class="<?php echo $existingUser ? 'user-exists' : ''; ?>">
  103. <td>
  104. <input type="checkbox" name="users[]"
  105. value="<?php echo htmlspecialchars($ldapUser['username']); ?>"
  106. <?php echo $existingUser ? 'disabled' : ''; ?>>
  107. </td>
  108. <td><?php echo htmlspecialchars($ldapUser['username']); ?></td>
  109. <td><?php echo htmlspecialchars($ldapUser['name'] ?? 'N/A'); ?></td>
  110. <td><?php echo htmlspecialchars($ldapUser['email'] ?? 'N/A'); ?></td>
  111. <td>
  112. <?php if ($existingUser): ?>
  113. <span class="status-badge status-published">Already Imported</span>
  114. <?php else: ?>
  115. <button type="button" class="btn btn-sm"
  116. onclick="importSingleUser('<?php echo htmlspecialchars($ldapUser['username']); ?>')">
  117. Import
  118. </button>
  119. <?php endif; ?>
  120. </td>
  121. </tr>
  122. <?php endforeach; ?>
  123. </tbody>
  124. </table>
  125. </div>
  126. <div class="form-actions">
  127. <button type="submit" name="import_users" class="btn btn-primary">
  128. Import Selected Users
  129. </button>
  130. <a href="index.php" class="btn btn-secondary">Back to Dashboard</a>
  131. </div>
  132. </form>
  133. <?php endif; ?>
  134. <?php elseif ($query): ?>
  135. <p>No users found matching "<?php echo htmlspecialchars($query); ?>"</p>
  136. <?php else: ?>
  137. <p>Enter a search query to find LDAP users available for import.</p>
  138. <?php endif; ?>
  139. </main>
  140. </div>
  141. <script>
  142. function toggleAllCheckboxes() {
  143. const selectAll = document.getElementById('selectAll');
  144. const checkboxes = document.querySelectorAll('input[name="users[]"]:not(:disabled)');
  145. checkboxes.forEach(checkbox => {
  146. checkbox.checked = selectAll.checked;
  147. });
  148. }
  149. function importSingleUser(username) {
  150. const form = document.createElement('form');
  151. form.method = 'post';
  152. form.innerHTML = '<input type="hidden" name="users[]" value="' + username + '"><input type="hidden" name="import_users" value="1">';
  153. document.body.appendChild(form);
  154. form.submit();
  155. }
  156. </script>
  157. <style>
  158. .ldap-search {
  159. background: white;
  160. padding: 1.5rem;
  161. border-radius: 0.5rem;
  162. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  163. margin-bottom: 2rem;
  164. }
  165. .ldap-status {
  166. margin-top: 1rem;
  167. padding: 0.5rem;
  168. border-radius: 0.25rem;
  169. font-weight: 500;
  170. }
  171. .status-ok {
  172. background-color: #d4edda;
  173. color: #155724;
  174. }
  175. .status-error {
  176. background-color: #f8d7da;
  177. color: #721c24;
  178. }
  179. .user-exists {
  180. background-color: #f8f9fa;
  181. opacity: 0.7;
  182. }
  183. .ldap-import-form {
  184. background: white;
  185. padding: 1.5rem;
  186. border-radius: 0.5rem;
  187. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  188. }
  189. </style>
  190. </body>
  191. </html>