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

質問編集履歴

10

policyとmodelを全文追記

2021/02/08 11:21

投稿

hiro_app
hiro_app

スコア123

title CHANGED
File without changes
body CHANGED
@@ -36,7 +36,10 @@
36
36
 
37
37
  ・Laravel HogePolicy(一部)
38
38
  ```
39
+ <?php
40
+
39
41
  namespace App\Policies;
42
+
40
43
  use App\Models\Admin\Hoge;
41
44
  use App\Models\Admin\User;
42
45
  use Illuminate\Auth\Access\HandlesAuthorization;
@@ -44,24 +47,110 @@
44
47
  class HogePolicy
45
48
  {
46
49
  use HandlesAuthorization;
50
+
51
+ /**
52
+ * Determine whether the user can view any hoges.
53
+ *
54
+ * @param \App\Models\Admin\User $user
55
+ * @return mixed
56
+ */
57
+ public function viewAny(User $user)
58
+ {
59
+ //
60
+ }
61
+
62
+ /**
63
+ * Determine whether the user can view the hoge.
64
+ *
65
+ * @param \App\Models\Admin\User $user
66
+ * @param \App\Hoge $hoge
67
+ * @return mixed
68
+ */
47
69
  public function view(User $user, Hoge $hoge)
48
70
  {
49
- \Log::debug('call view policy'); // ←ログ出力されない
71
+ \Log::debug('-------------------- call view policy --------------------');
50
72
  return false;
51
73
  }
52
74
  public function edit(User $user, Hoge $hoge)
53
75
  {
54
- \Log::debug('call edit policy'); // ←ログ出力されない
76
+ \Log::debug('-------------------- call edit policy --------------------');
55
77
  return false;
56
78
  }
79
+
80
+ /**
81
+ * Determine whether the user can create hoges.
82
+ *
83
+ * @param \App\Models\Admin\User $user
84
+ * @return mixed
85
+ */
86
+ public function create(User $user)
87
+ {
88
+ //
89
+ }
90
+
91
+ /**
92
+ * Determine whether the user can update the hoge.
93
+ *
94
+ * @param \App\Models\Admin\User $user
95
+ * @param \App\Hoge $hoge
96
+ * @return mixed
97
+ */
98
+ public function update(User $user, Hoge $hoge)
99
+ {
100
+ //
101
+ }
102
+
103
+ /**
104
+ * Determine whether the user can delete the hoge.
105
+ *
106
+ * @param \App\Models\Admin\User $user
107
+ * @param \App\Hoge $hoge
108
+ * @return mixed
109
+ */
110
+ public function delete(User $user, Hoge $hoge)
111
+ {
112
+ //
113
+ }
114
+
115
+ /**
116
+ * Determine whether the user can restore the hoge.
117
+ *
118
+ * @param \App\Models\Admin\User $user
119
+ * @param \App\Hoge $hoge
120
+ * @return mixed
121
+ */
122
+ public function restore(User $user, Hoge $hoge)
123
+ {
124
+ //
125
+ }
126
+
127
+ /**
128
+ * Determine whether the user can permanently delete the hoge.
129
+ *
130
+ * @param \App\Models\Admin\User $user
131
+ * @param \App\Hoge $hoge
132
+ * @return mixed
133
+ */
134
+ public function forceDelete(User $user, Hoge $hoge)
135
+ {
136
+ //
137
+ }
57
138
  }
58
139
  ```
59
140
 
60
141
  ・Laravel Hogeモデル
61
142
  ```
143
+ <?php
144
+
62
145
  namespace App\Models\Admin;
63
146
 
147
+ use Eloquent as Model;
148
+
149
+ /**
150
+ * Class Hoge
151
+ */
64
152
  class Hoge extends Model
65
153
  {
154
+ public $table = 'hogehoge';
66
155
  }
67
156
  ```

9

policyへログ出力処理追加

2021/02/08 11:21

投稿

hiro_app
hiro_app

スコア123

title CHANGED
File without changes
body CHANGED
@@ -46,10 +46,12 @@
46
46
  use HandlesAuthorization;
47
47
  public function view(User $user, Hoge $hoge)
48
48
  {
49
+ \Log::debug('call view policy'); // ←ログ出力されない
49
50
  return false;
50
51
  }
51
52
  public function edit(User $user, Hoge $hoge)
52
53
  {
54
+ \Log::debug('call edit policy'); // ←ログ出力されない
53
55
  return false;
54
56
  }
55
57
  }

8

インスタンス生成方法を修正

2021/02/08 10:54

投稿

hiro_app
hiro_app

スコア123

title CHANGED
File without changes
body CHANGED
@@ -27,7 +27,8 @@
27
27
  ```
28
28
  public function edit($id)
29
29
  {
30
- $hoge= $this->Hoge->find($id); // Hogeモデルの検索結果取得OK
30
+ // $hoge= $this->Hoge->find($id); // Hogeモデルの検索結果取得OK
31
+ $hoge= (new Hoge)::find($id); // ↑と結果は同じ
31
32
  $this->authorize('edit', $hoge);
32
33
  // dd(policy(Hoge::class)); // 「^ App\Policies\HogePolicy {#724}」と表示されるのでAuthServiceProviderの設定は上手くいっている?
33
34
  }

7

policy出力結果追加

2021/02/08 10:44

投稿

hiro_app
hiro_app

スコア123

title CHANGED
File without changes
body CHANGED
@@ -29,6 +29,7 @@
29
29
  {
30
30
  $hoge= $this->Hoge->find($id); // Hogeモデルの検索結果取得OK
31
31
  $this->authorize('edit', $hoge);
32
+ // dd(policy(Hoge::class)); // 「^ App\Policies\HogePolicy {#724}」と表示されるのでAuthServiceProviderの設定は上手くいっている?
32
33
  }
33
34
  ```
34
35
 

6

$policiesから不要パターン削除、伴い説明文一部削除

2021/02/05 05:20

投稿

hiro_app
hiro_app

スコア123

title CHANGED
File without changes
body CHANGED
@@ -1,11 +1,7 @@
1
1
  ### LaravelのPolicy処理が呼び出されず、認証制御ができない
2
2
 
3
- AuthServiceProvider の $policies に存在ないモデルや下記例のようなデタラメな数字書いもエラーとならないため、ここの設定が読み込めていないような気がするのですが
3
+ 期待値とてはPolicyでfalse返していためアクセス拒否となっほしですが、普通に画面が表示されてしまいます。
4
- どこの設定が誤っているせいなのかが解らない状態です。
5
4
 
6
- 期待値としてはPolicyでfalseを返しているためアクセス拒否 or $policiesでデタラメな設定のせいでエラー
7
- となってほしい所ですが、普通に画面が表示されてしまいます。
8
-
9
5
  ### 該当のソースコードサンプル
10
6
 
11
7
  ・Laravel AuthServiceProvider
@@ -17,10 +13,7 @@
17
13
  class AuthServiceProvider extends ServiceProvider
18
14
  {
19
15
  protected $policies = [
20
- Hoge::class => HogePolicy::class, // 存在するモデル・ポリシー パターン1
16
+ Hoge::class => HogePolicy::class
21
- 'App\Models\Admin\Hoge' => 'App\Policies\HogePolicy', // 存在するモデル・ポリシー パターン2
22
- App\Models\Admin\Hoge::class => App\Policies\HogePolicy::class, // 存在するモデル・ポリシー パターン3
23
- 123456 // $policiesが読み込まれているなら、ここが原因でエラーになってほしい
24
17
  ];
25
18
 
26
19
  public function boot()

5

namespace追記

2021/02/05 04:54

投稿

hiro_app
hiro_app

スコア123

title CHANGED
File without changes
body CHANGED
@@ -41,7 +41,9 @@
41
41
 
42
42
  ・Laravel HogePolicy(一部)
43
43
  ```
44
+ namespace App\Policies;
44
45
  use App\Models\Admin\Hoge;
46
+ use App\Models\Admin\User;
45
47
  use Illuminate\Auth\Access\HandlesAuthorization;
46
48
 
47
49
  class HogePolicy
@@ -56,4 +58,13 @@
56
58
  return false;
57
59
  }
58
60
  }
61
+ ```
62
+
63
+ ・Laravel Hogeモデル
64
+ ```
65
+ namespace App\Models\Admin;
66
+
67
+ class Hoge extends Model
68
+ {
69
+ }
59
70
  ```

4

use追記

2021/02/05 04:17

投稿

hiro_app
hiro_app

スコア123

title CHANGED
File without changes
body CHANGED
@@ -10,6 +10,8 @@
10
10
 
11
11
  ・Laravel AuthServiceProvider
12
12
  ```
13
+ use Illuminate\Support\Facades\Gate;
14
+ use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
13
15
  use App\Models\Admin\Hoge;
14
16
  use App\Policies\HogePolicy;
15
17
  class AuthServiceProvider extends ServiceProvider

3

ソースタイトル追記

2021/02/05 01:46

投稿

hiro_app
hiro_app

スコア123

title CHANGED
File without changes
body CHANGED
@@ -8,7 +8,8 @@
8
8
 
9
9
  ### 該当のソースコードサンプル
10
10
 
11
- ```Laravel AuthServiceProvider
11
+ Laravel AuthServiceProvider
12
+ ```
12
13
  use App\Models\Admin\Hoge;
13
14
  use App\Policies\HogePolicy;
14
15
  class AuthServiceProvider extends ServiceProvider
@@ -27,7 +28,8 @@
27
28
  }
28
29
  ```
29
30
 
30
- ```Laravel Controller
31
+ Laravel Controller
32
+ ```
31
33
  public function edit($id)
32
34
  {
33
35
  $hoge= $this->Hoge->find($id); // Hogeモデルの検索結果取得OK
@@ -35,7 +37,8 @@
35
37
  }
36
38
  ```
37
39
 
38
- ```Laravel HogePolicy(一部)
40
+ Laravel HogePolicy(一部)
41
+ ```
39
42
  use App\Models\Admin\Hoge;
40
43
  use Illuminate\Auth\Access\HandlesAuthorization;
41
44
 

2

ソース加筆

2021/02/05 01:43

投稿

hiro_app
hiro_app

スコア123

title CHANGED
File without changes
body CHANGED
@@ -8,7 +8,9 @@
8
8
 
9
9
  ### 該当のソースコードサンプル
10
10
 
11
- ```Laravel
11
+ ```Laravel AuthServiceProvider
12
+ use App\Models\Admin\Hoge;
13
+ use App\Policies\HogePolicy;
12
14
  class AuthServiceProvider extends ServiceProvider
13
15
  {
14
16
  protected $policies = [
@@ -28,12 +30,18 @@
28
30
  ```Laravel Controller
29
31
  public function edit($id)
30
32
  {
31
- $hoge= $this->Hoge->find($id); // 値は取得OK
33
+ $hoge= $this->Hoge->find($id); // Hogeモデルの検索結果取得OK
32
- $this->authorize('view', $hoge);
34
+ $this->authorize('edit', $hoge);
33
35
  }
34
36
  ```
35
37
 
36
- ```Laravel Policy(一部)
38
+ ```Laravel HogePolicy(一部)
39
+ use App\Models\Admin\Hoge;
40
+ use Illuminate\Auth\Access\HandlesAuthorization;
41
+
42
+ class HogePolicy
43
+ {
44
+ use HandlesAuthorization;
37
45
  public function view(User $user, Hoge $hoge)
38
46
  {
39
47
  return false;
@@ -42,4 +50,5 @@
42
50
  {
43
51
  return false;
44
52
  }
53
+ }
45
54
  ```

1

$policiesへパターン3を追加

2021/02/05 01:38

投稿

hiro_app
hiro_app

スコア123

title CHANGED
File without changes
body CHANGED
@@ -13,7 +13,8 @@
13
13
  {
14
14
  protected $policies = [
15
15
  Hoge::class => HogePolicy::class, // 存在するモデル・ポリシー パターン1
16
- 'App\Model\Admin\Hoge' => 'App\Policies\HogePolicy', // 存在するモデル・ポリシー パターン2
16
+ 'App\Models\Admin\Hoge' => 'App\Policies\HogePolicy', // 存在するモデル・ポリシー パターン2
17
+ App\Models\Admin\Hoge::class => App\Policies\HogePolicy::class, // 存在するモデル・ポリシー パターン3
17
18
  123456 // $policiesが読み込まれているなら、ここが原因でエラーになってほしい
18
19
  ];
19
20