|
1
|
|
|
<?php namespace App\Http\Controllers\Auth; |
|
2
|
|
|
|
|
3
|
|
|
use App\Events\Users\RequestedActivationLink; |
|
4
|
|
|
use App\Exceptions\Common\ValidationException; |
|
5
|
|
|
use App\Exceptions\Users\TokenNotValidException; |
|
6
|
|
|
use App\Exceptions\Users\UserAlreadyActivatedException; |
|
7
|
|
|
use App\Http\Controllers\Controller; |
|
8
|
|
|
use App\Models\User; |
|
9
|
|
|
use App\Models\UserActivation; |
|
10
|
|
|
use App\Traits\Users\Activates; |
|
11
|
|
|
use Illuminate\Foundation\Auth\RedirectsUsers; |
|
12
|
|
|
use Illuminate\Http\Request; |
|
13
|
|
|
|
|
14
|
|
|
class ActivateController extends Controller |
|
15
|
|
|
{ |
|
16
|
|
|
use Activates, RedirectsUsers; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Create a new authentication controller instance. |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->middleware('auth', ['only' => ['requestActivationCode']]); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Request account activation link via email. |
|
28
|
|
|
* |
|
29
|
|
|
* @param \Illuminate\Http\Request $request |
|
30
|
|
|
* |
|
31
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse |
|
32
|
|
|
* @throws \App\Exceptions\Users\UserAlreadyActivatedException |
|
33
|
|
|
*/ |
|
34
|
|
|
public function requestActivationCode(Request $request) |
|
35
|
|
|
{ |
|
36
|
|
|
/** @var User $user */ |
|
37
|
|
|
$user = app('auth.driver')->user(); |
|
38
|
|
|
|
|
39
|
|
|
if (false !== $this->completed($user)) { |
|
40
|
|
|
throw new UserAlreadyActivatedException; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
app('events')->fire(new RequestedActivationLink($user)); |
|
44
|
|
|
|
|
45
|
|
|
if ($request->expectsJson()) { |
|
46
|
|
|
return response()->json(['message' => 'Activation link sent']); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return redirect()->back()->with('message', 'Activation link sent'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Activate an account [Web only]. |
|
54
|
|
|
* |
|
55
|
|
|
* @param \Illuminate\Http\Request $request |
|
56
|
|
|
* @param string|null $token |
|
57
|
|
|
* |
|
58
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
|
59
|
|
|
* @throws \App\Exceptions\Common\ValidationException |
|
60
|
|
|
*/ |
|
61
|
|
|
public function activate(Request $request, $token = null) |
|
62
|
|
|
{ |
|
63
|
|
|
$data = !is_null($token) ? ['token' => $token] : $request->all(); |
|
64
|
|
|
$validator = app('validator')->make($data, [ |
|
65
|
|
|
'token' => 'required|string', |
|
66
|
|
|
]); |
|
67
|
|
|
if ($validator->fails()) { |
|
68
|
|
|
throw new ValidationException($validator); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$activation = UserActivation::whereCode($data['token'])->first(); |
|
|
|
|
|
|
72
|
|
|
if (!$activation) { |
|
73
|
|
|
throw new TokenNotValidException; |
|
74
|
|
|
} |
|
75
|
|
|
/** @var \App\Models\User $user */ |
|
76
|
|
|
$user = User::findOrFail($activation->user_id); |
|
77
|
|
|
|
|
78
|
|
|
$this->complete($user, $data['token']); |
|
79
|
|
|
|
|
80
|
|
|
return ($request->expectsJson()) ? response()->json(['message' => 'Activated']) : redirect($this->redirectPath())->with('message', 'Activation successful'); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: