|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Logic\Activation; |
|
4
|
|
|
|
|
5
|
|
|
use App\Activation; |
|
6
|
|
|
use App\Models\User; |
|
7
|
|
|
use App\Notifications\SendActivationEmail; |
|
8
|
|
|
use App\Notifications\SendActivationEmailApi; |
|
|
|
|
|
|
9
|
|
|
// use App\Traits\CaptureIpTrait; |
|
10
|
|
|
use Carbon\Carbon; |
|
11
|
|
|
use Str; |
|
12
|
|
|
class ActivationRepository |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Creates a token and send email. |
|
16
|
|
|
* |
|
17
|
|
|
* @param \App\Models\User $user |
|
18
|
|
|
* |
|
19
|
|
|
* @return bool or void |
|
20
|
|
|
*/ |
|
21
|
|
|
public function createTokenAndSendEmail(User $user) |
|
22
|
|
|
{ |
|
23
|
|
|
$activations = Activation::where('user_id', $user->id) |
|
|
|
|
|
|
24
|
|
|
->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod'))) |
|
25
|
|
|
->count(); |
|
26
|
|
|
|
|
27
|
|
|
// if ($activations >= config('settings.maxAttempts')) { |
|
28
|
|
|
// return true; |
|
29
|
|
|
// } |
|
30
|
|
|
|
|
31
|
|
|
//if user changed activated email to new one |
|
32
|
|
|
if ($user->email_verified_at) { |
|
|
|
|
|
|
33
|
|
|
$user->update([ |
|
34
|
|
|
'email_verified_at' => null, |
|
35
|
|
|
]); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// Create new Activation record for this user |
|
39
|
|
|
$activation = self::createNewActivationToken($user); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
// Send activation email notification |
|
42
|
|
|
self::sendNewActivationEmail($user, $activation->token); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Creates a new activation token. |
|
49
|
|
|
* |
|
50
|
|
|
* @param \App\Models\User $user |
|
51
|
|
|
* |
|
52
|
|
|
* @return \App\Models\Activation $activation |
|
|
|
|
|
|
53
|
|
|
*/ |
|
54
|
|
|
public function createNewActivationToken(User $user) |
|
55
|
|
|
{ |
|
56
|
|
|
// $ipAddress = new CaptureIpTrait(); |
|
57
|
|
|
$activation = new Activation(); |
|
58
|
|
|
$activation->user_id = $user->id; |
|
59
|
|
|
$activation->token = Str::random(64); |
|
60
|
|
|
// $activation->ip_address = $ipAddress->getClientIp(); |
|
61
|
|
|
$activation->save(); |
|
62
|
|
|
|
|
63
|
|
|
return $activation; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Sends a new activation email. |
|
68
|
|
|
* |
|
69
|
|
|
* @param \App\Models\User $user The user |
|
70
|
|
|
* @param string $token The token |
|
71
|
|
|
*/ |
|
72
|
|
|
public function sendNewActivationEmail(User $user, $token) |
|
73
|
|
|
{ |
|
74
|
|
|
$user->notify(new SendActivationEmail($token)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Creates a token and send email.- api |
|
80
|
|
|
* |
|
81
|
|
|
* @param \App\Models\User $user |
|
82
|
|
|
* |
|
83
|
|
|
* @return bool or void |
|
84
|
|
|
*/ |
|
85
|
|
|
public function createTokenAndSendEmailApi(User $user) |
|
86
|
|
|
{ |
|
87
|
|
|
$activations = Activation::where('user_id', $user->id) |
|
88
|
|
|
->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod'))) |
|
89
|
|
|
->count(); |
|
90
|
|
|
|
|
91
|
|
|
if ($activations >= config('settings.maxAttempts')) { |
|
92
|
|
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
//if user changed activated email to new one |
|
96
|
|
|
if ($user->activated) { |
|
|
|
|
|
|
97
|
|
|
$user->update([ |
|
98
|
|
|
'activated' => false, |
|
99
|
|
|
]); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// Create new Activation record for this user |
|
103
|
|
|
$activation = self::createNewActivationTokenApi($user); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
// Send activation email notification |
|
106
|
|
|
self::sendNewActivationEmailApi($user, $activation->token); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Creates a new activation token. |
|
111
|
|
|
* |
|
112
|
|
|
* @param \App\Models\User $user |
|
113
|
|
|
* |
|
114
|
|
|
* @return \App\Models\Activation $activation |
|
115
|
|
|
*/ |
|
116
|
|
|
public function createNewActivationTokenApi(User $user) |
|
117
|
|
|
{ |
|
118
|
|
|
$token = sprintf("%06d", mt_rand(1, 999999)); |
|
119
|
|
|
// $ipAddress = new CaptureIpTrait(); |
|
120
|
|
|
$activation = new Activation(); |
|
121
|
|
|
$activation->user_id = $user->id; |
|
122
|
|
|
$activation->token = $token; |
|
123
|
|
|
// $activation->ip_address = $ipAddress->getClientIp(); |
|
124
|
|
|
$activation->save(); |
|
125
|
|
|
|
|
126
|
|
|
return $activation; |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Sends a new activation email. |
|
131
|
|
|
* |
|
132
|
|
|
* @param \App\Models\User $user The user |
|
133
|
|
|
* @param string $token The token |
|
134
|
|
|
*/ |
|
135
|
|
|
public function sendNewActivationEmailApi(User $user, $token) |
|
136
|
|
|
{ |
|
137
|
|
|
$user->notify(new SendActivationEmailApi($token)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Method to removed expired activations. |
|
142
|
|
|
* |
|
143
|
|
|
* @return void |
|
144
|
|
|
*/ |
|
145
|
|
|
public function deleteExpiredActivations() |
|
146
|
|
|
{ |
|
147
|
|
|
Activation::where('created_at', '<=', Carbon::now()->subHours(72))->delete(); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths