1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Http\Controllers\Api\Invitation; |
6
|
|
|
|
7
|
|
|
use App\Contracts\Http\Responses\ResponseFactory; |
8
|
|
|
use App\Http\Requests\Api\Invitation\AcceptRequest; |
9
|
|
|
use App\Models\User; |
10
|
|
|
use Illuminate\Auth\Events\PasswordReset; |
11
|
|
|
use Illuminate\Auth\Passwords\PasswordBrokerManager; |
12
|
|
|
use Illuminate\Contracts\Auth\PasswordBroker; |
13
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
14
|
|
|
use Illuminate\Contracts\Hashing\Hasher; |
15
|
|
|
use Illuminate\Contracts\Translation\Translator; |
16
|
|
|
use Illuminate\Http\JsonResponse; |
17
|
|
|
use Illuminate\Http\Response; |
18
|
|
|
use Illuminate\Support\Str; |
19
|
|
|
|
20
|
|
|
final class Accept |
21
|
|
|
{ |
22
|
|
|
private PasswordBrokerManager $passwordBrokerManager; |
23
|
|
|
|
24
|
|
|
private Hasher $hasher; |
25
|
|
|
|
26
|
|
|
private Dispatcher $eventDispatcher; |
27
|
|
|
|
28
|
|
|
private ResponseFactory $responseFactory; |
29
|
|
|
|
30
|
|
|
private Translator $translator; |
31
|
|
|
|
32
|
3 |
|
public function __construct( |
33
|
|
|
PasswordBrokerManager $passwordBrokerManager, |
34
|
|
|
Hasher $hasher, |
35
|
|
|
Dispatcher $eventDispatcher, |
36
|
|
|
ResponseFactory $responseFactory, |
37
|
|
|
Translator $translator |
38
|
|
|
) { |
39
|
3 |
|
$this->passwordBrokerManager = $passwordBrokerManager; |
40
|
3 |
|
$this->hasher = $hasher; |
41
|
3 |
|
$this->eventDispatcher = $eventDispatcher; |
42
|
3 |
|
$this->responseFactory = $responseFactory; |
43
|
3 |
|
$this->translator = $translator; |
44
|
3 |
|
} |
45
|
|
|
|
46
|
2 |
|
public function __invoke(AcceptRequest $request): JsonResponse |
47
|
|
|
{ |
48
|
2 |
|
$credentials = $request->only(['token', 'email', 'password', 'password_confirmation']); |
49
|
|
|
|
50
|
|
|
$response = $this->getPasswordBroker()->reset($credentials, function (User $user, string $password) { |
51
|
1 |
|
$user->setAttribute('password', $this->hasher->make($password)); |
52
|
|
|
|
53
|
1 |
|
$user->setRememberToken(Str::random(60)); |
54
|
|
|
|
55
|
1 |
|
$user->save(); |
56
|
|
|
|
57
|
1 |
|
$this->eventDispatcher->dispatch(new PasswordReset($user)); |
58
|
2 |
|
}); |
59
|
|
|
|
60
|
2 |
|
if (PasswordBroker::PASSWORD_RESET === $response) { |
61
|
1 |
|
return $this->responseFactory->json([ |
62
|
1 |
|
'message' => $this->translator->get($response), |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
return $this->responseFactory->json([ |
67
|
1 |
|
'message' => $this->translator->get(PasswordBroker::INVALID_TOKEN), |
68
|
1 |
|
], Response::HTTP_BAD_REQUEST); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
private function getPasswordBroker(): PasswordBroker |
72
|
|
|
{ |
73
|
2 |
|
return $this->passwordBrokerManager->broker('user-invitations'); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|