|
1
|
|
|
<?php namespace App\Traits\Users; |
|
2
|
|
|
|
|
3
|
|
|
use App\Models\User; |
|
4
|
|
|
use App\Models\UserActivation; |
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
|
|
7
|
|
|
trait Activates |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @param \App\Models\User $user |
|
11
|
|
|
* |
|
12
|
|
|
* @return \App\Models\UserActivation |
|
13
|
|
|
*/ |
|
14
|
1 |
|
protected function create(User $user) |
|
15
|
|
|
{ |
|
16
|
1 |
|
$activation = new UserActivation(); |
|
17
|
1 |
|
$code = $this->generateActivationCode(); |
|
18
|
1 |
|
$activation->fill(compact('code')); |
|
19
|
1 |
|
$activation->user_id = $user->id; |
|
20
|
1 |
|
$activation->save(); |
|
21
|
|
|
|
|
22
|
1 |
|
return $activation; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param \App\Models\User $user |
|
27
|
|
|
* @param string|null $code |
|
28
|
|
|
* |
|
29
|
|
|
* @return bool |
|
30
|
|
|
*/ |
|
31
|
1 |
|
protected function exists(User $user, $code = null) |
|
32
|
|
|
{ |
|
33
|
1 |
|
$expires = $this->expires(); |
|
34
|
1 |
|
$activation = UserActivation::where('user_id', $user->id)->where('completed', false)->where('created_at', '>', $expires); |
|
35
|
1 |
|
if ($code) { |
|
|
|
|
|
|
36
|
|
|
$activation->where('code', $code); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
return $activation->first() ? true : false; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Returns the expiration date. |
|
44
|
|
|
* |
|
45
|
|
|
* @return \Carbon\Carbon |
|
46
|
|
|
*/ |
|
47
|
1 |
|
protected function expires() |
|
48
|
|
|
{ |
|
49
|
1 |
|
$expires = config('auth.activations.expires', 259200); |
|
50
|
|
|
|
|
51
|
1 |
|
return Carbon::now()->subSeconds($expires); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param \App\Models\User $user |
|
56
|
|
|
* |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function completed(User $user) |
|
60
|
|
|
{ |
|
61
|
|
|
$activation = UserActivation::where('user_id', $user->id)->where('completed', true)->first(); |
|
62
|
|
|
|
|
63
|
|
|
return $activation ? true : false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param \App\Models\User $user |
|
68
|
|
|
* @param string $code |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
public function complete(User $user, $code) |
|
73
|
|
|
{ |
|
74
|
|
|
$expires = $this->expires(); |
|
75
|
|
|
$activation = UserActivation::where('user_id', $user->id)->where('code', $code)->where('completed', false)->where('created_at', '>', $expires)->first(); |
|
76
|
|
|
if ($activation === null) { |
|
77
|
|
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
$activation->fill([ |
|
80
|
|
|
'completed' => true, |
|
81
|
|
|
'completed_at' => Carbon::now(), |
|
82
|
|
|
]); |
|
83
|
|
|
$activation->save(); |
|
84
|
|
|
|
|
85
|
|
|
return true; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Return a random string for an activation code. |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
1 |
|
protected function generateActivationCode() |
|
94
|
|
|
{ |
|
95
|
1 |
|
return str_random(32); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get the e-mail subject line to be used for the reset link email. |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
1 |
|
protected function getActivationEmailSubject() |
|
104
|
|
|
{ |
|
105
|
1 |
|
return trans('auth.activation_email_subject'); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: