1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of laravel.su package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types = 1); |
9
|
|
|
|
10
|
|
|
namespace App\GraphQL\Mutations; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
use App\GraphQL\Types\AuthType; |
14
|
|
|
use App\Models\User; |
15
|
|
|
use App\Services\TokenAuth; |
16
|
|
|
use GraphQL\Type\Definition\ObjectType; |
17
|
|
|
use GraphQL\Type\Definition\Type; |
18
|
|
|
use Illuminate\Contracts\Auth\Guard; |
19
|
|
|
use Illuminate\Encryption\Encrypter; |
20
|
|
|
use Illuminate\Support\Facades\Hash; |
21
|
|
|
use Tymon\JWTAuth\JWTAuth; |
22
|
|
|
|
23
|
|
|
class AuthorisationMutation extends AbstractMutation |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
protected $attributes = [ |
27
|
|
|
'name' => 'authorization' |
28
|
|
|
]; |
29
|
|
|
/** |
30
|
|
|
* @var Guard |
31
|
|
|
*/ |
32
|
|
|
private $jwt; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Encrypter |
36
|
|
|
*/ |
37
|
|
|
private $encrypter; |
38
|
|
|
/** |
39
|
|
|
* @var TokenAuth |
40
|
|
|
*/ |
41
|
|
|
private $guard; |
42
|
|
|
|
43
|
|
|
public function __construct($attributes = [], TokenAuth $guard) |
44
|
|
|
{ |
45
|
|
|
parent::__construct($attributes); |
46
|
|
|
$this->guard = $guard; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return ObjectType |
51
|
|
|
*/ |
52
|
|
|
public function type(): ObjectType |
53
|
|
|
{ |
54
|
|
|
return \GraphQL::type(AuthType::getName()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function args():array |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
'login' => ['name' => 'login', |
64
|
|
|
'type' => Type::string()], |
65
|
|
|
'password' => ['name' => 'password', |
66
|
|
|
'type' => Type::string()] |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function rules():array |
74
|
|
|
{ |
75
|
|
|
return [ |
76
|
|
|
'login' => ['required'], |
77
|
|
|
'password' => ['required'] |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function messages():array |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
|
|
'login.required' => 'Укажите логин', |
88
|
|
|
'password.required' => 'Укажите пароль' |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param \Illuminate\Validation\Validator $validator |
94
|
|
|
* @param $args |
95
|
|
|
*/ |
96
|
|
|
public function afterValidation($validator, $args) |
97
|
|
|
{ |
98
|
|
|
if (!empty($args['login'])) { |
99
|
|
|
$user = User::whereName($args['login'])->orWhere('email', $args['login'])->first(); |
100
|
|
|
/**@var $user User */ |
101
|
|
|
if (empty($user)) { |
102
|
|
|
$validator->errors()->add('login', 'Пользователя с таким логином не существует'); |
103
|
|
|
} else { |
104
|
|
|
if (!empty($args['password'])) { |
105
|
|
|
if (!Hash::check($args['password'], $user->password)) { |
106
|
|
|
$validator->errors()->add('login', 'Связка логин и пароль не совпадают'); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $root |
115
|
|
|
* @param $args |
116
|
|
|
* @return User|mixed |
117
|
|
|
*/ |
118
|
|
|
public function resolve($root, $args) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$user = User::whereName($args['login'])->orWhere('email', $args['login'])->first(); |
121
|
|
|
$user->token = $this->guard->fromUser($user); |
122
|
|
|
return $user; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.