make permission for recipe edit

This commit is contained in:
2024-01-21 18:39:53 +03:00
parent 06f5aaaa96
commit 59aa4d79a1
4 changed files with 29 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Models\Recipe;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Gate;
class RecipeController extends Controller
{
@@ -37,6 +38,14 @@ class RecipeController extends Controller
public function edit(Recipe $recipe): View|ViewFactory
{
if (! $this->canEditRecipe($recipe)) {
abort(403, 'Unauthorized action.');
}
return view('recipe/edit_form', ['recipe' => $recipe]);
}
private function canEditRecipe(Recipe $recipe): bool
{
return Gate::allows('edit', $recipe);
}
}

View File

@@ -189,7 +189,8 @@ class Edit extends Component
if (is_string($step['image'])) {
$data['image'] = $step['image'];
} else {
$data['image'] = $step['image']->store('steps', 'public');
// $data['image'] = $step['image']->store('steps', 'public');
$data['image'] = $step['image']->store('steps');
}
$data['description'] = $step['description'];

View File

@@ -3,7 +3,9 @@
namespace App\Providers;
use App\Models\Plan;
use App\Models\Recipe;
use App\Policies\PlanPolicy;
use App\Policies\RecipePolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
@@ -15,6 +17,7 @@ class AuthServiceProvider extends ServiceProvider
*/
protected $policies = [
Plan::class => PlanPolicy::class,
Recipe::class => RecipePolicy::class,
// 'App\Models\Model' => 'App\Policies\ModelPolicy',
];

View File

@@ -37,13 +37,24 @@ final class EditTest extends TestCase
$this->recipe = Recipe::inRandomOrder()->first();
}
public function test_edit_page_recipe_success(): void
public function test_show_form_edit_recipe_success_for_author(): void
{
$user = $this->user;
$recipe = Recipe::factory()->create();
$user->author_of_recipe()->attach($recipe->id);
$response = $this
->actingAs($user)
->get(route('recipe.edit', $recipe));
$response->assertSuccessful();
}
public function test_show_form_edit_recipe_forbidden_for_not_author(): void
{
$response = $this
->actingAs($this->user)
->get(route('recipe.edit', $this->recipe, false));
$response->assertSuccessful();
->get(route('recipe.edit', $this->recipe));
$response->assertForbidden();
}
public function testUpdateRecipePublishSuccess(): void