This commit is contained in:
2026-11-02 20:27:11 +02:00
parent b44378b899
commit badaada043
300 changed files with 17458 additions and 1056 deletions
+29 -15
View File
@@ -63,21 +63,11 @@ class WorkContext
/** @return list<string> */
public function available(User $user): array
{
$enabled = $this->policy($user)['enabled'] ?? [];
$granted = [];
if (app(OrganizationAccess::class)->active($user->tenant_id)) {
$profiles = DB::table('organization_memberships as m')->join('organization_profiles as p', 'p.id', '=', 'm.profile_id')
->where('m.tenant_id', $user->tenant_id)->where('p.tenant_id', $user->tenant_id)->where('m.user_id', $user->id)
->where(fn ($q) => $q->whereNull('m.expires_at')->orWhere('m.expires_at', '>', now()))->get(['p.work_contexts', 'p.permissions']);
foreach ($profiles as $profile) {
$permissions = json_decode($profile->permissions, true, 32, JSON_THROW_ON_ERROR);
foreach (json_decode($profile->work_contexts ?? '[]', true, 32, JSON_THROW_ON_ERROR) as $key) {
if ($this->functional($permissions, $key)) {
$granted[] = $key;
}
}
}
} elseif (app()->environment(['testing', 'payable-testing'])) {
return $this->configuredAvailable($user);
}
$granted = [];
if (app()->environment(['testing', 'payable-testing'])) {
// Development roles remain usable locally; never become customer grants.
foreach (array_keys(self::OPTIONS) as $key) {
if ($this->functional(app(OrganizationAccess::class)->permissions($user), $key)) {
@@ -86,7 +76,31 @@ class WorkContext
}
}
return array_values(array_intersect($this->purchased($user), $enabled, array_unique($granted)));
return array_values(array_intersect($this->purchased($user), $this->policy($user)['enabled'] ?? [], array_unique($granted)));
}
/** Same configured intersection used after activation, without enabling it.
* @return list<string>
*/
public function configuredAvailable(User $user): array
{
if (! Schema::hasTable('organization_memberships') || ! Schema::hasColumn('organization_profiles', 'work_contexts')) {
return [];
}
$granted = [];
$profiles = DB::table('organization_memberships as m')->join('organization_profiles as p', 'p.id', '=', 'm.profile_id')
->where('m.tenant_id', $user->tenant_id)->where('p.tenant_id', $user->tenant_id)->where('m.user_id', $user->id)
->where(fn ($q) => $q->whereNull('m.expires_at')->orWhere('m.expires_at', '>', now()))->get(['p.work_contexts', 'p.permissions']);
foreach ($profiles as $profile) {
$permissions = json_decode($profile->permissions, true, 32, JSON_THROW_ON_ERROR);
foreach (json_decode($profile->work_contexts ?? '[]', true, 32, JSON_THROW_ON_ERROR) as $key) {
if ($this->functional($permissions, $key)) {
$granted[] = $key;
}
}
}
return array_values(array_intersect($this->purchased($user), $this->policy($user)['enabled'] ?? [], array_unique($granted)));
}
/** @param list<string> $permissions */