ldap-users.php 9.3 KB

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