掲示板の投稿者のみ編集できるようにしたい(Laravel)
前提・実現したいこと
Laravelを使用して掲示板を作成しています。投稿者のみ投稿内容の編集・更新ができるようにしたいです。ログイン機能の実装は上手く行きましたが、投稿者であるにも関わらず投稿内容の編集が出来ない状態です。
発生している問題・エラーメッセージ
ポリシーを作成・登録し、userのid と postのuser_idが合致した場合のみ編集・更新できるように記述、コントローラでauthorizeによる認可を追加しました。しかし、投稿者かどうかに関わらず、編集ボタンを押すと403 This action is unauthorized.とブラウザに表示されてしまいます。
該当のソースコード
App/Http/Providers/AuthServiceProvider.php
PHP
1<?php 2 3namespace App\Providers; 4 5use App\Post; 6use App\Policies\PostPolicy; 7use App\User; 8use App\Policies\UserPolicy; 9use Illuminate\Support\Facades\Gate; 10use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; 11 12class AuthServiceProvider extends ServiceProvider 13{ 14 /** 15 * The policy mappings for the application. 16 * 17 * @var array 18 */ 19 protected $policies = [ 20 'App\Model' => 'App\Policies\ModelPolicy', 21 // 'App\Model' => 'App\Policies\ModelPolicy', 22 User::class => UserPolicy::class, 23 Post::class => PostPolicy::class, 24 ]; 25 26 /** 27 * Register any authentication / authorization services. 28 * 29 * @return void 30 */ 31 public function boot() 32 { 33 $this->registerPolicies(); 34 config(['admin_id' => 1]); 35 // 36 } 37} 38
App/Http/Policies/PostPolicy.php
PHP
1<?php 2 3namespace App\Policies; 4use App\Post; 5use App\User; 6use Illuminate\Auth\Access\HandlesAuthorization; 7 8class PostPolicy 9{ 10 use HandlesAuthorization; 11 12 /** 13 * Create a new policy instance. 14 * 15 * @return void 16 */ 17 public function __construct() 18 { 19 // 20 } 21 22 public function edit(User $user, Post $post) 23 { 24 return $user->id === $post->user_id; 25 } 26 27 public function update(User $user, Post $post) 28 { 29 return $user->id === $post->user_id; 30 } 31 32 33 34}
App/Http/Policies/UserPolicy.php
PHP
1<?php 2 3namespace App\Policies; 4 5use App\Post; 6use App\User; 7use Illuminate\Auth\Access\HandlesAuthorization; 8 9class UserPolicy 10{ 11 use HandlesAuthorization; 12 13 /** 14 * Create a new policy instance. 15 * 16 * @return void 17 */ 18 public function __construct() 19 { 20 // 21 } 22 23 public function edit(User $user, User $model) 24 { 25 return $user->id == $model->id; 26 } 27 28 public function update(User $user, User $model) 29 { 30 return $user->id == $model->id; 31 } 32}
App/Http/Controllers/PostsController.php
PHP
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use App\Post; 7use App\Http\Controllers\Controller; 8class PostsController extends Controller 9{ 10 public function index() 11 { 12 return view('posts.top'); 13 14 } 15 16 public function bbs() 17 { 18 $posts = Post::with(['comments'])->orderBy('created_at', 'desc')->paginate(10); 19 return view('posts.bbs',['posts'=>$posts]); 20 } 21 22 public function create() 23 { 24 return view('posts.create'); 25 } 26 27 public function store(Request $request) 28 { 29 $params = $request->validate([ 30 'title' => 'required|max:50', 31 'body' => 'required|max:2000', 32 ]); 33 34 auth()->user()->posts()->create($params); 35 //Post::create($params); 36 37 return redirect()->route('bbsTop'); 38 } 39 40 public function show($post_id) 41 { 42 $post=Post::findOrFail($post_id); 43 44 return view('posts.show',[ 45 'post'=>$post, 46 ]); 47 } 48 49 public function edit($post_id) 50 { 51 52 $post=Post::findOrFail($post_id); 53 $this->authorize('edit', $post); 54 return view('posts.edit', [ 55 'post' => $post, 56 ]); 57 } 58 59 public function update($post_id,Request $request) 60 { 61 $params=$request->validate([ 62 'title'=>'required|max:50', 63 'body'=>'required|max:2000', 64 ]); 65 $post=Post::findOrFail($post_id); 66 $this->authorize('update', $post); 67 $post->fill($params)->save(); 68 return redirect()->route('posts.show',['post'=>$post]); 69 } 70 71 public function destroy($post_id) 72 { 73 $post=Post::findOrFail($post_id); 74 75 \DB::transaction(function() use ($post){ 76 $post->comments()->delete(); 77 $post->delete(); 78 }); 79 80 return redirect()->route('bbsTop'); 81 } 82 83 84 } 85
database/migrations/posts_table.php
PHP
1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreatePostsTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('posts', function (Blueprint $table) { 17 $table->increments('id'); 18 $table->unsignedInteger('user_id'); 19 $table->string('title',50); 20 $table->text('body'); 21 $table->timestamps(); 22 23 $table->foreign('user_id')->references('id')->on('users'); 24 }); 25 } 26 27 /** 28 * Reverse the migrations. 29 * 30 * @return void 31 */ 32 public function down() 33 { 34 Schema::dropIfExists('posts'); 35 } 36}
database/migrations/users_table.php
PHP
1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateUsersTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('users', function (Blueprint $table) { 17 $table->bigIncrements('id'); 18 $table->string('name'); 19 $table->string('email')->unique(); 20 $table->timestamp('email_verified_at')->nullable(); 21 $table->string('password'); 22 $table->rememberToken(); 23 $table->timestamps(); 24 }); 25 } 26 27 /** 28 * Reverse the migrations. 29 * 30 * @return void 31 */ 32 public function down() 33 { 34 Schema::dropIfExists('users'); 35 } 36}
App/Http/Providers/Post.php
PHP
1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6 7class Post extends Model 8{ 9 protected $fillable = [ 10 'title', 11 'body', 12 ]; 13 14 public function comments() 15 { 16 return $this->hasMany('App\Comment'); 17 } 18 19 public function user() 20 { 21 return $this->belongsTo('App\User'); 22 } 23} 24
App/Http/Providers/User.php
PHP
1<?php 2 3namespace App; 4 5use Illuminate\Notifications\Notifiable; 6use Illuminate\Contracts\Auth\MustVerifyEmail; 7use Illuminate\Foundation\Auth\User as Authenticatable; 8 9class User extends Authenticatable 10{ 11 use Notifiable; 12 13 /** 14 * The attributes that are mass assignable. 15 * 16 * @var array 17 */ 18 protected $fillable = [ 19 'name', 'email', 'password', 20 ]; 21 22 /** 23 * The attributes that should be hidden for arrays. 24 * 25 * @var array 26 */ 27 protected $hidden = [ 28 'password', 'remember_token', 29 ]; 30 31 /** 32 * The attributes that should be cast to native types. 33 * 34 * @var array 35 */ 36 protected $casts = [ 37 'email_verified_at' => 'datetime', 38 ]; 39 40 public function posts() 41 { 42 return $this->hasMany('App\Post'); 43 } 44 45 46} 47
試したこと
参照サイトを参考にポリシーの作成・登録、ルールの記述、コントローラに認可を追加しました。何度やっても上手くいかなかったので、Post.php,User.phpと、テーブルも確認し記述し直しましたが、投稿者IDとユーザーIDを合致させて認可することができませんでした。
参照サイト:https://readouble.com/laravel/5.7/ja/authorization.html#writing-policies
https://qiita.com/sutara79/items/11d8417a6fc91ca1b841
https://tech.windii.jp/backend/laravel/authorization-basic
補足情報(FW/ツールのバージョンなど)
Laravel Framework 5.8.35
DB SQLite
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/12/23 14:29
2019/12/23 16:19
2019/12/24 10:15
2019/12/24 11:32
2019/12/25 13:55
2019/12/25 14:24
2019/12/26 13:48
2019/12/26 14:30
2019/12/26 14:51 編集
2019/12/26 15:10