add composer-unused, comprehensive docs, and project restructure
- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks - Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists - Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services - Add Microsoft OIDC SSO, API token management, and user lifecycle features - Add swagger-ui vendor integration and OpenAPI spec - Add production Docker setup and bin/ scripts - Update composer dependencies, config, templates, and frontend assets throughout Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -33,7 +33,7 @@ class DepartmentRepository
|
||||
public static function list(): array
|
||||
{
|
||||
$rows = DB::select(
|
||||
'select id, uuid, description, code, cost_center, active, created_by, modified_by, created, modified from departments order by id desc'
|
||||
'select id, uuid, description, tenant_id, code, cost_center, active, created_by, modified_by, created, modified from departments order by id desc'
|
||||
);
|
||||
return self::unwrapList($rows);
|
||||
}
|
||||
@@ -54,6 +54,31 @@ class DepartmentRepository
|
||||
return array_values(array_unique($ids));
|
||||
}
|
||||
|
||||
public static function listActiveIdsByTenantIds(array $tenantIds): array
|
||||
{
|
||||
$tenantIds = array_values(array_unique(array_map('intval', $tenantIds)));
|
||||
$tenantIds = array_filter($tenantIds, static fn ($id) => $id > 0);
|
||||
if (!$tenantIds) {
|
||||
return [];
|
||||
}
|
||||
$placeholders = implode(',', array_fill(0, count($tenantIds), '?'));
|
||||
$rows = DB::select(
|
||||
'select id from departments where active = 1 and tenant_id in (' . $placeholders . ')',
|
||||
...array_map('strval', $tenantIds)
|
||||
);
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
$ids = [];
|
||||
foreach ($rows as $row) {
|
||||
$data = $row['departments'] ?? $row;
|
||||
if (is_array($data) && isset($data['id'])) {
|
||||
$ids[] = (int) $data['id'];
|
||||
}
|
||||
}
|
||||
return array_values(array_unique($ids));
|
||||
}
|
||||
|
||||
public static function listByTenantIds(array $tenantIds): array
|
||||
{
|
||||
$tenantIds = array_values(array_unique(array_map('intval', $tenantIds)));
|
||||
@@ -63,17 +88,16 @@ class DepartmentRepository
|
||||
}
|
||||
$placeholders = implode(',', array_fill(0, count($tenantIds), '?'));
|
||||
$rows = DB::select(
|
||||
'select departments.id, departments.uuid, departments.description, departments.code, departments.cost_center, departments.active, departments.created_by, departments.modified_by, departments.created, departments.modified ' .
|
||||
'from departments departments join tenant_departments td on td.department_id = departments.id ' .
|
||||
'where departments.active = 1 and td.tenant_id in (' . $placeholders . ') ' .
|
||||
'group by departments.id, departments.uuid, departments.description, departments.code, departments.cost_center, departments.active, departments.created_by, departments.modified_by, departments.created, departments.modified ' .
|
||||
'select departments.id, departments.uuid, departments.description, departments.tenant_id, departments.code, departments.cost_center, departments.active, departments.created_by, departments.modified_by, departments.created, departments.modified ' .
|
||||
"from departments departments join tenants t on t.id = departments.tenant_id and t.status = 'active' " .
|
||||
'where departments.active = 1 and departments.tenant_id in (' . $placeholders . ') ' .
|
||||
'order by departments.description asc',
|
||||
...array_map('strval', $tenantIds)
|
||||
);
|
||||
return self::unwrapList($rows);
|
||||
}
|
||||
|
||||
public static function listByIds(array $departmentIds): array
|
||||
public static function listByIds(array $departmentIds, bool $includeInactive = false): array
|
||||
{
|
||||
$departmentIds = array_values(array_unique(array_map('intval', $departmentIds)));
|
||||
$departmentIds = array_filter($departmentIds, static fn ($id) => $id > 0);
|
||||
@@ -81,8 +105,9 @@ class DepartmentRepository
|
||||
return [];
|
||||
}
|
||||
$placeholders = implode(',', array_fill(0, count($departmentIds), '?'));
|
||||
$activeSql = $includeInactive ? '' : 'active = 1 and ';
|
||||
$rows = DB::select(
|
||||
'select id, uuid, description, code, cost_center, active, created_by, modified_by, created, modified from departments where active = 1 and id in (' . $placeholders . ') order by description asc',
|
||||
'select id, uuid, description, tenant_id, code, cost_center, active, created_by, modified_by, created, modified from departments where ' . $activeSql . 'id in (' . $placeholders . ') order by description asc',
|
||||
...array_map('strval', $departmentIds)
|
||||
);
|
||||
return self::unwrapList($rows);
|
||||
@@ -113,44 +138,25 @@ class DepartmentRepository
|
||||
$where,
|
||||
$params,
|
||||
$tenant,
|
||||
"exists (select 1 from tenant_departments td join tenants t on t.id = td.tenant_id and t.status = 'active' where td.department_id = departments.id and t.uuid = ?)"
|
||||
"exists (select 1 from tenants t where t.id = departments.tenant_id and t.status = 'active' and t.uuid = ?)"
|
||||
);
|
||||
|
||||
if (!empty($options['tenantUserId'])) {
|
||||
$tenantUserId = (int) $options['tenantUserId'];
|
||||
if ($tenantUserId > 0) {
|
||||
if (defined('TENANT_SCOPE_STRICT') && TENANT_SCOPE_STRICT) {
|
||||
$where[] = 'exists (select 1 from tenant_departments td ' .
|
||||
"join tenants t on t.id = td.tenant_id and t.status = 'active' " .
|
||||
'join user_tenants ut on ut.tenant_id = td.tenant_id ' .
|
||||
'where ut.user_id = ? and td.department_id = departments.id)';
|
||||
$params[] = (string) $tenantUserId;
|
||||
} else {
|
||||
$where[] = '(not exists (select 1 from tenant_departments td where td.department_id = departments.id) ' .
|
||||
'or exists (select 1 from tenant_departments td ' .
|
||||
"join tenants t on t.id = td.tenant_id and t.status = 'active' " .
|
||||
'join user_tenants ut on ut.tenant_id = td.tenant_id ' .
|
||||
'where ut.user_id = ? and td.department_id = departments.id))';
|
||||
$params[] = (string) $tenantUserId;
|
||||
}
|
||||
$where[] = 'exists (select 1 from user_tenants ut ' .
|
||||
"join tenants t on t.id = ut.tenant_id and t.status = 'active' " .
|
||||
'where ut.user_id = ? and ut.tenant_id = departments.tenant_id)';
|
||||
$params[] = (string) $tenantUserId;
|
||||
}
|
||||
} elseif (array_key_exists('tenantIds', $options)) {
|
||||
$tenantIds = $options['tenantIds'];
|
||||
$tenantIds = is_array($tenantIds) ? array_values(array_unique(array_map('intval', $tenantIds))) : [];
|
||||
if ($tenantIds) {
|
||||
if (defined('TENANT_SCOPE_STRICT') && TENANT_SCOPE_STRICT) {
|
||||
$where[] = 'exists (select 1 from tenant_departments td where td.department_id = departments.id and td.tenant_id in (???))';
|
||||
$params[] = $tenantIds;
|
||||
} else {
|
||||
$where[] = '(not exists (select 1 from tenant_departments td where td.department_id = departments.id) ' .
|
||||
'or exists (select 1 from tenant_departments td where td.department_id = departments.id and td.tenant_id in (???)))';
|
||||
$params[] = $tenantIds;
|
||||
}
|
||||
$where[] = 'departments.tenant_id in (???)';
|
||||
$params[] = $tenantIds;
|
||||
} else {
|
||||
if (defined('TENANT_SCOPE_STRICT') && TENANT_SCOPE_STRICT) {
|
||||
$where[] = '1=0';
|
||||
} else {
|
||||
$where[] = 'not exists (select 1 from tenant_departments td where td.department_id = departments.id)';
|
||||
}
|
||||
$where[] = '1=0';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,7 +164,7 @@ class DepartmentRepository
|
||||
$count = DB::selectValue('select count(*) from departments' . $whereSql, ...$params);
|
||||
$total = $count ? (int) $count : 0;
|
||||
|
||||
$query = 'select id, uuid, description, code, cost_center, active, created_by, modified_by, created, modified from departments' .
|
||||
$query = 'select id, uuid, description, tenant_id, code, cost_center, active, created_by, modified_by, created, modified from departments' .
|
||||
$whereSql .
|
||||
sprintf(' order by `%s` %s limit ? offset ?', $order, $dir);
|
||||
|
||||
@@ -176,48 +182,28 @@ class DepartmentRepository
|
||||
if ($ids) {
|
||||
$placeholders = implode(',', array_fill(0, count($ids), '?'));
|
||||
$labelRows = DB::select(
|
||||
"select td.department_id as department_id, t.description as description from tenant_departments td join tenants t on t.id = td.tenant_id and t.status = 'active' " .
|
||||
'where td.department_id in (' . $placeholders . ') order by t.description asc',
|
||||
'select d.id as department_id, t.description as description from departments d join tenants t on t.id = d.tenant_id ' .
|
||||
'where d.id in (' . $placeholders . ') order by t.description asc',
|
||||
...array_map('strval', $ids)
|
||||
);
|
||||
$collectLabels = static function (array $rows): array {
|
||||
$labelsByDepartment = [];
|
||||
foreach ($rows as $row) {
|
||||
$departmentId = 0;
|
||||
$label = '';
|
||||
if (isset($row['td']) && is_array($row['td'])) {
|
||||
$departmentId = (int) ($row['td']['department_id'] ?? 0);
|
||||
foreach ((array) $labelRows as $row) {
|
||||
$departmentId = 0;
|
||||
$label = '';
|
||||
foreach ((array) $row as $table) {
|
||||
if (!is_array($table)) {
|
||||
continue;
|
||||
}
|
||||
if (isset($row['t']) && is_array($row['t'])) {
|
||||
$label = (string) ($row['t']['description'] ?? '');
|
||||
if ($departmentId === 0 && isset($table['department_id'])) {
|
||||
$departmentId = (int) $table['department_id'];
|
||||
}
|
||||
if ($departmentId === 0 || $label === '') {
|
||||
foreach ($row as $value) {
|
||||
if (!is_array($value)) {
|
||||
continue;
|
||||
}
|
||||
if ($departmentId === 0 && isset($value['department_id'])) {
|
||||
$departmentId = (int) $value['department_id'];
|
||||
}
|
||||
if ($label === '' && isset($value['description'])) {
|
||||
$label = (string) $value['description'];
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($departmentId === 0 && isset($row['department_id'])) {
|
||||
$departmentId = (int) $row['department_id'];
|
||||
}
|
||||
if ($label === '' && isset($row['description'])) {
|
||||
$label = (string) $row['description'];
|
||||
}
|
||||
if ($departmentId > 0 && $label !== '') {
|
||||
$labelsByDepartment[$departmentId] ??= [];
|
||||
$labelsByDepartment[$departmentId][] = $label;
|
||||
if ($label === '' && isset($table['description'])) {
|
||||
$label = (string) $table['description'];
|
||||
}
|
||||
}
|
||||
return $labelsByDepartment;
|
||||
};
|
||||
$tenantLabelsByDepartment = $collectLabels($labelRows);
|
||||
if ($departmentId > 0 && $label !== '') {
|
||||
$tenantLabelsByDepartment[$departmentId] = [$label];
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($list as &$department) {
|
||||
$departmentId = (int) ($department['id'] ?? 0);
|
||||
@@ -234,7 +220,7 @@ class DepartmentRepository
|
||||
public static function find(int $id): ?array
|
||||
{
|
||||
$row = DB::selectOne(
|
||||
'select id, uuid, description, code, cost_center, active, created_by, modified_by, created, modified from departments where id = ? limit 1',
|
||||
'select id, uuid, description, tenant_id, code, cost_center, active, created_by, modified_by, created, modified from departments where id = ? limit 1',
|
||||
(string) $id
|
||||
);
|
||||
return self::unwrap($row);
|
||||
@@ -243,7 +229,7 @@ class DepartmentRepository
|
||||
public static function findByUuid(string $uuid): ?array
|
||||
{
|
||||
$row = DB::selectOne(
|
||||
'select id, uuid, description, code, cost_center, active, created_by, modified_by, created, modified from departments where uuid = ? limit 1',
|
||||
'select id, uuid, description, tenant_id, code, cost_center, active, created_by, modified_by, created, modified from departments where uuid = ? limit 1',
|
||||
$uuid
|
||||
);
|
||||
return self::unwrap($row);
|
||||
@@ -263,20 +249,48 @@ class DepartmentRepository
|
||||
return (int) $count > 0;
|
||||
}
|
||||
|
||||
private static function uuidV4(): string
|
||||
public static function existsByTenantAndDescription(int $tenantId, string $description, int $excludeId = 0): bool
|
||||
{
|
||||
$data = random_bytes(16);
|
||||
$data[6] = chr((ord($data[6]) & 0x0f) | 0x40);
|
||||
$data[8] = chr((ord($data[8]) & 0x3f) | 0x80);
|
||||
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
|
||||
$tenantId = (int) $tenantId;
|
||||
$description = trim($description);
|
||||
if ($tenantId <= 0 || $description === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($excludeId > 0) {
|
||||
$count = DB::selectValue(
|
||||
'select count(*) from departments where tenant_id = ? and lower(description) = lower(?) and id != ?',
|
||||
(string) $tenantId,
|
||||
$description,
|
||||
(string) $excludeId
|
||||
);
|
||||
} else {
|
||||
$count = DB::selectValue(
|
||||
'select count(*) from departments where tenant_id = ? and lower(description) = lower(?)',
|
||||
(string) $tenantId,
|
||||
$description
|
||||
);
|
||||
}
|
||||
|
||||
return (int) $count > 0;
|
||||
}
|
||||
|
||||
public static function countByTenantId(int $tenantId): int
|
||||
{
|
||||
if ($tenantId <= 0) {
|
||||
return 0;
|
||||
}
|
||||
$count = DB::selectValue('select count(*) from departments where tenant_id = ?', (string) $tenantId);
|
||||
return $count ? (int) $count : 0;
|
||||
}
|
||||
|
||||
public static function create(array $data)
|
||||
{
|
||||
return DB::insert(
|
||||
'insert into departments (uuid, description, code, cost_center, active, created_by, created) values (?,?,?,?,?,?,NOW())',
|
||||
$data['uuid'] ?? self::uuidV4(),
|
||||
'insert into departments (uuid, description, tenant_id, code, cost_center, active, created_by, created) values (?,?,?,?,?,?,?,NOW())',
|
||||
$data['uuid'] ?? RepoQuery::uuidV4(),
|
||||
$data['description'],
|
||||
(string) ($data['tenant_id'] ?? 0),
|
||||
$data['code'] ?? null,
|
||||
$data['cost_center'] ?? null,
|
||||
$data['active'] ?? 1,
|
||||
@@ -288,6 +302,7 @@ class DepartmentRepository
|
||||
{
|
||||
$fields = [
|
||||
'description' => $data['description'],
|
||||
'tenant_id' => (string) ($data['tenant_id'] ?? 0),
|
||||
'code' => $data['code'] ?? null,
|
||||
'cost_center' => $data['cost_center'] ?? null,
|
||||
'active' => $data['active'] ?? 1,
|
||||
@@ -309,6 +324,19 @@ class DepartmentRepository
|
||||
return $result !== false;
|
||||
}
|
||||
|
||||
public static function setTenant(int $id, int $tenantId): bool
|
||||
{
|
||||
if ($id <= 0 || $tenantId <= 0) {
|
||||
return false;
|
||||
}
|
||||
$result = DB::update(
|
||||
'update departments set tenant_id = ? where id = ?',
|
||||
(string) $tenantId,
|
||||
(string) $id
|
||||
);
|
||||
return $result !== false;
|
||||
}
|
||||
|
||||
public static function delete(int $id): bool
|
||||
{
|
||||
$result = DB::delete('delete from departments where id = ?', (string) $id);
|
||||
|
||||
Reference in New Issue
Block a user