feat: admin näkee oman yrityksen käyttäjät
- Käyttäjät-nappi näkyy nyt myös admin-roolille - Admin näkee/hallitsee vain oman yrityksensä käyttäjiä - Admin voi luoda admin/user-rooleja (ei superadmin) - Admin ei voi poistaa/muokata superadmineja - Superadmin-vaihtoehto piilotettu rooli-dropdownista adminilta - Yritysoikeudet-osio piilotettu adminilta (lisätään automaattisesti) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
51
api.php
51
api.php
@@ -1068,8 +1068,16 @@ switch ($action) {
|
||||
|
||||
// ---------- USERS ----------
|
||||
case 'users':
|
||||
requireSuperAdmin();
|
||||
requireAdmin();
|
||||
$users = dbLoadUsers();
|
||||
// Admin näkee vain oman yrityksensä käyttäjät, superadmin näkee kaikki
|
||||
$role = $_SESSION['role'] ?? '';
|
||||
$companyId = $_SESSION['company_id'] ?? '';
|
||||
if ($role !== 'superadmin') {
|
||||
$users = array_filter($users, function($u) use ($companyId) {
|
||||
return in_array($companyId, $u['companies'] ?? []);
|
||||
});
|
||||
}
|
||||
$safe = array_map(function($u) {
|
||||
unset($u['password_hash']);
|
||||
return $u;
|
||||
@@ -1078,14 +1086,15 @@ switch ($action) {
|
||||
break;
|
||||
|
||||
case 'user_create':
|
||||
requireSuperAdmin();
|
||||
requireAdmin();
|
||||
if ($method !== 'POST') break;
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$username = trim($input['username'] ?? '');
|
||||
$password = $input['password'] ?? '';
|
||||
$nimi = trim($input['nimi'] ?? '');
|
||||
$email = trim($input['email'] ?? '');
|
||||
$validRoles = ['superadmin', 'admin', 'user'];
|
||||
$isSA = ($_SESSION['role'] ?? '') === 'superadmin';
|
||||
$validRoles = $isSA ? ['superadmin', 'admin', 'user'] : ['admin', 'user'];
|
||||
$role = in_array($input['role'] ?? '', $validRoles) ? $input['role'] : 'user';
|
||||
if (empty($username) || empty($password)) {
|
||||
http_response_code(400);
|
||||
@@ -1104,6 +1113,11 @@ switch ($action) {
|
||||
break;
|
||||
}
|
||||
$companies = $input['companies'] ?? [];
|
||||
// Admin voi lisätä käyttäjiä vain omaan yritykseensä
|
||||
if (!$isSA) {
|
||||
$myCompanyId = $_SESSION['company_id'] ?? '';
|
||||
$companies = [$myCompanyId];
|
||||
}
|
||||
// Validoi yritys-IDt
|
||||
$allCompanies = dbLoadCompanies();
|
||||
$validIds = array_column($allCompanies, 'id');
|
||||
@@ -1133,7 +1147,7 @@ switch ($action) {
|
||||
break;
|
||||
|
||||
case 'user_update':
|
||||
requireSuperAdmin();
|
||||
requireAdmin();
|
||||
if ($method !== 'POST') break;
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$id = $input['id'] ?? '';
|
||||
@@ -1143,11 +1157,24 @@ switch ($action) {
|
||||
echo json_encode(['error' => 'Käyttäjää ei löydy']);
|
||||
break;
|
||||
}
|
||||
$isSA = ($_SESSION['role'] ?? '') === 'superadmin';
|
||||
$myCompanyId = $_SESSION['company_id'] ?? '';
|
||||
// Admin voi muokata vain oman yrityksensä käyttäjiä
|
||||
if (!$isSA && !in_array($myCompanyId, $u['companies'] ?? [])) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['error' => 'Ei oikeutta muokata tätä käyttäjää']);
|
||||
break;
|
||||
}
|
||||
if (isset($input['nimi'])) $u['nimi'] = trim($input['nimi']);
|
||||
if (isset($input['email'])) $u['email'] = trim($input['email']);
|
||||
if (isset($input['role'])) {
|
||||
$validRoles = ['superadmin', 'admin', 'user'];
|
||||
$u['role'] = in_array($input['role'], $validRoles) ? $input['role'] : 'user';
|
||||
$validRoles = $isSA ? ['superadmin', 'admin', 'user'] : ['admin', 'user'];
|
||||
// Admin ei voi muuttaa superadminia
|
||||
if (!$isSA && ($u['role'] === 'superadmin')) {
|
||||
// Älä muuta roolia
|
||||
} else {
|
||||
$u['role'] = in_array($input['role'], $validRoles) ? $input['role'] : 'user';
|
||||
}
|
||||
}
|
||||
if (isset($input['companies'])) {
|
||||
$allCompanies = dbLoadCompanies();
|
||||
@@ -1183,7 +1210,7 @@ switch ($action) {
|
||||
break;
|
||||
|
||||
case 'user_delete':
|
||||
requireSuperAdmin();
|
||||
requireAdmin();
|
||||
if ($method !== 'POST') break;
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$id = $input['id'] ?? '';
|
||||
@@ -1193,6 +1220,16 @@ switch ($action) {
|
||||
break;
|
||||
}
|
||||
$deleted = dbGetUser($id);
|
||||
$isSA = ($_SESSION['role'] ?? '') === 'superadmin';
|
||||
$myCompanyId = $_SESSION['company_id'] ?? '';
|
||||
// Admin ei voi poistaa superadmineja eikä toisen yrityksen käyttäjiä
|
||||
if (!$isSA) {
|
||||
if ($deleted && ($deleted['role'] === 'superadmin' || !in_array($myCompanyId, $deleted['companies'] ?? []))) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['error' => 'Ei oikeutta poistaa tätä käyttäjää']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
dbDeleteUser($id);
|
||||
$companyId = $_SESSION['company_id'] ?? '';
|
||||
if ($deleted) dbAddLog($companyId, currentUser(), 'user_delete', '', '', "Poisti käyttäjän: {$deleted['username']}");
|
||||
|
||||
Reference in New Issue
Block a user