1
|
|
|
<?php namespace App\Http\Controllers\Users; |
2
|
|
|
|
3
|
|
|
use App\Contracts\Registrar; |
4
|
|
|
use App\Events\Users\RequestedActivationLink; |
5
|
|
|
use App\Exceptions\Common\NotImplementedException; |
6
|
|
|
use App\Exceptions\Users\UserAlreadyActivatedException; |
7
|
|
|
use App\Exceptions\Users\UserNotFoundException; |
8
|
|
|
use App\Http\Controllers\Controller; |
9
|
|
|
use App\Models\User; |
10
|
|
|
use App\Traits\Users\Activates; |
11
|
|
|
use Illuminate\Http\Request; |
12
|
|
|
|
13
|
|
|
class ActivationController extends Controller |
14
|
|
|
{ |
15
|
|
|
use Activates; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Create a new authentication controller instance. |
19
|
|
|
*/ |
20
|
5 |
|
public function __construct() |
21
|
|
|
{ |
22
|
5 |
|
$this->middleware('auth', ['only' => ['requestActivationCode']]); |
23
|
5 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Request account activation link via email. |
27
|
|
|
* |
28
|
|
|
* @param \Illuminate\Http\Request $request |
29
|
|
|
* |
30
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse |
31
|
|
|
* @throws \App\Exceptions\Users\UserAlreadyActivatedException |
32
|
|
|
*/ |
33
|
2 |
|
public function requestActivationCode(Request $request) |
34
|
|
|
{ |
35
|
|
|
/** @var User $user */ |
36
|
2 |
|
$user = app('auth.driver')->user(); |
37
|
|
|
|
38
|
2 |
|
if (false !== $this->completed($user)) { |
39
|
1 |
|
throw new UserAlreadyActivatedException; |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
app('events')->fire(new RequestedActivationLink($user)); |
43
|
|
|
|
44
|
1 |
|
if ($request->ajax() || $request->wantsJson()) { |
45
|
1 |
|
return response()->json(['message' => 'Activation link sent']); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return redirect()->back()->with('message', 'Activation link sent'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Activate an account [Web only]. |
53
|
|
|
* |
54
|
|
|
* @param \Illuminate\Http\Request $request |
55
|
|
|
* @param \App\Contracts\Registrar $registrar |
56
|
|
|
* @param string|null $token |
57
|
|
|
* |
58
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse |
59
|
|
|
*/ |
60
|
3 |
|
public function activate(Request $request, Registrar $registrar, $token = null) |
61
|
2 |
|
{ |
62
|
3 |
|
switch ($requestMethod = $request->getMethod()) { |
63
|
3 |
|
case 'GET': |
64
|
1 |
|
if ($request->ajax() || $request->wantsJson()) { |
65
|
1 |
|
throw new NotImplementedException; |
66
|
|
|
} |
67
|
|
|
break; |
68
|
2 |
|
case 'POST': |
69
|
2 |
|
if (!$request->ajax() && !$request->wantsJson()) { |
70
|
|
|
throw new NotImplementedException; |
71
|
|
|
} |
72
|
2 |
|
break; |
73
|
2 |
|
} |
74
|
|
|
|
75
|
2 |
|
$registrar->activate($token); |
76
|
|
|
|
77
|
1 |
|
return $requestMethod == 'GET' ? redirect($this->redirectPath())->with('message', 'Activation successful') : response()->json(['message' => 'Activated']); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the post register / login redirect path. |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
private function redirectPath() |
86
|
|
|
{ |
87
|
|
|
if (isset($this->redirectPath)) { |
88
|
|
|
return $this->redirectPath; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return isset($this->redirectTo) ? $this->redirectTo : '/'; |
|
|
|
|
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: