22-05-2026 Optimierung der User und Admin Panels
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled

This commit is contained in:
Kevin Adametz 2026-05-22 11:18:59 +02:00
parent d2ba22c0cf
commit e8c47b7553
73 changed files with 10282 additions and 1546 deletions

View file

@ -6,6 +6,8 @@ use App\Models\Company;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
class CustomerCompanyContext
{
@ -60,7 +62,54 @@ class CustomerCompanyContext
return null;
}
return $this->companiesFor($user)->firstWhere('id', $companyId);
return $this->findFor($user, $companyId);
}
/**
* @return Collection<int, Company>
*/
public function latestCompaniesFor(User $user, int $limit = 10): Collection
{
return $this->accessibleCompanyQuery($user)
->select(['companies.id', 'companies.name', 'companies.portal', 'companies.owner_user_id', 'companies.created_at'])
->latest('companies.created_at')
->latest('companies.id')
->limit($limit)
->get();
}
public function companyCountFor(User $user): int
{
return $this->accessibleCompanyQuery($user)->count();
}
/**
* @return Collection<int, Company>
*/
public function switcherCompaniesFor(User $user, ?int $selectedCompanyId = null, int $limit = 50): Collection
{
$companies = $this->switcherCompanyQuery($user)
->latest('companies.created_at')
->latest('companies.id')
->limit($limit)
->get();
if ($selectedCompanyId === null || $companies->contains('id', $selectedCompanyId)) {
return $companies;
}
$selectedCompany = $this->switcherCompanyQuery($user)
->whereKey($selectedCompanyId)
->first();
if (! $selectedCompany) {
return $companies;
}
return $companies
->prepend($selectedCompany)
->unique('id')
->values();
}
/**
@ -100,14 +149,14 @@ class CustomerCompanyContext
public function clear(): void
{
session()->forget(self::SessionKey);
Session::forget(self::SessionKey);
}
public function roleLabelFor(Company $company, User $user): string
{
$role = $company->owner_user_id === $user->id
? 'owner'
: ($company->pivot?->role ?? 'member');
: ($company->getAttribute('current_user_role') ?? $company->pivot?->role ?? 'member');
return match ($role) {
'owner' => __('Owner'),
@ -116,6 +165,22 @@ class CustomerCompanyContext
};
}
/**
* @return Builder<Company>
*/
private function switcherCompanyQuery(User $user): Builder
{
return $this->accessibleCompanyQuery($user)
->select(['companies.id', 'companies.name', 'companies.portal', 'companies.owner_user_id', 'companies.created_at'])
->addSelect([
'current_user_role' => DB::table('company_user')
->select('role')
->whereColumn('company_user.company_id', 'companies.id')
->where('company_user.user_id', $user->id)
->limit(1),
]);
}
private function userCanAccessCompany(User $user, int $companyId): bool
{
return $this->accessibleCompanyQuery($user)