Merge pull request #12467 from marcusmoore/fix/scope-people-with-full-multiple-companies

Fix full company scoping in user selects
This commit is contained in:
snipe 2023-02-23 10:36:33 -08:00 committed by GitHub
commit eb6357636a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 124 additions and 6 deletions

View file

@ -286,9 +286,11 @@ class UsersController extends Controller
$users = Company::scopeCompanyables($users);
if ($request->filled('search')) {
$users = $users->SimpleNameSearch($request->get('search'))
->orWhere('username', 'LIKE', '%'.$request->get('search').'%')
->orWhere('employee_num', 'LIKE', '%'.$request->get('search').'%');
$users = $users->where(function ($query) use ($request) {
$query->SimpleNameSearch($request->get('search'))
->orWhere('username', 'LIKE', '%'.$request->get('search').'%')
->orWhere('employee_num', 'LIKE', '%'.$request->get('search').'%');
});
}
$users = $users->orderBy('last_name', 'asc')->orderBy('first_name', 'asc');

View file

@ -27,7 +27,7 @@ class AssetCheckoutController extends Controller
public function create($assetId)
{
// Check if the asset exists
if (is_null($asset = Asset::find(e($assetId)))) {
if (is_null($asset = Asset::with('company')->find(e($assetId)))) {
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
}

View file

@ -52,4 +52,13 @@ class SettingFactory extends Factory
'email_domain' => 'test.com',
];
}
public function withMultipleFullCompanySupport()
{
return $this->state(function () {
return [
'full_multiple_companies_support' => 1,
];
});
}
}

View file

@ -26,6 +26,16 @@
</div>
<div class="box-body">
{{csrf_field()}}
@if ($asset->company && $asset->company->name)
<div class="form-group">
{{ Form::label('model', trans('general.company'), array('class' => 'col-md-3 control-label')) }}
<div class="col-md-8">
<p class="form-control-static">
{{ $asset->company->name }}
</p>
</div>
</div>
@endif
<!-- AssetModel name -->
<div class="form-group">
{{ Form::label('model', trans('admin/hardware/form.model'), array('class' => 'col-md-3 control-label')) }}
@ -33,7 +43,6 @@
<p class="form-control-static">
@if (($asset->model) && ($asset->model->name))
{{ $asset->model->name }}
@else
<span class="text-danger text-bold">
<i class="fas fa-exclamation-triangle"></i>{{ trans('admin/hardware/general.model_invalid')}}
@ -168,4 +177,4 @@
</script>
@stop
@stop

View file

@ -0,0 +1,90 @@
<?php
namespace Tests\Feature\Api\Users;
use App\Models\Company;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Testing\Fluent\AssertableJson;
use Laravel\Passport\Passport;
use Tests\TestCase;
class UsersForSelectListTest extends TestCase
{
use RefreshDatabase;
public function testUsersAreReturned()
{
Setting::factory()->create();
User::factory()->count(3)->create();
Passport::actingAs(User::factory()->firstAdmin()->create());
$this->getJson(route('api.users.selectlist'))
->assertOk()
->assertJsonStructure([
'results',
'pagination',
'total_count',
'page',
'page_count',
])
->assertJson(fn(AssertableJson $json) => $json->has('results', 4)->etc());
}
public function testUsersScopedToCompanyWhenMultipleFullCompanySupportEnabled()
{
Setting::factory()->withMultipleFullCompanySupport()->create();
$jedi = Company::factory()->has(User::factory()->count(3)->sequence(
['first_name' => 'Luke', 'last_name' => 'Skywalker', 'username' => 'lskywalker'],
['first_name' => 'Obi-Wan', 'last_name' => 'Kenobi', 'username' => 'okenobi'],
['first_name' => 'Anakin', 'last_name' => 'Skywalker', 'username' => 'askywalker'],
))->create();
$sith = Company::factory()
->has(User::factory()->state(['first_name' => 'Darth', 'last_name' => 'Vader', 'username' => 'dvader']))
->create();
Passport::actingAs($jedi->users->first());
$response = $this->getJson(route('api.users.selectlist'))->assertOk();
$results = collect($response->json('results'));
$this->assertEquals(3, $results->count());
$this->assertTrue(
$results->pluck('text')->contains(fn($text) => str_contains($text, 'Luke'))
);
$this->assertFalse(
$results->pluck('text')->contains(fn($text) => str_contains($text, 'Darth'))
);
}
public function testUsersScopedToCompanyDuringSearchWhenMultipleFullCompanySupportEnabled()
{
Setting::factory()->withMultipleFullCompanySupport()->create();
$jedi = Company::factory()->has(User::factory()->count(3)->sequence(
['first_name' => 'Luke', 'last_name' => 'Skywalker', 'username' => 'lskywalker'],
['first_name' => 'Obi-Wan', 'last_name' => 'Kenobi', 'username' => 'okenobi'],
['first_name' => 'Anakin', 'last_name' => 'Skywalker', 'username' => 'askywalker'],
))->create();
Company::factory()
->has(User::factory()->state(['first_name' => 'Darth', 'last_name' => 'Vader', 'username' => 'dvader']))
->create();
Passport::actingAs($jedi->users->first());
$response = $this->getJson(route('api.users.selectlist', ['search' => 'a']))->assertOk();
$results = collect($response->json('results'));
$this->assertEquals(3, $results->count());
$this->assertTrue($results->pluck('text')->contains(fn($text) => str_contains($text, 'Luke')));
$this->assertTrue($results->pluck('text')->contains(fn($text) => str_contains($text, 'Anakin')));
$response = $this->getJson(route('api.users.selectlist', ['search' => 'v']))->assertOk();
$this->assertEquals(0, collect($response->json('results'))->count());
}
}

View file

@ -2,9 +2,17 @@
namespace Tests;
use App\Models\Setting;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected function setUp(): void
{
parent::setUp();
$this->beforeApplicationDestroyed(fn() => Setting::$_cache = null);
}
}