回答編集履歴

12

リネーム

2019/10/16 09:37

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -1,10 +1,20 @@
1
- いろいろ方法はありますが,標準の `Auth::attempt()` を使う方法に乗るのであれば,`UserProvider` を自分で実装する方法が一番きれいなのでお勧めしておきます。
1
+ いろいろ方法はありますが,標準の `Auth::attempt()` を使う方法に乗るのであれば,`UserProvider` を自分で実装する方法が一番きれいなのでお勧めしておきます。
2
2
 
3
3
 
4
4
 
5
5
  - 方法: [認証 6.0 Laravel](https://readouble.com/laravel/6.0/ja/authentication.html)
6
6
 
7
7
  - 継承するデフォルト実装: [auth/EloquentUserProvider.php](https://github.com/illuminate/auth/blob/6.x/EloquentUserProvider.php)
8
+
9
+
10
+
11
+ `LoginController::credentials()` をオーバーライドした上でこちらも実装してください。
12
+
13
+
14
+
15
+ - HTTP リクエストからクレデンシャル配列を取り出す責務 → `LoginController::credentials()`
16
+
17
+ - クレデンシャル配列を使ってデータベースから取ってきてユーザオブジェクトまたはNULLを返す責務 → `UserProvider::retrieveByCredentials(array $credentials)`
8
18
 
9
19
 
10
20
 
@@ -32,7 +42,7 @@
32
42
 
33
43
 
34
44
 
35
- class AppUserProvider extends EloquentUserProivder
45
+ class StatusAwareEloquentUserProvider extends EloquentUserProivder
36
46
 
37
47
  {
38
48
 
@@ -96,9 +106,9 @@
96
106
 
97
107
 
98
108
 
99
- Auth::provider('app', function (Application $app, array $config) {
109
+ Auth::provider('status_aware_eloquent', function (Application $app, array $config) {
100
110
 
101
- return new AppUserProvider($app['hash'], $config['model']);
111
+ return new StatusAwareEloquentUserProvider($app['hash'], $config['model']);
102
112
 
103
113
  });
104
114
 
@@ -116,7 +126,7 @@
116
126
 
117
127
  'users' => [
118
128
 
119
- 'driver' => 'app',
129
+ 'driver' => 'status_aware_eloquent',
120
130
 
121
131
  'model' => App\User::class,
122
132
 

11

バージョン追従

2019/10/16 09:37

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  - 方法: [認証 6.0 Laravel](https://readouble.com/laravel/6.0/ja/authentication.html)
6
6
 
7
- - 継承するデフォルト実装: [auth/EloquentUserProvider.php](https://github.com/illuminate/auth/blob/a8a03aa650eadfa8e2a1a11709fa7de47ca859f3/EloquentUserProvider.php)
7
+ - 継承するデフォルト実装: [auth/EloquentUserProvider.php](https://github.com/illuminate/auth/blob/6.x/EloquentUserProvider.php)
8
8
 
9
9
 
10
10
 

10

newModelQuery() が生えていたのでショートハンドに変更

2019/10/16 09:27

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- - 方法: [認証 5.6 Laravel](https://readouble.com/laravel/5.6/ja/authentication.html)
5
+ - 方法: [認証 6.0 Laravel](https://readouble.com/laravel/6.0/ja/authentication.html)
6
6
 
7
7
  - 継承するデフォルト実装: [auth/EloquentUserProvider.php](https://github.com/illuminate/auth/blob/a8a03aa650eadfa8e2a1a11709fa7de47ca859f3/EloquentUserProvider.php)
8
8
 
@@ -44,7 +44,7 @@
44
44
 
45
45
 
46
46
 
47
- return $this->createModel()->newQuery()
47
+ return $this->newModelQuery()
48
48
 
49
49
  ->whereEmail($credentials['email'])
50
50
 

9

remove unused

2019/10/16 09:25

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -27,8 +27,6 @@
27
27
 
28
28
 
29
29
  use Illuminate\Auth\EloquentUserProivder;
30
-
31
- use Illuminate\Support\Arr;
32
30
 
33
31
  use Illuminate\Contracts\Auth\Authenticatable;
34
32
 

8

implements → extends

2019/06/11 08:32

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -34,7 +34,7 @@
34
34
 
35
35
 
36
36
 
37
- class AppUserProvider implements EloquentUserProivder
37
+ class AppUserProvider extends EloquentUserProivder
38
38
 
39
39
  {
40
40
 

7

typo

2019/06/11 08:31

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -34,7 +34,7 @@
34
34
 
35
35
 
36
36
 
37
- class AppUserProvider implements EloquentUserProivder;
37
+ class AppUserProvider implements EloquentUserProivder
38
38
 
39
39
  {
40
40
 

6

space

2018/04/14 09:46

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -98,7 +98,7 @@
98
98
 
99
99
 
100
100
 
101
- Auth::provider('app', function(Application $app, array $config) {
101
+ Auth::provider('app', function (Application $app, array $config) {
102
102
 
103
103
  return new AppUserProvider($app['hash'], $config['model']);
104
104
 

5

app

2018/04/14 09:45

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -80,6 +80,8 @@
80
80
 
81
81
  use Illuminate\Support\Facades\Auth;
82
82
 
83
+ use Illuminate\Foundation\Application;
84
+
83
85
  use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
84
86
 
85
87
 
@@ -96,9 +98,9 @@
96
98
 
97
99
 
98
100
 
99
- Auth::provider('app', function($app, array $config) {
101
+ Auth::provider('app', function(Application $app, array $config) {
100
102
 
101
- return new AppUserProvider($this->app['hash'], $config['model']);
103
+ return new AppUserProvider($app['hash'], $config['model']);
102
104
 
103
105
  });
104
106
 

4

追記

2018/04/14 09:44

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
 
14
14
 
15
- ```
15
+ ```PHP
16
16
 
17
17
  <?php
18
18
 
@@ -59,3 +59,69 @@
59
59
  }
60
60
 
61
61
  ```
62
+
63
+
64
+
65
+ ```PHP
66
+
67
+ <?php
68
+
69
+
70
+
71
+ declare(strict_types=1);
72
+
73
+
74
+
75
+ namespace App\Providers;
76
+
77
+
78
+
79
+ use App\Auth\AppUserProvider;
80
+
81
+ use Illuminate\Support\Facades\Auth;
82
+
83
+ use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
84
+
85
+
86
+
87
+ class AuthServiceProvider extends ServiceProvider
88
+
89
+ {
90
+
91
+ public function boot(): void
92
+
93
+ {
94
+
95
+ $this->registerPolicies();
96
+
97
+
98
+
99
+ Auth::provider('app', function($app, array $config) {
100
+
101
+ return new AppUserProvider($this->app['hash'], $config['model']);
102
+
103
+ });
104
+
105
+ }
106
+
107
+ }
108
+
109
+ ```
110
+
111
+
112
+
113
+ ```PHP
114
+
115
+ 'providers' => [
116
+
117
+ 'users' => [
118
+
119
+ 'driver' => 'app',
120
+
121
+ 'model' => App\User::class,
122
+
123
+ ],
124
+
125
+ ],
126
+
127
+ ```

3

strict_types

2018/04/14 09:32

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -8,9 +8,17 @@
8
8
 
9
9
 
10
10
 
11
+ ### 実装例 (PHP7.1以降向け)
12
+
13
+
14
+
11
15
  ```
12
16
 
13
17
  <?php
18
+
19
+
20
+
21
+ declare(strict_types=1);
14
22
 
15
23
 
16
24
 

2

型指定

2018/04/14 09:20

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -22,29 +22,19 @@
22
22
 
23
23
  use Illuminate\Support\Arr;
24
24
 
25
+ use Illuminate\Contracts\Auth\Authenticatable;
26
+
25
27
 
26
28
 
27
29
  class AppUserProvider implements EloquentUserProivder;
28
30
 
29
31
  {
30
32
 
31
- /**
32
-
33
- * Retrieve a user by the given credentials.
34
-
35
- *
36
-
37
- * @param array $credentials
38
-
39
- * @return \Illuminate\Contracts\Auth\Authenticatable|null
40
-
41
- */
42
-
43
- public function retrieveByCredentials(array $credentials)
33
+ public function retrieveByCredentials(array $credentials): ?Authenticatable
44
34
 
45
35
  {
46
36
 
47
- if (!isset($credentials['email'])) return;
37
+ if (!isset($credentials['email'])) return null;
48
38
 
49
39
 
50
40
 

1

追加

2018/04/14 09:18

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -2,6 +2,62 @@
2
2
 
3
3
 
4
4
 
5
- - [認証 5.6 Laravel](https://readouble.com/laravel/5.6/ja/authentication.html)
5
+ - 方法: [認証 5.6 Laravel](https://readouble.com/laravel/5.6/ja/authentication.html)
6
6
 
7
- - [auth/EloquentUserProvider.php at a8a03aa650eadfa8e2a1a11709fa7de47ca859f3 · illuminate/auth](https://github.com/illuminate/auth/blob/a8a03aa650eadfa8e2a1a11709fa7de47ca859f3/EloquentUserProvider.php)
7
+ - 継承するデフォルト実装: [auth/EloquentUserProvider.php](https://github.com/illuminate/auth/blob/a8a03aa650eadfa8e2a1a11709fa7de47ca859f3/EloquentUserProvider.php)
8
+
9
+
10
+
11
+ ```
12
+
13
+ <?php
14
+
15
+
16
+
17
+ namespace App\Auth;
18
+
19
+
20
+
21
+ use Illuminate\Auth\EloquentUserProivder;
22
+
23
+ use Illuminate\Support\Arr;
24
+
25
+
26
+
27
+ class AppUserProvider implements EloquentUserProivder;
28
+
29
+ {
30
+
31
+ /**
32
+
33
+ * Retrieve a user by the given credentials.
34
+
35
+ *
36
+
37
+ * @param array $credentials
38
+
39
+ * @return \Illuminate\Contracts\Auth\Authenticatable|null
40
+
41
+ */
42
+
43
+ public function retrieveByCredentials(array $credentials)
44
+
45
+ {
46
+
47
+ if (!isset($credentials['email'])) return;
48
+
49
+
50
+
51
+ return $this->createModel()->newQuery()
52
+
53
+ ->whereEmail($credentials['email'])
54
+
55
+ ->whereIn('status', [1, 4])
56
+
57
+ ->first();
58
+
59
+ }
60
+
61
+ }
62
+
63
+ ```