Skip to content

Commit

Permalink
[add]CommentControllerの各アクションにPolicyのチェックを追加
Browse files Browse the repository at this point in the history
追加権限、編集権限、削除権限を確認して適切な画面へ繊維させる
  • Loading branch information
inaka-phper committed Sep 12, 2015
1 parent 89780ee commit 382c548
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions laravel/app/Http/Controllers/PostCommentController.php
Expand Up @@ -10,6 +10,7 @@
use App\Http\Controllers\Controller;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;

class PostCommentController extends Controller
{
Expand Down Expand Up @@ -71,6 +72,10 @@ public function create()
*/
public function store(Request $request)
{
if (Gate::denies('create', $this->comment)) {
return redirect('/auth/login')->with('message', 'コメントするにはログインしてください。');
}

$this->comment->fill($request->all());
$this->comment->user_id = $this->user->id;
$this->comment->post_id = $this->post->id;
Expand Down Expand Up @@ -99,9 +104,13 @@ public function show($id)
public function edit(Route $route)
{
$id = $route->parameter('comment');
$comment = $this->comment->findOrFail($id);
$this->comment = $this->comment->findOrFail($id);

return view('post.comment.edit', ['comment' => $comment]);
if (Gate::denies('update', $this->comment)) {
return redirect('/post/' . $this->post->id)->with('message', '編集できるのは投稿者と管理者のみです。');
}

return view('post.comment.edit', ['comment' => $this->comment]);
//
}

Expand All @@ -116,6 +125,11 @@ public function update(Request $request, Route $route)
{
$id = $route->parameter('comment');
$this->comment = $this->comment->findOrFail($id);

if (Gate::denies('update', $this->comment)) {
return redirect('/post/' . $this->post->id)->with('message', '編集できるのは投稿者と管理者のみです。');
}

$this->comment->fill($request->all());
$this->comment->save();

Expand All @@ -131,7 +145,13 @@ public function update(Request $request, Route $route)
public function destroy(Route $route)
{
$id = $route->parameter('comment');
$this->comment->destroy($id);
$this->comment = $this->comment->findOrFail($id);

if (Gate::denies('delete', [$this->comment, $this->post])) {
return redirect('/post/' . $this->post->id)->with('message', '削除できるのは投稿者と記事の投稿者、管理者のみです。');
}

$this->comment->delete();

return redirect('/post/' . $this->post->id)->with('message', 'コメントを削除しました。');
}
Expand Down

0 comments on commit 382c548

Please sign in to comment.