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 Illuminate\Http\Request; |
10
|
|
|
|
11
|
|
|
class ActivationController extends Controller |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Create a new authentication controller instance. |
15
|
|
|
*/ |
16
|
5 |
|
public function __construct() |
17
|
|
|
{ |
18
|
5 |
|
$this->middleware('auth', ['only' => ['getCode']]); |
19
|
5 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Request account activation link via email. |
23
|
|
|
* |
24
|
|
|
* @param \Illuminate\Http\Request $request |
25
|
|
|
* |
26
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse |
27
|
|
|
* @throws \App\Exceptions\Users\UserAlreadyActivatedException |
28
|
|
|
*/ |
29
|
1 |
|
public function getCode(Request $request) |
30
|
|
|
{ |
31
|
1 |
|
if (!($user = app('sentinel')->getUser())) { |
32
|
|
|
throw new UserNotFoundException; |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
if (false !== app('sentinel.activations')->completed($user)) { |
36
|
|
|
throw new UserAlreadyActivatedException; |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
app('events')->fire(new RequestedActivationLink($user)); |
40
|
|
|
|
41
|
1 |
|
if ($request->ajax() || $request->wantsJson()) { |
42
|
1 |
|
return response()->json(['message' => 'Activation link sent']); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return redirect()->back()->with('message', 'Activation link sent'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Activate an account [Web only]. |
50
|
|
|
* |
51
|
|
|
* @param \Illuminate\Http\Request $request |
52
|
|
|
* @param \App\Contracts\Registrar $registrar |
53
|
|
|
* @param string $token |
54
|
|
|
* |
55
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
56
|
|
|
*/ |
57
|
1 |
|
public function getProcess(Request $request, Registrar $registrar, $token) |
58
|
|
|
{ |
59
|
1 |
|
if ($request->ajax() || $request->wantsJson()) { |
60
|
1 |
|
throw new NotImplementedException; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$registrar->activate($token); |
64
|
|
|
|
65
|
|
|
return redirect($this->redirectPath())->with('message', 'Activation successful'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Activate an account [JSON-API only]. |
70
|
|
|
* |
71
|
|
|
* @param \Illuminate\Http\Request $request * |
72
|
|
|
* @param \App\Contracts\Registrar $registrar |
73
|
|
|
* |
74
|
|
|
* @return \Illuminate\Http\JsonResponse |
75
|
|
|
*/ |
76
|
2 |
|
public function postProcess(Request $request, Registrar $registrar) |
77
|
|
|
{ |
78
|
2 |
|
if (!$request->ajax() && !$request->wantsJson()) { |
79
|
|
|
throw new NotImplementedException; |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
$registrar->activate(); |
83
|
|
|
|
84
|
1 |
|
return response()->json(['message' => 'Activated']); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the post register / login redirect path. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
private function redirectPath() |
93
|
|
|
{ |
94
|
|
|
if (isset($this->redirectPath)) { |
95
|
|
|
return $this->redirectPath; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return isset($this->redirectTo) ? $this->redirectTo : '/'; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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: