Skip to content

Commit 786b207

Browse files
committed
Merge remote-tracking branch 'origin/develop'
2 parents 0e957ca + 35739c2 commit 786b207

File tree

7 files changed

+135
-15
lines changed

7 files changed

+135
-15
lines changed

app/Http/Controllers/Api/AssetsController.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,9 +1290,19 @@ public function requestable(Request $request): JsonResponse | array
12901290

12911291
public function assignedAssets(Request $request, Asset $asset) : JsonResponse | array
12921292
{
1293+
$this->authorize('view', Asset::class);
1294+
$this->authorize('view', $asset);
1295+
1296+
$query = Asset::where([
1297+
'assigned_to' => $asset->id,
1298+
'assigned_type' => Asset::class,
1299+
]);
1300+
1301+
$total = $query->count();
1302+
1303+
$assets = $query->applyOffsetAndLimit($total)->get();
12931304

1294-
return [];
1295-
// to do
1305+
return (new AssetsTransformer)->transformAssets($assets, $total);
12961306
}
12971307

12981308
public function assignedAccessories(Request $request, Asset $asset) : JsonResponse | array

app/Http/Controllers/Components/ComponentCheckoutController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,15 @@ public function store(Request $request, $componentId)
102102
return redirect()->route('components.checkout.show', $componentId)->with('error', trans('general.error_user_company'));
103103
}
104104

105+
$component->checkout_qty = $request->input('assigned_qty');
106+
105107
// Update the component data
106108
$component->asset_id = $request->input('asset_id');
107109
$component->assets()->attach($component->id, [
108110
'component_id' => $component->id,
109111
'created_by' => auth()->user()->id,
110112
'created_at' => date('Y-m-d H:i:s'),
111-
'assigned_qty' => $request->input('assigned_qty'),
113+
'assigned_qty' => $component->checkout_qty,
112114
'asset_id' => $request->input('asset_id'),
113115
'note' => $request->input('note'),
114116
]);

app/Mail/CheckoutComponentMail.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct(Component $component, $checkedOutTo, User $checkedOu
2626
$this->note = $note;
2727
$this->target = $checkedOutTo;
2828
$this->acceptance = $acceptance;
29-
$this->qty = $component->assets->first()?->pivot?->assigned_qty;
29+
$this->qty = $component->checkout_qty;
3030

3131
$this->settings = Setting::getSettings();
3232
}

app/Models/Ldap.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ public static function connectToLdap()
7878
if (env('LDAPTLS_CACERT')) {
7979
putenv('LDAPTLS_CACERT='.env('LDAPTLS_CACERT'));
8080
}
81+
// You _were_ allowed to do this *after* the ldap_connect() in some versions of PHP, but it's not how they want
82+
// you to anymore, and it seems to not work at all in later PHP versions.
83+
if (Setting::getSettings()->ldap_client_tls_cert && Setting::getSettings()->ldap_client_tls_key) {
84+
ldap_set_option(null, LDAP_OPT_X_TLS_CERTFILE, Setting::get_client_side_cert_path());
85+
ldap_set_option(null, LDAP_OPT_X_TLS_KEYFILE, Setting::get_client_side_key_path());
86+
}
87+
8188
$connection = @ldap_connect($ldap_host);
8289

8390
if (! $connection) {
@@ -89,11 +96,6 @@ public static function connectToLdap()
8996
ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, $ldap_version);
9097
ldap_set_option($connection, LDAP_OPT_NETWORK_TIMEOUT, 20);
9198

92-
if (Setting::getSettings()->ldap_client_tls_cert && Setting::getSettings()->ldap_client_tls_key) {
93-
ldap_set_option(null, LDAP_OPT_X_TLS_CERTFILE, Setting::get_client_side_cert_path());
94-
ldap_set_option(null, LDAP_OPT_X_TLS_KEYFILE, Setting::get_client_side_key_path());
95-
}
96-
9799
if ($ldap_use_tls=='1') {
98100
ldap_start_tls($connection);
99101
}

app/Models/SnipeModel.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace App\Models;
44

55
use App\Helpers\Helper;
6+
use Illuminate\Database\Eloquent\Builder;
67
use Illuminate\Database\Eloquent\Casts\Attribute;
78
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Support\Facades\Request;
810

911
class SnipeModel extends Model
1012
{
@@ -156,6 +158,20 @@ public function setStatusIdAttribute($value)
156158
$this->attributes['status_id'] = $value;
157159
}
158160

161+
/**
162+
* Applies offset (from request) and limit to query.
163+
*
164+
* @param Builder $query
165+
* @param int $total
166+
* @return void
167+
*/
168+
public function scopeApplyOffsetAndLimit(Builder $query, int $total)
169+
{
170+
$offset = (Request::input('offset') > $total) ? $total : app('api_offset_value');
171+
$limit = app('api_limit_value');
172+
173+
$query->skip($offset)->take($limit);
174+
}
159175

160176
protected function displayName(): Attribute
161177
{
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Tests\Feature\Assets\Api;
4+
5+
use App\Models\Asset;
6+
use App\Models\Company;
7+
use App\Models\User;
8+
use Illuminate\Testing\Fluent\AssertableJson;
9+
use Tests\TestCase;
10+
11+
class AssignedAssetsTest extends TestCase
12+
{
13+
public function test_requires_permission()
14+
{
15+
$this->actingAsForApi(User::factory()->create())
16+
->getJson(route('api.assets.assigned_assets', Asset::factory()->create()))
17+
->assertForbidden();
18+
}
19+
20+
public function test_adheres_to_company_scoping()
21+
{
22+
$this->settings->enableMultipleFullCompanySupport();
23+
24+
[$companyA, $companyB] = Company::factory()->count(2)->create();
25+
26+
$asset = Asset::factory()->for($companyA)->create();
27+
28+
$user = User::factory()->for($companyB)->viewAssets()->create();
29+
30+
$this->actingAsForApi($user)
31+
->getJson(route('api.assets.assigned_assets', $asset))
32+
->assertOk()
33+
->assertStatusMessageIs('error')
34+
->assertMessagesAre('Asset not found');
35+
}
36+
37+
public function test_can_get_assets_assigned_to_specific_asset()
38+
{
39+
$unassociatedAsset = Asset::factory()->create();
40+
41+
$asset = Asset::factory()->hasAssignedAssets(2)->create();
42+
43+
$assetsAssignedToAsset = Asset::where([
44+
'assigned_to' => $asset->id,
45+
'assigned_type' => Asset::class,
46+
])->get();
47+
48+
$this->actingAsForApi(User::factory()->viewAssets()->create())
49+
->getJson(route('api.assets.assigned_assets', $asset))
50+
->assertOk()
51+
->assertResponseContainsInRows($assetsAssignedToAsset, 'serial')
52+
->assertResponseDoesNotContainInRows($unassociatedAsset, 'serial')
53+
->assertJson(function (AssertableJson $json) {
54+
$json->where('total', 2)
55+
->count('rows', 2)
56+
->etc();
57+
});
58+
}
59+
60+
public function test_adheres_to_offset_and_limit()
61+
{
62+
$asset = Asset::factory()->hasAssignedAssets(2)->create();
63+
64+
$assetsAssignedToAsset = Asset::where([
65+
'assigned_to' => $asset->id,
66+
'assigned_type' => Asset::class,
67+
])->get();
68+
69+
$this->actingAsForApi(User::factory()->viewAssets()->create())
70+
->getJson(route('api.assets.assigned_assets', [
71+
'asset' => $asset,
72+
'offset' => 1,
73+
'limit' => 1,
74+
]))
75+
->assertOk()
76+
->assertResponseDoesNotContainInRows($assetsAssignedToAsset->first(), 'serial')
77+
->assertResponseContainsInRows($assetsAssignedToAsset->last(), 'serial')
78+
->assertJson(function (AssertableJson $json) {
79+
$json->where('total', 2)
80+
->count('rows', 1)
81+
->etc();
82+
});
83+
}
84+
}

tests/Support/CustomTestMacros.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@ protected function registerCustomMacros()
2121

2222
TestResponse::macro(
2323
'assertResponseContainsInRows',
24-
function (Model $model, string $property = 'name') use ($guardAgainstNullProperty) {
25-
$guardAgainstNullProperty($model, $property);
24+
function (iterable|Model $models, string $property = 'name') use ($guardAgainstNullProperty) {
25+
if ($models instanceof Model) {
26+
$models = [$models];
27+
}
2628

27-
Assert::assertTrue(
28-
collect($this['rows'])->pluck($property)->contains(e($model->{$property})),
29-
"Response did not contain the expected value: {$model->{$property}}"
30-
);
29+
foreach ($models as $model) {
30+
$guardAgainstNullProperty($model, $property);
31+
32+
Assert::assertTrue(
33+
collect($this['rows'])->pluck($property)->contains(e($model->{$property})),
34+
"Response did not contain the expected value: {$model->{$property}}"
35+
);
36+
}
3137

3238
return $this;
3339
}

0 commit comments

Comments
 (0)