Skip to content

A free open source IT asset/license management system

Added #18555 - ability to clone components

snipe0662e3351e6a
diff --git a/app/Http/Controllers/Components/ComponentsController.php b/app/Http/Controllers/Components/ComponentsController.php
index 2b204ff456..7824212e8c 100644
--- a/app/Http/Controllers/Components/ComponentsController.php
+++ b/app/Http/Controllers/Components/ComponentsController.php
@@ -7,8 +7,8 @@ use App\Http\Requests\ImageUploadRequest;
use App\Models\Company;
use App\Models\Component;
use App\Helpers\Helper;
-use Illuminate\Support\Facades\Auth;
-use Illuminate\Support\Facades\Input;
+use Illuminate\Contracts\View\View;
+use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Log;
@@ -228,4 +228,18 @@ class ComponentsController extends Controller
$this->authorize('view', $component);
return view('components/view', compact('component'))->with('snipe_component', $component);
}
+
+ public function getClone(Component $component) : View | RedirectResponse
+ {
+ $this->authorize('create', Component::class);
+
+ $cloned_component = clone $component;
+ $cloned_component->id = null;
+ $cloned_component->deleted_at = null;
+
+ // Show the page
+ return view('components/edit')
+ ->with('item', $cloned_component)
+ ->with('component', $cloned_component);
+ }
}
diff --git a/app/Http/Transformers/ComponentsTransformer.php b/app/Http/Transformers/ComponentsTransformer.php
index a87176f31f..967c5470c7 100644
--- a/app/Http/Transformers/ComponentsTransformer.php
+++ b/app/Http/Transformers/ComponentsTransformer.php
@@ -74,6 +74,7 @@ class ComponentsTransformer
'checkout' => Gate::allows('checkout', Component::class),
'checkin' => Gate::allows('checkin', Component::class),
'update' => Gate::allows('update', Component::class),
+ 'clone' => Gate::allows('create', Component::class),
'delete' => $component->isDeletable(),
];
$array += $permissions_array;
diff --git a/app/Providers/BreadcrumbsServiceProvider.php b/app/Providers/BreadcrumbsServiceProvider.php
index 93c41612fe..b018c7ea6e 100644
--- a/app/Providers/BreadcrumbsServiceProvider.php
+++ b/app/Providers/BreadcrumbsServiceProvider.php
@@ -214,6 +214,12 @@ class BreadcrumbsServiceProvider extends ServiceProvider
->push(trans('general.breadcrumb_button_actions.edit_item', ['name' => $component->name]), route('components.edit', $component))
);
+ Breadcrumbs::for('components.clone.create', fn (Trail $trail, Component $component) =>
+ $trail->parent('components.index', route('components.index'))
+ ->push($component->display_name, route('components.show', $component))
+ ->push(trans('general.clone'), route('components.create'))
+ );
+
/**
* Consumables Breadcrumbs
diff --git a/resources/views/components/view.blade.php b/resources/views/components/view.blade.php
index a0d9d3d85e..d84af0e14a 100644
--- a/resources/views/components/view.blade.php
+++ b/resources/views/components/view.blade.php
@@ -86,6 +86,8 @@
@endcan
<x-button.wide-edit :item="$snipe_component" :route="route('components.edit', $snipe_component->id)" />
+ <x-button.wide-clone :item="$snipe_component" :route="route('components.clone.create', $snipe_component->id)" />
+
<x-button.wide-delete :item="$snipe_component" />
diff --git a/routes/web/components.php b/routes/web/components.php
index cbc7b70899..fa66ab5ad6 100644
--- a/routes/web/components.php
+++ b/routes/web/components.php
@@ -25,6 +25,11 @@ Route::group(['prefix' => 'components', 'middleware' => ['auth']], function () {
[Components\ComponentCheckinController::class, 'store']
)->name('components.checkin.store');
+ Route::get('{component}/clone',
+ [Components\ComponentsController::class, 'getClone']
+ )->name('components.clone.create');
+
+
});
diff --git a/tests/Feature/Components/Ui/CloneComponentTest.php b/tests/Feature/Components/Ui/CloneComponentTest.php
new file mode 100644
index 0000000000..95a80f7f77
--- /dev/null
+++ b/tests/Feature/Components/Ui/CloneComponentTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Tests\Feature\Components\Ui;
+
+use App\Models\Component;
+use App\Models\User;
+use Tests\TestCase;
+
+class CloneComponentTest extends TestCase
+{
+ public function testPermissionRequiredToCreateComponent()
+ {
+ $component = Component::factory()->create();
+ $this->actingAs(User::factory()->create())
+ ->get(route('components.clone.create', $component))
+ ->assertForbidden();
+ }
+
+ public function testPageCanBeAccessed(): void
+ {
+ $component = Component::factory()->create();
+ $response = $this->actingAs(User::factory()->createComponents()->create())
+ ->get(route('components.clone.create', $component));
+ $response->assertStatus(200);
+ }
+
+ public function testComponentCanBeCloned()
+ {
+ $component_to_clone = Component::factory()->create(['name'=>'Component to clone']);
+ $this->actingAs(User::factory()->createComponents()->create())
+ ->get(route('components.clone.create', $component_to_clone))
+ ->assertOk()
+ ->assertSee([
+ 'Component to clone'
+ ], false);
+ }
+}