Skip to content

A free open source IT asset/license management system

Accoutn for deliberate null company pseudo company

snipeb9672dc238f0
diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php
index 6241b4f9d4..755cfcc5ee 100644
--- a/app/Helpers/Helper.php
+++ b/app/Helpers/Helper.php
@@ -1151,8 +1151,15 @@ class Helper
if (! auth()->check()) {
return false;
}
+ $actor = auth()->user();
+ if ($actor->isSuperUser()) {
+ return false;
+ }
- return ! auth()->user()->isSuperUser();
+ // Uncompanied users work in the null pseudo-company namespace
+ // under strict mode; null IS a valid company id for them, so
+ // don't render the field as required.
+ return $actor->companies()->exists();
}
/**
diff --git a/app/Http/Controllers/Users/BulkUsersController.php b/app/Http/Controllers/Users/BulkUsersController.php
index bf836813fc..0eed29b406 100644
--- a/app/Http/Controllers/Users/BulkUsersController.php
+++ b/app/Http/Controllers/Users/BulkUsersController.php
@@ -275,8 +275,25 @@ class BulkUsersController extends Controller
$allowedIds = Company::getIdsForCurrentUser($bulkCompanyIds);
}
- // Floater-mode self-elevation guard (#19200). See User::canGrantFloaterStatus.
$wouldClear = $clearCompanies || ($bulkCompanyIds && empty($allowedIds));
+
+ // Strict-FMCS #19192 gate — mirrors the branch in SaveUserRequest
+ // so bulk-editing a batch to clear all company memberships in
+ // strict mode is blocked for companied non-superusers. Fires
+ // before the older floater-mode gate so its more specific error
+ // wins when both apply. Skips uncompanied actors because they
+ // legitimately operate in the null pseudo-company namespace in
+ // strict mode; nulling pivots there is their normal workflow,
+ // not a self-escalation attempt.
+ $settings = Setting::getSettings();
+ $strictFmcs = $settings->full_multiple_companies_support && ! $settings->null_company_is_floater;
+ $actor = auth()->user();
+ if ($wouldClear && $strictFmcs && ! $actor->isSuperUser() && $actor->companies()->exists()) {
+ return redirect()->route('users.index')
+ ->with('error', trans('validation.fmcs_company', ['attribute' => trans('general.company')]));
+ }
+
+ // Floater-mode self-elevation guard (#19200). See User::canGrantFloaterStatus.
if ($wouldClear && ! auth()->user()->canGrantFloaterStatus()) {
return redirect()->route('users.index')
->with('error', trans('admin/users/general.cannot_make_floater'));
diff --git a/app/Http/Requests/SaveUserRequest.php b/app/Http/Requests/SaveUserRequest.php
index 6fb28675d6..b42a4b2d1d 100644
--- a/app/Http/Requests/SaveUserRequest.php
+++ b/app/Http/Requests/SaveUserRequest.php
@@ -116,20 +116,25 @@ class SaveUserRequest extends FormRequest
if (empty($effective)) {
$settings = Setting::getSettings();
- $creatorIsSuper = (bool) auth()->user()?->isSuperUser();
+ $actor = auth()->user();
+ $creatorIsSuper = (bool) $actor?->isSuperUser();
+ $creatorHasCompanies = (bool) $actor?->companies()->exists();
$strictFmcs = $settings->full_multiple_companies_support && ! $settings->null_company_is_floater;
// Strict-FMCS #19192 gate — hits before the older floater
// gate so its more specific error message wins when both
- // apply.
- if ($strictFmcs && ! $creatorIsSuper) {
+ // apply. Skips uncompanied actors because they legitimately
+ // work in the null pseudo-company namespace under strict
+ // mode; forcing them to add memberships they don't have
+ // would lock them out of their normal workflow.
+ if ($strictFmcs && ! $creatorIsSuper && $creatorHasCompanies) {
$validator->errors()->add('company_ids', trans('validation.fmcs_company', ['attribute' => trans('general.company')]));
return;
}
// Original #19200 floater-grant gate.
- if (! auth()->user()?->canGrantFloaterStatus()) {
+ if (! $actor?->canGrantFloaterStatus()) {
$validator->errors()->add('company_ids', trans('admin/users/general.cannot_make_floater'));
}
}
diff --git a/app/Providers/ValidationServiceProvider.php b/app/Providers/ValidationServiceProvider.php
index 73d860d41c..7d7b5abc9e 100644
--- a/app/Providers/ValidationServiceProvider.php
+++ b/app/Providers/ValidationServiceProvider.php
@@ -454,17 +454,24 @@ class ValidationServiceProvider extends ServiceProvider
// Enforces "Company must be picked" when FMCS is on AND
// null_company_is_floater is disabled (strict mode). Without this
- // rule non-superuser users can save a form with an unset company
- // dropdown, land a row with company_id=NULL, and then have that
- // row instantly filtered out of their own view by the strict-mode
- // scope. See #19192. Passes when:
+ // rule a companied non-superuser can save a form with an unset
+ // company dropdown, land a row with company_id=NULL, and then
+ // have that row instantly filtered out of their own view by the
+ // strict-mode scope. See #19192. Passes when:
// - FMCS is off (nothing to enforce)
// - null_company_is_floater is on (nulls are legal floaters)
// - value is present (form was filled in)
+ // - no auth context (CLI / seeders / importers bypass — same
+ // posture as SaveUserRequest's cannot_make_floater gate)
// - acting user is a superuser (they see everything; a null is
// an explicit choice, not an accident)
- // - no auth context (CLI / seeders / importers bypass — same
- // posture as SaveUserRequest's cannot_make_floater gate).
+ // - acting user has NO company memberships. In strict mode
+ // such users legitimately operate in the null "pseudo-company"
+ // namespace — Company::scopeCompanyablesDirectly scopes them
+ // to whereNull($company_id), so null IS a valid company id
+ // for them. Forcing them to pick a non-null company would
+ // both lock them out of their normal workflow and produce a
+ // row they wouldn't be able to see afterward.
Validator::extend('fmcs_company', function ($attribute, $value, $parameters, $validator) {
$settings = Setting::getSettings();
if (! $settings->full_multiple_companies_support) {
@@ -479,8 +486,15 @@ class ValidationServiceProvider extends ServiceProvider
if (! auth()->check()) {
return true;
}
+ $actor = auth()->user();
+ if ($actor->isSuperUser()) {
+ return true;
+ }
+ if (! $actor->companies()->exists()) {
+ return true;
+ }
- return (bool) auth()->user()->isSuperUser();
+ return false;
});
Validator::replacer('fmcs_company', function ($message, $attribute, $rule, $parameters) {
diff --git a/tests/Feature/Fmcs/StrictModeRequiresCompanyOnCreateTest.php b/tests/Feature/Fmcs/StrictModeRequiresCompanyOnCreateTest.php
index 351e744435..65574c26e0 100644
--- a/tests/Feature/Fmcs/StrictModeRequiresCompanyOnCreateTest.php
+++ b/tests/Feature/Fmcs/StrictModeRequiresCompanyOnCreateTest.php
@@ -103,6 +103,45 @@ class StrictModeRequiresCompanyOnCreateTest extends TestCase
$this->assertFalse($validator->fails());
}
+ public function test_rule_accepts_null_for_uncompanied_non_superuser_in_strict_mode()
+ {
+ // Regression guard for the pseudo-company workflow. Under
+ // Company::scopeCompanyablesDirectly in strict mode, actors
+ // with no company memberships are scoped to null-company rows
+ // (whereNull($column)). Null IS a valid company id for them —
+ // forcing them to pick a non-null company would both lock them
+ // out of their normal workflow AND produce a row they wouldn't
+ // be able to see afterward.
+ $this->settings->enableMultipleFullCompanySupport();
+ $this->settings->disableFloaterMode();
+
+ $actor = User::factory()->withoutCompany()->create();
+ $this->assertFalse($actor->companies()->exists(), 'test precondition: actor is uncompanied');
+ auth()->login($actor);
+
+ $validator = Validator::make(['company_id' => null], ['company_id' => 'fmcs_company']);
+
+ $this->assertFalse($validator->fails());
+ }
+
+ public function test_rule_still_rejects_null_for_companied_non_superuser_in_strict_mode()
+ {
+ // Reporter's #19192 case: a non-superuser WITH company
+ // memberships submitting a null company_id would land an
+ // invisible row. That must still fail.
+ $this->settings->enableMultipleFullCompanySupport();
+ $this->settings->disableFloaterMode();
+
+ $company = Company::factory()->create();
+ $actor = $company->users()->save(User::factory()->create());
+ $this->assertTrue($actor->companies()->exists(), 'test precondition: actor has memberships');
+ auth()->login($actor);
+
+ $validator = Validator::make(['company_id' => null], ['company_id' => 'fmcs_company']);
+
+ $this->assertTrue($validator->fails());
+ }
+
// ------------------------------------------------------------------
// Sanity: every model the reporter listed has the rule wired
// ------------------------------------------------------------------
diff --git a/tests/Feature/Users/Api/UpdateUserTest.php b/tests/Feature/Users/Api/UpdateUserTest.php
index 3b455831d7..6d4cec721a 100644
--- a/tests/Feature/Users/Api/UpdateUserTest.php
+++ b/tests/Feature/Users/Api/UpdateUserTest.php
@@ -377,20 +377,16 @@ class UpdateUserTest extends TestCase
->assertStatusMessageIs('error')
->json();
- // Behavior change note (#19192): an admin with no company
- // memberships in strict FMCS mode cannot successfully PATCH any
- // user. Empty company_ids trips the new gate; any non-empty
- // list is filtered to empty by Company::getIdsForCurrentUser()
- // because they have no accessible companies. Before the gate,
- // the no-company-user case below was a permissive no-op
- // success — that path is now closed. Practical impact: strict
- // FMCS deployments should grant such admins at least one
- // company (or superuser) so they can act.
+ // Admin without a company should allow updating user without
+ // a company. Under strict FMCS mode uncompanied users operate
+ // in the null pseudo-company namespace (Company scoping shows
+ // them null-company rows); the #19192 gate steps aside for
+ // them so this normal workflow keeps working.
$this->actingAsForApi($adminNoCompany)
->patchJson(route('api.users.update', $scoped_user_in_no_company))
->assertOk()
->assertStatus(200)
- ->assertStatusMessageIs('error')
+ ->assertStatusMessageIs('success')
->json();
// Admin without a company should get denied updating user from Company A
diff --git a/tests/Feature/Users/Ui/FloaterModeGateTest.php b/tests/Feature/Users/Ui/FloaterModeGateTest.php
index 9a4ad25e00..cf6c0d3d39 100644
--- a/tests/Feature/Users/Ui/FloaterModeGateTest.php
+++ b/tests/Feature/Users/Ui/FloaterModeGateTest.php
@@ -118,6 +118,56 @@ class FloaterModeGateTest extends TestCase
$this->assertNotEmpty($victim->fresh()->companies);
}
+ public function test_non_superuser_cannot_bulk_clear_companies_in_strict_fmcs_mode()
+ {
+ // #19192 companion to the floater-mode test above: strict FMCS
+ // (floaters OFF) also blocks a non-superuser from bulk-clearing
+ // pivot memberships, because doing so would make every targeted
+ // user instantly invisible to the acting admin's own scope.
+ // Pre-fix, BulkUsersController's only gate was canGrantFloaterStatus,
+ // which returns true in strict mode and let the clear proceed.
+ $this->settings->enableMultipleFullCompanySupport();
+ $this->settings->disableFloaterMode();
+
+ $company = Company::factory()->create();
+ $actor = $company->users()->save(User::factory()->editUsers()->create());
+ $victim = $company->users()->save(User::factory()->create());
+
+ $this->actingAs($actor)
+ ->post(route('users/bulkeditsave'), [
+ 'ids' => [$victim->id => '1'],
+ 'null_company_ids' => '1',
+ ])
+ ->assertRedirect()
+ ->assertSessionHas('error');
+
+ $this->assertContains($company->id, $victim->fresh()->companies->pluck('id')->all(), 'Bulk clear in strict mode should be refused');
+ $this->assertNotEmpty($victim->fresh()->companies);
+ }
+
+ public function test_strict_fmcs_bulk_clear_gate_does_not_fire_for_superuser()
+ {
+ // Superusers see everything (their scope reaches null-pivot
+ // rows), so leaving pivots empty is a deliberate action for
+ // them, not the visibility trap that motivates #19192. The
+ // gate must not fire — whether the pivot ends up actually
+ // cleared is a separate concern owned by the bulk-clear
+ // controller flow, tested elsewhere.
+ $this->settings->enableMultipleFullCompanySupport();
+ $this->settings->disableFloaterMode();
+
+ $company = Company::factory()->create();
+ $actor = User::factory()->superuser()->create();
+ $victim = $company->users()->save(User::factory()->create());
+
+ $this->actingAs($actor)
+ ->post(route('users/bulkeditsave'), [
+ 'ids' => [$victim->id => '1'],
+ 'null_company_ids' => '1',
+ ])
+ ->assertSessionMissing('error');
+ }
+
public function test_non_superuser_cannot_create_a_new_user_with_no_companies_in_floater_mode()
{
// Covers the web POST path — SaveUserRequest's withValidator fires on