Skip to content

Commit

Permalink
Added user ID checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Effersøe committed Sep 19, 2022
1 parent ec26f69 commit 9c2b64e
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/Http/Adapters/Nemligcom/RecipeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static function adapt(array $data): Recipe
'number' => $persons,
'work_time' => $workTime,
'cooking_time' => $cookingTime,
'user_id' => Auth()->user()->id,
]);

return $recipe;
Expand Down
1 change: 1 addition & 0 deletions app/Http/Adapters/Valdemarsrodk/RecipeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static function adapt(array $data): Recipe
'number' => $persons,
'work_time' => $workTime,
'cooking_time' => $cookingTime,
'user_id' => Auth()->user()->id,
]);

return $recipe;
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/Api/RecipeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Api;

use App\Http\Services\AccessService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Models\Recipe;
Expand All @@ -27,7 +28,10 @@ public function index()
*/
public function show($id)
{
//dd(auth()->user());
/** @var Recipe $recipe */
$recipe = Recipe::with('ingredients')->find($id);
//AccessService::hasAccess($recipe);
return response()->json($recipe);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/ScrapeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function index(Request $request, ScrapeService $scrapeService)
$recipe = $scrapeService->scrapeAndSave($url);
return response()->json(['recipe' => $recipe->toArray(), 'url' => '/recipes/' . $recipe->id]);
} catch (Exception $e) {
Log::error('Exception found in '. __METHOD__ . ' with url: ' . $url);
Log::error('Exception found in '. __METHOD__ . ' with url: ' . $url, ['e' => $e->getMessage()]);
return response()->json(['error' => 'No scraper found for provided domain or some other error has occured'], 501);
}
}
Expand Down
13 changes: 9 additions & 4 deletions app/Http/Controllers/RecipeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace App\Http\Controllers;

use App\Http\Services\AccessService;
use App\Http\Services\RecipeService;
use App\Models\Ingredient;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use App\Models\Recipe;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\View\View;

class RecipeController extends Controller
Expand All @@ -20,7 +18,7 @@ class RecipeController extends Controller
*/
public function index(): View
{
$recipes = DB::table('recipes')->orderBy('title')->get();
$recipes = Recipe::ForUser()->orderBy('title')->get();
return view('recipes.list', ['recipes' => $recipes]);
}

Expand Down Expand Up @@ -65,6 +63,7 @@ public function store(Request $request): RedirectResponse
public function show(Request $request, int $id): View
{
$recipe = Recipe::find($id);
AccessService::hasAccess($recipe);

$totalTime = (int) $recipe->work_time + (int) $recipe->cooking_time;
return view('recipes.show', [
Expand All @@ -81,6 +80,8 @@ public function edit(int $id): View
{
/** @var Recipe $recipe */
$recipe = Recipe::find($id);
AccessService::hasAccess($recipe);

$numberOfIngredientFields = count($recipe->ingredients) + 10;
return view('recipes.edit', [
'method' => 'PUT',
Expand All @@ -96,6 +97,10 @@ public function edit(int $id): View
*/
public function update(Request $request, int $id): RedirectResponse
{
/** @var Recipe $recipe */
$recipe = Recipe::find($id);
AccessService::hasAccess($recipe);

/** @var RecipeService $recipeService */
$recipeService = app(RecipeService::class);

Expand Down
17 changes: 17 additions & 0 deletions app/Http/Services/AccessService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Http\Services;

use App\Models\Recipe;

class AccessService
{
public static function hasAccess(Recipe $recipe): bool
{
if ($recipe->user_id !== auth()->user()->id) {
throw new \Exception('You shall not pass', 403);
}

return true;
}
}
3 changes: 2 additions & 1 deletion app/Http/Services/RecipeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Recipe;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class RecipeService
{
Expand Down Expand Up @@ -65,8 +66,8 @@ private function saveRecipe(Request $request, Recipe $recipe): void
$recipe->number = $request->get('number');
$recipe->cooking_time = (int) trim($request->get('cooking_time'));
$recipe->work_time = (int) trim($request->get('work_time'));
$recipe->image_path = '$fileName';
$recipe->active = $request->get('active') ?? false;
$recipe->user_id = Auth()->user()->id;
$recipe->save();
}
}
2 changes: 2 additions & 0 deletions app/Http/Services/ScrapeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function scrapeAndSave(string $url): Recipe
/** @var RecipeAdapterService $adapterService */
$adapterService = app(RecipeAdapterService::class);
$adapter = $adapterService->getAdapter($url);

// TODO modify and use RecipeService to save the adapted content
$recipe = $adapter::adapt($content);
$recipe->save();

Expand Down
13 changes: 13 additions & 0 deletions app/Models/Recipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Model;
use App\Models\Ingredient;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class Recipe extends Model
{
Expand All @@ -24,6 +25,7 @@ class Recipe extends Model
'cooking_time',
'work_time',
'ingredients',
'user_id',
];

/**
Expand All @@ -33,4 +35,15 @@ public function ingredients(): HasMany
{
return $this->hasMany(Ingredient::class, 'recipe_id');
}

public function user(): HasOne
{
return $this->hasOne(User::class, 'id');
}

public function scopeForUser($query)
{
// TODO add column "for_all" to make globally visible recipes
return $query->where('user_id', auth()->user()?->id)->orWhere('user_id', '=', null);
}
}
2 changes: 1 addition & 1 deletion app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RouteServiceProvider extends ServiceProvider
*
* @var string
*/
public const HOME = '/dashboard';
public const HOME = '/';

/**
* The controller namespace for the application.
Expand Down
12 changes: 7 additions & 5 deletions database/factories/RecipeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Database\Factories;

use App\Models\Recipe;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

class RecipeFactory extends Factory
Expand All @@ -24,11 +25,12 @@ public function definition()
return [
'title' => $this->faker->sentence(3),
'body' => $this->faker->paragraphs(10, true),
'number' => $this->faker->numberBetween(1,10),
'cooking_time' => $this->faker->numberBetween(1,120),
'work_time' => $this->faker->numberBetween(1,120),
'active' => $this->faker->numberBetween(0,1),
'image_path' => $this->faker->numberBetween(0,2) . '.jpg',
'number' => $this->faker->numberBetween(1, 10),
'cooking_time' => $this->faker->numberBetween(1, 120),
'work_time' => $this->faker->numberBetween(1, 120),
'active' => $this->faker->numberBetween(0, 1),
'image_path' => $this->faker->numberBetween(0, 2) . '.jpg',
'user_id' => 1,
];
}
}
34 changes: 34 additions & 0 deletions database/migrations/2022_09_15_183505_add_user_to_recipes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddUserToRecipes extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('recipes', function (Blueprint $table) {
$table->bigInteger('user_id')->nullable()->unsigned();
$table->index('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('recipes', function (Blueprint $table) {
//
});
}
}
2 changes: 1 addition & 1 deletion resources/views/auth/register.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<x-auth-card>
<x-slot name="logo">
<a href="/">
<x-application-logo class="w-20 h-20 fill-current text-gray-500" />
<x-logo/>
</a>
</x-slot>

Expand Down
1 change: 1 addition & 0 deletions resources/views/components/logo.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<img class="bg-cover h-12" src="{{ asset('/images/23462.png') }}"/>
11 changes: 10 additions & 1 deletion resources/views/layouts/navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<!-- Logo -->
<div class="flex-shrink-0 flex items-center">
<a href="{{ route('recipes') }}">
<img class="bg-cover h-12" src="{{ asset('/images/23462.png') }}"/>
<x-logo/>
</a>

<div class="max-w-7xl mx-auto pl-6 px-4 sm:px-6 lg:px-8">
Expand All @@ -15,21 +15,30 @@
<div class="max-w-7xl mx-auto pl-6 px-4 sm:px-6 lg:px-8">
<a href="{{ route('recipes') }}">Alle opskrifter</a>
</div>
@if (Auth()->user())
<div class="max-w-7xl mx-auto pl-6 px-4 sm:px-6 lg:px-8">
<a href="{{ route('scrape') }}">Scraper</a>
</div>
<div class="max-w-7xl mx-auto pl-6 px-4 sm:px-6 lg:px-8">
<a href="{{ route('recipes.create') }}">Opret ny</a>
</div>
@endif
</div>
</div>

<!-- Settings Dropdown -->
<div class="hidden sm:flex sm:items-center sm:ml-6">
@if (Auth()->user())
<form method="POST" action="{{ route('logout') }}">
@csrf
<x-button>Log ud</x-button>
</form>
@else
<form method="GET" action="{{ route('login') }}">
@csrf
<x-button>Log ind</x-button>
</form>
@endif
</div>

</div>
Expand Down
17 changes: 15 additions & 2 deletions resources/views/scrape/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,30 @@

<script>
$('#scrapeForm').submit(function (e) {
e.preventDefault();
initiateCopy()
});
function initiateCopy() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
const link = '/api/v1/scrape/';
disableButton();
console.log('{{csrf_token()}}', 'weeeee1');
console.log('{{csrf_token()}}', 'weeeee2');
resetStatusMessage();
$.post(link, {
'url': $('#url').val()
$.ajax(link, {
method: 'POST',
url: $('#url').val(),
data: {
_token: '{{csrf_token()}}'
},
}).fail(function (response) {
console.log(response);
updateStatusMessage(response.responseJSON.error);
Expand Down

0 comments on commit 9c2b64e

Please sign in to comment.