diff --git a/app/Http/Controllers/RecipeController.php b/app/Http/Controllers/RecipeController.php index 6af3fb7..4935d1a 100644 --- a/app/Http/Controllers/RecipeController.php +++ b/app/Http/Controllers/RecipeController.php @@ -15,9 +15,6 @@ class RecipeController extends Controller */ public function __construct(){ $this->middleware('auth', ['except' => ['index','show']]); - - $this->middleware(['permissions:CreateUser.EditUser.DeleteUser.AssignRole'], ['only' => ['index','show']]); - $this->middleware(['permissions:DeleteUser'], ['only' => ['destroy']]); } /** * Display a listing of the resource. @@ -38,7 +35,12 @@ class RecipeController extends Controller } public function edit($id){ + //verify user has permission to access page $recipe=Recipe::with('user')->with('categories')->with('ingredients')->findOrFail($id); + if(!(\Auth::user()->hasPerm('EditRecipe') || \Auth::user()->id == $recipe->user_id)){ + return redirect()->route('recipes.index')->with('message','You don\'t have permission to access this page'); + } + $lists['ingredients']=array_values(RecipeIngredient::get()->sortby('name')->pluck('name')->unique()->toArray()); $lists['measurements']=array_values(RecipeIngredient::get()->sortby('measurement')->pluck('measurement')->unique()->toArray()); $lists['categories']=array_values(RecipeCategory::get()->sortby('name')->pluck('name')->unique()->toArray()); @@ -49,7 +51,11 @@ class RecipeController extends Controller public function update(Request $request, $id) { + //verify user has permission to access page $recipe = Recipe::with('ingredients')->with('categories')->findOrFail($id); + if(!(\Auth::user()->hasPerm('EditRecipe') || \Auth::user()->id == $recipe->user_id)){ + return redirect()->route('recipes.index')->with('message','You don\'t have permission to access this page'); + } $this->validate($request, [ 'name'=>'required|max:500', @@ -235,4 +241,24 @@ class RecipeController extends Controller } return redirect()->route('recipes.index')->with('message','Recipe successfully edited.'); } + + /** + * Remove the specified resource from storage. + * + * @param int $id + * @return \Illuminate\Http\Response + */ + public function destroy($id) + { + //Find and remove user + $recipe = Recipe::with('categories')->with("ingredients")->findOrFail($id); + if(!(\Auth::user()->hasPerm('DeleteRecipe') || \Auth::user()->id == $recipe->user_id)){ + return redirect()->route('recipes.index')->with('message','You don\'t have permission to access this page'); + } + foreach($recipe->categories as $c) $c->delete(); + foreach($recipe->ingredients as $i) $i->delete(); + $recipe->delete(); + + return redirect()->route('recipes.index')->with('message','Recipe successfully deleted.'); + } } diff --git a/database/seeders/PermissionSeeder.php b/database/seeders/PermissionSeeder.php index a1b7fed..c66dfd9 100644 --- a/database/seeders/PermissionSeeder.php +++ b/database/seeders/PermissionSeeder.php @@ -23,6 +23,9 @@ class PermissionSeeder extends Seeder ['name'=>'Create Role', 'category'=>'Roles', 'details'=>'Create New Roles'], ['name'=>'Edit Role', 'category'=>'Roles', 'details'=>'Edit Existing Roles'], ['name'=>'Delete Role', 'category'=>'Roles', 'details'=>'Delete Existing Roles'], + + ['name'=>'Edit Recipe', 'category'=>'Recipes', 'details'=>'Edit Recipes from other Users'], + ['name'=>'Delete Recipe', 'category'=>'Recipes', 'details'=>'Delete Recipes from other Users'], ]; foreach ($permissions as $key => $value) { Permission::updateOrCreate($value); diff --git a/resources/views/layouts/segments/logout.blade.php b/resources/views/layouts/segments/logout.blade.php index 895c1e8..8aa7c0e 100644 --- a/resources/views/layouts/segments/logout.blade.php +++ b/resources/views/layouts/segments/logout.blade.php @@ -1,4 +1,4 @@ -
  • - Logout +
  • + Logout
  • diff --git a/resources/views/recipes/show.blade.php b/resources/views/recipes/show.blade.php index c863006..64605d5 100644 --- a/resources/views/recipes/show.blade.php +++ b/resources/views/recipes/show.blade.php @@ -2,7 +2,13 @@ @extends('content_wrappers.md-10') @section('title', ' | '.$recipe->name) -@section('heading', $recipe->name) +@section('heading') + @if(Auth::user() && (in_array('EditRecipe',$allperms) || $recipe->user_id == Auth::user()->id)) + {{$recipe->name}} + @else + {{$recipe->name }} + @endif +@endsection @section('content')