12-05-2026 Frontend dev
This commit is contained in:
parent
405df0a122
commit
5b8bdf4182
779 changed files with 480564 additions and 6241 deletions
125
app/Services/Customer/CustomerCompanyContext.php
Normal file
125
app/Services/Customer/CustomerCompanyContext.php
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\Customer;
|
||||
|
||||
use App\Models\Company;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class CustomerCompanyContext
|
||||
{
|
||||
public const SessionKey = 'customer_company_context_id';
|
||||
|
||||
/**
|
||||
* @return Collection<int, Company>
|
||||
*/
|
||||
public function companiesFor(User $user): Collection
|
||||
{
|
||||
$linkedCompanies = $user->companies()
|
||||
->withoutGlobalScopes()
|
||||
->select(['companies.id', 'companies.name', 'companies.portal', 'companies.owner_user_id'])
|
||||
->withPivot('role')
|
||||
->get();
|
||||
|
||||
$ownedCompanies = $user->ownedCompanies()
|
||||
->withoutGlobalScopes()
|
||||
->whereNotIn('id', $linkedCompanies->pluck('id'))
|
||||
->get(['id', 'name', 'portal', 'owner_user_id']);
|
||||
|
||||
return $linkedCompanies
|
||||
->concat($ownedCompanies)
|
||||
->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE)
|
||||
->values();
|
||||
}
|
||||
|
||||
public function selectedCompanyId(User $user): ?int
|
||||
{
|
||||
$companyId = session(self::SessionKey);
|
||||
|
||||
if (! is_numeric($companyId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$companyId = (int) $companyId;
|
||||
|
||||
if (! $this->userCanAccessCompany($user, $companyId)) {
|
||||
$this->clear();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return $companyId;
|
||||
}
|
||||
|
||||
public function selectedCompany(User $user): ?Company
|
||||
{
|
||||
$companyId = $this->selectedCompanyId($user);
|
||||
|
||||
if ($companyId === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->companiesFor($user)->firstWhere('id', $companyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Builder<Company>
|
||||
*/
|
||||
public function accessibleCompanyQuery(User $user): Builder
|
||||
{
|
||||
return Company::query()
|
||||
->withoutGlobalScopes()
|
||||
->where(function ($query) use ($user): void {
|
||||
$query->where('owner_user_id', $user->id)
|
||||
->orWhereHas('users', fn ($userQuery) => $userQuery->whereKey($user->id));
|
||||
});
|
||||
}
|
||||
|
||||
public function findFor(User $user, int $companyId): ?Company
|
||||
{
|
||||
return $this->accessibleCompanyQuery($user)
|
||||
->whereKey($companyId)
|
||||
->first();
|
||||
}
|
||||
|
||||
public function select(User $user, ?int $companyId): void
|
||||
{
|
||||
if ($companyId === null) {
|
||||
$this->clear();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $this->userCanAccessCompany($user, $companyId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
session([self::SessionKey => $companyId]);
|
||||
}
|
||||
|
||||
public function clear(): void
|
||||
{
|
||||
session()->forget(self::SessionKey);
|
||||
}
|
||||
|
||||
public function roleLabelFor(Company $company, User $user): string
|
||||
{
|
||||
$role = $company->owner_user_id === $user->id
|
||||
? 'owner'
|
||||
: ($company->pivot?->role ?? 'member');
|
||||
|
||||
return match ($role) {
|
||||
'owner' => __('Owner'),
|
||||
'responsible' => __('Verantwortlich'),
|
||||
default => __('Mitglied'),
|
||||
};
|
||||
}
|
||||
|
||||
private function userCanAccessCompany(User $user, int $companyId): bool
|
||||
{
|
||||
return $this->accessibleCompanyQuery($user)
|
||||
->whereKey($companyId)
|
||||
->exists();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue