all(); $authorizationService = app(\MintyPHP\Service\Access\AuthorizationService::class); $decision = $authorizationService->authorize(UserAuthorizationPolicy::ABILITY_ADMIN_USERS_VIEW, [ 'actor_user_id' => (int) ($session['user']['id'] ?? 0), ]); if (!$decision->isAllowed()) { if (Request::wantsJson()) { http_response_code($decision->status()); Router::json(['error' => $decision->error()]); return; } Router::redirect('error/forbidden?url=' . urlencode(Request::pathWithQuery())); return; } $request = requestInput(); $currentUserId = (int) ($session['user']['id'] ?? 0); $search = $request->queryString('search'); $order = (string) $request->query('order', 'id'); $dir = strtolower((string) $request->query('dir', 'desc')) === 'asc' ? 'asc' : 'desc'; $active = (string) $request->query('active', 'all'); $createdFrom = $request->queryString('created_from'); $createdTo = $request->queryString('created_to'); $tenant = $request->queryString('tenant'); $roles = $request->query('roles', ''); $departments = $request->query('departments', ''); $allowedOrder = ['id', 'uuid', 'first_name', 'last_name', 'email', 'locale', 'theme', 'created', 'modified', 'active']; if (!in_array($order, $allowedOrder, true)) { $order = 'id'; } $limit = exportCapLimit($request->query('limit'), 5000); $result = app(\MintyPHP\Service\User\UserAccountService::class)->listPaged([ 'limit' => $limit, 'offset' => 0, 'search' => $search, 'order' => $order, 'dir' => $dir, 'active' => $active, 'created_from' => $createdFrom, 'created_to' => $createdTo, 'tenant' => $tenant, 'roles' => $roles, 'departments' => $departments, 'tenantUserId' => $currentUserId, ]); $joinLabels = static function (mixed $value): string { if (is_array($value)) { return implode(' | ', array_map('strval', $value)); } if (is_string($value)) { $trimmed = trim($value); if ($trimmed === '') { return ''; } return str_replace('||', ' | ', $trimmed); } return ''; }; $formatTimestamp = static function (mixed $value): string { if ($value === null || $value === '') { return ''; } return (string) dt($value, 'Y-m-d H:i:s'); }; $columns = [ new ExportColumn('id', static fn (array $r): string => (string) ($r['id'] ?? '')), new ExportColumn('uuid', static fn (array $r): string => (string) ($r['uuid'] ?? '')), new ExportColumn('first_name', static fn (array $r): string => (string) ($r['first_name'] ?? '')), new ExportColumn('last_name', static fn (array $r): string => (string) ($r['last_name'] ?? '')), new ExportColumn('email', static fn (array $r): string => (string) ($r['email'] ?? '')), new ExportColumn('phone', static fn (array $r): string => (string) ($r['phone'] ?? ''), allowSignedNumeric: true), new ExportColumn('mobile', static fn (array $r): string => (string) ($r['mobile'] ?? ''), allowSignedNumeric: true), new ExportColumn('short_dial', static fn (array $r): string => (string) ($r['short_dial'] ?? ''), allowSignedNumeric: true), new ExportColumn('locale', static fn (array $r): string => (string) ($r['locale'] ?? '')), new ExportColumn('theme', static fn (array $r): string => (string) ($r['theme'] ?? '')), new ExportColumn('active', static fn (array $r): int => (int) ($r['active'] ?? 0)), new ExportColumn('tenants', static fn (array $r): string => $joinLabels($r['tenant_labels'] ?? [])), new ExportColumn('departments', static fn (array $r): string => $joinLabels($r['department_labels'] ?? [])), new ExportColumn('roles', static fn (array $r): string => $joinLabels($r['role_labels'] ?? [])), new ExportColumn('created_by', static fn (array $r): string => (string) ($r['created_by'] ?? '')), new ExportColumn('modified_by', static fn (array $r): string => (string) ($r['modified_by'] ?? '')), new ExportColumn('created', static fn (array $r): string => $formatTimestamp($r['created'] ?? '')), new ExportColumn('modified', static fn (array $r): string => $formatTimestamp($r['modified'] ?? '')), ]; $filename = 'users-' . date('Ymd-His') . '.csv'; exportSendCsv($filename, $result['rows'] ?? [], $columns, exportResolveFlavor($request));