| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- require_once '../includes/config.php';
- require_once '../includes/database.php';
- require_once '../includes/auth.php';
- require_once '../includes/ldap.php';
- $auth = new Auth();
- $auth->requireAuth();
- if (!LDAP_ENABLED) {
- header('Location: index.php');
- exit;
- }
- $ldap = new LDAPAuth();
- $db = Database::getInstance();
- $query = $_GET['q'] ?? '';
- $users = [];
- if ($query) {
- $users = $ldap->searchUsers($query);
- }
- // Handle user import
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['import_users'])) {
- $selectedUsers = $_POST['users'] ?? [];
-
- foreach ($selectedUsers as $username) {
- $userInfo = $ldap->getUserInfo($username);
- if ($userInfo) {
- $auth->createLDAPUser($username, $userInfo);
- }
- }
-
- header('Location: ldap-users.php?imported=' . count($selectedUsers));
- exit;
- }
- $user = $auth->getUser();
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>LDAP Users - <?php echo SITE_TITLE; ?></title>
- <link rel="stylesheet" href="../css/style.css">
- </head>
- <body>
- <div class="admin-layout">
- <header class="admin-header">
- <div class="header-content">
- <h1><?php echo SITE_TITLE; ?></h1>
- <nav class="admin-nav">
- <a href="index.php" class="nav-link">Dashboard</a>
- <a href="publications.php" class="nav-link">Publications</a>
- <a href="categories.php" class="nav-link">Categories</a>
- <a href="ldap-users.php" class="nav-link active">LDAP Users</a>
- <a href="logout.php" class="nav-link">Logout</a>
- </nav>
- <div class="user-info">
- Welcome, <?php echo htmlspecialchars($user['username']); ?>
- </div>
- </div>
- </header>
- <main class="admin-main">
- <h2>LDAP Directory Users</h2>
-
- <?php if (isset($_GET['imported'])): ?>
- <div class="alert alert-success">
- <?php echo (int)$_GET['imported']; ?> users imported successfully.
- </div>
- <?php endif; ?>
- <div class="ldap-search">
- <form method="get" class="search-form">
- <input type="text" name="q" placeholder="Search LDAP users..."
- value="<?php echo htmlspecialchars($query); ?>">
- <button type="submit" class="btn btn-primary">Search</button>
- </form>
-
- <?php if ($ldap->testConnection()): ?>
- <p class="ldap-status status-ok">LDAP connection: OK</p>
- <?php else: ?>
- <p class="ldap-status status-error">LDAP connection: Failed</p>
- <?php endif; ?>
- </div>
- <?php if ($query && !empty($users)): ?>
- <?php if (count($users) > 0): ?>
- <form method="post" class="ldap-import-form">
- <div class="table-container">
- <table class="admin-table">
- <thead>
- <tr>
- <th>
- <input type="checkbox" id="selectAll" onchange="toggleAllCheckboxes()">
- </th>
- <th>Username</th>
- <th>Name</th>
- <th>Email</th>
- <th>Actions</th>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($users as $ldapUser): ?>
- <?php
- // Check if user already exists
- $existingUser = $db->fetch("SELECT id FROM users WHERE username = ?", [$ldapUser['username']]);
- ?>
- <tr class="<?php echo $existingUser ? 'user-exists' : ''; ?>">
- <td>
- <input type="checkbox" name="users[]"
- value="<?php echo htmlspecialchars($ldapUser['username']); ?>"
- <?php echo $existingUser ? 'disabled' : ''; ?>>
- </td>
- <td><?php echo htmlspecialchars($ldapUser['username']); ?></td>
- <td><?php echo htmlspecialchars($ldapUser['name'] ?? 'N/A'); ?></td>
- <td><?php echo htmlspecialchars($ldapUser['email'] ?? 'N/A'); ?></td>
- <td>
- <?php if ($existingUser): ?>
- <span class="status-badge status-published">Already Imported</span>
- <?php else: ?>
- <button type="button" class="btn btn-sm"
- onclick="importSingleUser('<?php echo htmlspecialchars($ldapUser['username']); ?>')">
- Import
- </button>
- <?php endif; ?>
- </td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- </div>
-
- <div class="form-actions">
- <button type="submit" name="import_users" class="btn btn-primary">
- Import Selected Users
- </button>
- <a href="index.php" class="btn btn-secondary">Back to Dashboard</a>
- </div>
- </form>
- <?php endif; ?>
- <?php elseif ($query): ?>
- <p>No users found matching "<?php echo htmlspecialchars($query); ?>"</p>
- <?php else: ?>
- <p>Enter a search query to find LDAP users available for import.</p>
- <?php endif; ?>
- </main>
- </div>
- <script>
- function toggleAllCheckboxes() {
- const selectAll = document.getElementById('selectAll');
- const checkboxes = document.querySelectorAll('input[name="users[]"]:not(:disabled)');
-
- checkboxes.forEach(checkbox => {
- checkbox.checked = selectAll.checked;
- });
- }
-
- function importSingleUser(username) {
- const form = document.createElement('form');
- form.method = 'post';
- form.innerHTML = '<input type="hidden" name="users[]" value="' + username + '"><input type="hidden" name="import_users" value="1">';
- document.body.appendChild(form);
- form.submit();
- }
- </script>
- <style>
- .ldap-search {
- background: white;
- padding: 1.5rem;
- border-radius: 0.5rem;
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
- margin-bottom: 2rem;
- }
-
- .ldap-status {
- margin-top: 1rem;
- padding: 0.5rem;
- border-radius: 0.25rem;
- font-weight: 500;
- }
-
- .status-ok {
- background-color: #d4edda;
- color: #155724;
- }
-
- .status-error {
- background-color: #f8d7da;
- color: #721c24;
- }
-
- .user-exists {
- background-color: #f8f9fa;
- opacity: 0.7;
- }
-
- .ldap-import-form {
- background: white;
- padding: 1.5rem;
- border-radius: 0.5rem;
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
- }
- </style>
- </body>
- </html>
|