teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

2021/04/28 11:09

投稿

RuiShunsin
RuiShunsin

スコア4

title CHANGED
File without changes
body CHANGED
@@ -118,6 +118,8 @@
118
118
 
119
119
  ### 試したこと
120
120
  policyを利用しようとしましたが、自分の理解が浅くうまく実装できていません。
121
+ エラーはとくにでず、
122
+ ログインしたユーザーが他の人が投稿した記事を編集できる状況になっています。
121
123
  ```php
122
124
 
123
125
  sample/src/app/Policies/PostPolicy.php

2

試したことの修正

2021/04/28 11:09

投稿

RuiShunsin
RuiShunsin

スコア4

title CHANGED
File without changes
body CHANGED
@@ -117,10 +117,204 @@
117
117
  ```
118
118
 
119
119
  ### 試したこと
120
- ミドルウェアを利用することで、上記の記述を利用、別の場所に反映させるこでスッキリさせることができるのではないかと考えましたが、うまくきませんでした
120
+ policyを利用しようましたが、自分の理解が浅くうまく実装でていません。
121
- [サイトURL](https://readouble.com/laravel/6.x/ja/middleware.html)
121
+ ```php
122
122
 
123
+ sample/src/app/Policies/PostPolicy.php
124
+ <?php
123
125
 
126
+ namespace App\Policies;
127
+
128
+ use App\Post;
129
+ use App\User;
130
+ use Illuminate\Auth\Access\HandlesAuthorization;
131
+
132
+ class PostPolicy
133
+ {
134
+ use HandlesAuthorization;
135
+
136
+ /**
137
+ * Determine whether the user can view any posts.
138
+ *
139
+ * @param \App\User $user
140
+ * @return mixed
141
+ */
142
+ public function viewAny(User $user)
143
+ {
144
+ //
145
+ }
146
+
147
+ /**
148
+ * Determine whether the user can view the post.
149
+ *
150
+ * @param \App\User $user
151
+ * @param \App\Post $post
152
+ * @return mixed
153
+ */
154
+ public function view(User $user, Post $post)
155
+ {
156
+ //
157
+ }
158
+
159
+ /**
160
+ * Determine whether the user can create posts.
161
+ *
162
+ * @param \App\User $user
163
+ * @return mixed
164
+ */
165
+ public function edit(Post $post)
166
+ {
167
+ if (!(\Auth::user()->can('admin') || \Auth::user()->id == $post->user_id)) {
168
+ abort(403);
169
+ }
170
+
171
+ /**
172
+ * Determine whether the user can update the post.
173
+ *
174
+ * @param \App\User $user
175
+ * @param \App\Post $post
176
+ * @return mixed
177
+ */
178
+ public function update(User $user, Post $post)
179
+ {
180
+ if (!(\Auth::user()->can('admin') || \Auth::user()->id == $post->user_id)) {
181
+ abort(403);
182
+ }
183
+
184
+ ```
185
+
186
+
187
+ ```php
188
+
189
+ sample/src/app/Providers/AuthServiceProvider.php
190
+
191
+ <?php
192
+
193
+ namespace App\Providers;
194
+
195
+ use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
196
+ use Illuminate\Support\Facades\Gate;
197
+
198
+ class AuthServiceProvider extends ServiceProvider
199
+ {
200
+ /**
201
+ * The policy mappings for the application.
202
+ *
203
+ * @var array
204
+ */
205
+ protected $policies = [
206
+ Post::class => PostPolicy::class,
207
+ ];
208
+
209
+ /**
210
+ * Register any authentication / authorization services.
211
+ *
212
+ * @return void
213
+ */
214
+ public function boot()
215
+ {
216
+
217
+ $this->registerPolicies();
218
+ }
219
+ }
220
+
221
+ ```
222
+
223
+ ```php
224
+ sample/src/app/Http/Controllers/PostsController.php
225
+
226
+ <?php
227
+
228
+ namespace App\Http\Controllers;
229
+
230
+ use Illuminate\Http\Request;
231
+ use App\Post;
232
+ use App\Http\Requests\PostRequest;
233
+ use Illuminate\Support\Facades\Auth;
234
+
235
+
236
+ class PostsController extends Controller
237
+ {
238
+ public function __construct()
239
+ {
240
+ $this->middleware('auth')->except(['index', 'show']);
241
+ }
242
+
243
+
244
+ public function index()
245
+ {
246
+ $posts = Post::orderBy('created_at', 'desc')->paginate(10);
247
+ return view('bbs.index', ['posts' => $posts]);
248
+ }
249
+
250
+ public function show(Request $request, $id)
251
+ {
252
+ $post = Post::findOrFail($id);
253
+
254
+ return view('bbs.show', [
255
+ 'post' => $post,
256
+ ]);
257
+ }
258
+
259
+ public function create()
260
+ {
261
+ return view('bbs.create');
262
+ }
263
+
264
+ public function store(PostRequest $request)
265
+ {
266
+ $savedata = [
267
+ 'name' => $request->name,
268
+ 'subject' => $request->subject,
269
+ 'message' => $request->message,
270
+ 'user_id' => Auth::id(),
271
+
272
+ ];
273
+
274
+ $post = new Post;
275
+ $post->fill($savedata)->save();
276
+
277
+ return redirect('/bbs')->with('poststatus', '新規投稿しました');
278
+ }
279
+
280
+ public function edit($post_id)
281
+ {
282
+ $post = Post::findOrFail($post_id);
283
+ return view('bbs.edit', ['post' => $post]);
284
+ }
285
+
286
+ public function update(PostRequest $request, Post $post)
287
+ {
288
+ $savedata = [
289
+ 'name' => $request->name,
290
+ 'subject' => $request->subject,
291
+ 'message' => $request->message,
292
+ 'user_id' => Auth::user()->id,
293
+
294
+ ];
295
+
296
+ $post = new Post;
297
+ $post->fill($savedata)->save();
298
+
299
+ return redirect('/bbs')->with('poststatus', '投稿を編集しました');
300
+ }
301
+
302
+
303
+ public function destroy($id)
304
+ {
305
+ $post = Post::findOrFail($id); {
306
+ if (!(\Auth::user()->can('admin') || \Auth::user()->id == $post->user_id)) {
307
+ abort(403);
308
+ }
309
+ }
310
+ $post->comments()->delete();
311
+ $post->delete();
312
+
313
+ return redirect('/bbs')->with('poststatus', '投稿を削除しました');
314
+ }
315
+ }
316
+
317
+ ```
124
318
  ### 補足情報(FW/ツールのバージョンなど)
125
319
  laravel 6.5
126
320
  どうぞよろしくお願い致します。

1

ソースコードの編集

2021/04/28 10:56

投稿

RuiShunsin
RuiShunsin

スコア4

title CHANGED
File without changes
body CHANGED
@@ -5,7 +5,6 @@
5
5
  if (!(\Auth::user()->can('admin') || \Auth::user()->id == $post->user_id)) {
6
6
  abort(403);
7
7
  }
8
- コード
9
8
  ```
10
9
  アドバイスいただけると幸いです。
11
10