File: /home/muratemr/theotto.tr/admin/users.php
<?php
/**
* admin/users.php
* Kullanıcı Yönetimi (sadece admin)
*/
declare(strict_types=1);
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/csrf.php';
require_once __DIR__ . '/../includes/functions.php';
require_permission('users');
$page_title = 'Kullanıcı Yönetimi';
$active_menu = 'users';
$modules = [
'dashboard' => 'Dashboard',
'reservations' => 'Rezervasyonlar',
'customers' => 'Müşteri CRM',
'customer_tags' => 'Müşteri Etiketleri',
'suppliers' => 'Tedarikçiler',
'daily_orders' => 'Günlük Sipariş',
'staff' => 'Personel Takip',
'settings' => 'Site Ayarları',
'users' => 'Kullanıcı Yönetimi',
];
$users = db_rows("SELECT * FROM users ORDER BY role, full_name");
require_once __DIR__ . '/../includes/admin_layout.php';
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h4 class="mb-0"><i class="bi bi-person-gear me-2 text-warning"></i>Kullanıcı Yönetimi</h4>
<button class="btn btn-warning btn-sm" onclick="openUserModal()">
<i class="bi bi-plus-lg me-1"></i>Yeni Kullanıcı
</button>
</div>
<div class="card">
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-dark table-hover mb-0">
<thead>
<tr>
<th>Ad Soyad</th>
<th>Kullanıcı Adı</th>
<th>Rol</th>
<th>Durum</th>
<th>Kayıt Tarihi</th>
<th>İşlemler</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $u): ?>
<tr>
<td><?= e($u['full_name']) ?></td>
<td><?= e($u['username']) ?></td>
<td>
<?php if ($u['role'] === 'admin'): ?>
<span class="badge bg-warning text-dark">Admin</span>
<?php else: ?>
<span class="badge bg-info text-dark">Staff</span>
<?php endif; ?>
</td>
<td>
<?php if ($u['is_active']): ?>
<span class="badge bg-success">Aktif</span>
<?php else: ?>
<span class="badge bg-secondary">Pasif</span>
<?php endif; ?>
</td>
<td><?= format_date($u['created_at']) ?></td>
<td>
<a href="user_edit.php?id=<?= $u['id'] ?>"
class="btn btn-outline-warning btn-sm"
title="Düzenle">
<i class="bi bi-pencil"></i>
</a>
<?php if ($u['id'] != current_user()['id']): ?>
<button class="btn btn-outline-danger btn-sm"
onclick="deleteUser(<?= $u['id'] ?>, '<?= e($u['full_name']) ?>')"
title="Sil">
<i class="bi bi-trash"></i>
</button>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- Kullanıcı Ekle/Düzenle Modal -->
<div class="modal fade" id="userModal" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content bg-dark">
<div class="modal-header border-secondary">
<h5 class="modal-title" id="userModalTitle">Kullanıcı Ekle</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<input type="hidden" id="userId">
<div class="row g-3">
<div class="col-md-6">
<label class="form-label">Ad Soyad <span class="text-danger">*</span></label>
<input type="text" class="form-control" id="userFullName">
</div>
<div class="col-md-6">
<label class="form-label">Kullanıcı Adı <span class="text-danger">*</span></label>
<input type="text" class="form-control" id="userUsername">
</div>
<div class="col-md-6">
<label class="form-label">Şifre <span class="text-danger" id="passRequired">*</span>
<small class="text-muted" id="passOptional" style="display:none">(boş bırakılırsa değişmez)</small>
</label>
<input type="password" class="form-control" id="userPassword" autocomplete="new-password">
<div class="form-text">En az 8 karakter.</div>
</div>
<div class="col-md-3">
<label class="form-label">Rol</label>
<select class="form-select" id="userRole" onchange="togglePermissions()">
<option value="staff">Staff</option>
<option value="admin">Admin</option>
</select>
</div>
<div class="col-md-3 d-flex align-items-end pb-2">
<div class="form-check form-switch">
<input type="checkbox" class="form-check-input" id="userIsActive" checked>
<label class="form-check-label" for="userIsActive">Aktif</label>
</div>
</div>
</div>
<!-- Modül Yetkileri (sadece staff) -->
<div id="permissionsSection" class="mt-3">
<hr class="border-secondary">
<h6 class="text-warning mb-3"><i class="bi bi-shield-check me-2"></i>Modül Yetkileri</h6>
<div class="row g-2">
<?php foreach ($modules as $key => $label): ?>
<div class="col-6 col-md-4">
<div class="form-check">
<input type="checkbox" class="form-check-input perm-check"
id="perm_<?= $key ?>" value="<?= $key ?>">
<label class="form-check-label" for="perm_<?= $key ?>"><?= $label ?></label>
</div>
</div>
<?php endforeach; ?>
</div>
<div class="mt-2">
<button class="btn btn-outline-secondary btn-sm" onclick="checkAllPerms(true)">Tümünü Seç</button>
<button class="btn btn-outline-secondary btn-sm ms-1" onclick="checkAllPerms(false)">Tümünü Kaldır</button>
</div>
</div>
</div>
<div class="modal-footer border-secondary">
<button class="btn btn-secondary" data-bs-dismiss="modal">İptal</button>
<button class="btn btn-warning" onclick="saveUser()">
<i class="bi bi-check-lg me-1"></i>Kaydet
</button>
</div>
</div>
</div>
</div>
<script>
const MODULES = <?= json_encode(array_keys($modules)) ?>;
const ME_ID = <?= (int)current_user()['id'] ?>;
function openUserModal(id = null) {
// Reset
document.getElementById('userId').value = '';
document.getElementById('userFullName').value = '';
document.getElementById('userUsername').value = '';
document.getElementById('userPassword').value = '';
document.getElementById('userRole').value = 'staff';
document.getElementById('userIsActive').checked = true;
document.getElementById('passRequired').style.display = '';
document.getElementById('passOptional').style.display = 'none';
MODULES.forEach(m => { const el = document.getElementById('perm_' + m); if (el) el.checked = false; });
document.getElementById('userModalTitle').textContent = id ? 'Kullanıcı Düzenle' : 'Kullanıcı Ekle';
togglePermissions();
if (id) {
// id'yi hemen set et, async yüklenene kadar kaydet butonunu disable et
document.getElementById('userId').value = id;
document.getElementById('passRequired').style.display = 'none';
document.getElementById('passOptional').style.display = '';
const saveBtn = document.querySelector('#userModal .btn-warning');
if (saveBtn) saveBtn.disabled = true;
apiPost('/api/admin/users.php', {action:'get', id}).then(r => {
if (r.success) {
const u = r.data.user;
document.getElementById('userId').value = u.id;
document.getElementById('userFullName').value = u.full_name;
document.getElementById('userUsername').value = u.username;
document.getElementById('userRole').value = u.role;
document.getElementById('userIsActive').checked = u.is_active == 1;
(r.data.permissions || []).forEach(m => {
const el = document.getElementById('perm_' + m);
if (el) el.checked = true;
});
togglePermissions();
}
}).finally(() => {
const saveBtn = document.querySelector('#userModal .btn-warning');
if (saveBtn) saveBtn.disabled = false;
});
}
openModal('userModal');
}
function togglePermissions() {
const isAdmin = document.getElementById('userRole').value === 'admin';
document.getElementById('permissionsSection').style.display = isAdmin ? 'none' : '';
}
function checkAllPerms(val) {
MODULES.forEach(m => {
const el = document.getElementById('perm_' + m);
if (el) el.checked = val;
});
}
function saveUser() {
const id = document.getElementById('userId').value;
const fullName = document.getElementById('userFullName').value.trim();
const username = document.getElementById('userUsername').value.trim();
const password = document.getElementById('userPassword').value;
const role = document.getElementById('userRole').value;
const isActive = document.getElementById('userIsActive').checked ? 1 : 0;
if (!fullName) { showToast('Ad Soyad zorunlu.', 'error'); return; }
if (!username) { showToast('Kullanıcı adı zorunlu.', 'error'); return; }
if (!id && !password) { showToast('Şifre zorunlu.', 'error'); return; }
if (password && password.length < 8) { showToast('Şifre en az 8 karakter olmalı.', 'error'); return; }
const perms = MODULES.filter(m => {
const el = document.getElementById('perm_' + m);
return el && el.checked;
});
// Butonu disable et - çift gönderim engelle
const saveBtn = document.querySelector('#userModal .btn-warning:last-of-type');
if (saveBtn) { saveBtn.disabled = true; saveBtn.textContent = 'Kaydediliyor...'; }
apiPost('/api/admin/users.php', {
action: id ? 'update' : 'create',
id, full_name: fullName, username, password,
role, is_active: isActive,
permissions: perms.join(',')
}).then(r => {
if (r.success) {
showToast(r.message, 'success');
closeModal('userModal');
setTimeout(() => location.reload(), 600);
} else {
showToast(r.message, 'error');
}
}).finally(() => {
if (saveBtn) { saveBtn.disabled = false; saveBtn.innerHTML = '<i class="bi bi-check-lg me-1"></i>Kaydet'; }
});
}
function deleteUser(id, name) {
if (id == ME_ID) { showToast('Kendinizi silemezsiniz.', 'error'); return; }
confirmAction(`"${name}" kullanıcısı silinsin mi?`, () => {
apiPost('/api/admin/users.php', {action:'delete', id}).then(r => {
if (r.success) { showToast(r.message, 'success'); setTimeout(()=>location.reload(), 700); }
else showToast(r.message, 'error');
});
});
}
</script>
<?php require_once __DIR__ . '/../includes/admin_layout_end.php'; ?>