ldap-users.php 9.1 KB

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