Skip to content

Commit 7decb6a

Browse files
committed
check for spam in registration / login post
1 parent a244de4 commit 7decb6a

File tree

4 files changed

+88
-24
lines changed

4 files changed

+88
-24
lines changed

src/Controllers/User.php

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,35 @@
33
namespace Franky5831\CodeIgniter4UserLibrary\Controllers;
44

55
use CodeIgniter\Controller;
6+
use Exception;
67
use Franky5831\CodeIgniter4UserLibrary\Models\User as UserModel;
78

89
class User extends Controller
910
{
11+
private $userModel;
12+
1013
public function __construct()
1114
{
1215
// Loads the user helper
1316
helper('user_helper');
1417
// Adds form validation user rules
1518
config('Validation')->ruleSets[] = \Franky5831\CodeIgniter4UserLibrary\Validation\ValidationRules::class;
19+
20+
$this->userModel = new UserModel();
1621
}
1722

1823
public function login(): \CodeIgniter\HTTP\RedirectResponse|string
1924
{
25+
$config = config(\Franky5831\CodeIgniter4UserLibrary\Config\App::class);
26+
$userCanLogin = $config->userCanLogin;
27+
if (!$userCanLogin) {
28+
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
29+
}
30+
2031
if (isLoggedIn()) {
2132
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
2233
}
34+
2335
$validationRules = [
2436
'email' => [
2537
'label' => 'Email',
@@ -34,13 +46,16 @@ public function login(): \CodeIgniter\HTTP\RedirectResponse|string
3446
$captchaRules = $this->getCaptchaRules();
3547
$validationRules = array_merge($validationRules, $captchaRules);
3648

37-
if ($this->request->getMethod() == "POST" && $this->validate($validationRules)) {
38-
$userModel = new userModel();
39-
$user = $userModel->where("email", $this->request->getPost("email"))->first();
49+
if ($this->request->getMethod() == "POST") {
50+
if ($this->userModel->getUserCanPost() && $this->validate($validationRules)) {
51+
$user = $this->userModel->where("email", $this->request->getPost("email"))->first();
4052

41-
$this->setUserMethod($user);
53+
$this->setUserMethod($user);
4254

43-
return redirect()->to("/");
55+
return redirect()->to("/");
56+
} else {
57+
$this->userModel->setPostError();
58+
}
4459
}
4560
try {
4661
// Returns the view from the app's folder, if it doesn't exist, it returns the vendor's view
@@ -81,27 +96,29 @@ public function register(): \CodeIgniter\HTTP\RedirectResponse|string
8196
$userExtraAttributes = $config->userExtraAttributes;
8297
$validationRules = array_merge($validationRules, $captchaRules, $userExtraAttributes);
8398

84-
if ($this->request->getMethod() == "POST" && $this->validate($validationRules)) {
85-
$userModel = new UserModel();
86-
87-
$userData = [
88-
'email' => $this->request->getPost('email'),
89-
'password' => $this->request->getPost('password'),
90-
];
91-
foreach ($userExtraAttributes as $attribute => $data) {
92-
$attributeValue = $this->request->getPost($attribute);
93-
$userData[$attribute] = $attributeValue;
94-
}
99+
if ($this->request->getMethod() == "POST") {
100+
if ($this->userModel->getUserCanPost() && $this->validate($validationRules)) {
101+
$userData = [
102+
'email' => $this->request->getPost('email'),
103+
'password' => $this->request->getPost('password'),
104+
];
105+
foreach ($userExtraAttributes as $attribute => $data) {
106+
$attributeValue = $this->request->getPost($attribute);
107+
$userData[$attribute] = $attributeValue;
108+
}
95109

96-
$userModel->save($userData);
97-
$session = session();
110+
$this->userModel->save($userData);
111+
$session = session();
98112

99-
$user = $userModel->where("email", $this->request->getPost("email"))->first();
100-
$this->setUserMethod($user);
113+
$user = $this->userModel->where("email", $this->request->getPost("email"))->first();
114+
$this->setUserMethod($user);
101115

102-
$session->setFlashdata('success', "Registrazione avvenuta con successo");
116+
$session->setFlashdata('success', "Registrazione avvenuta con successo");
103117

104-
return redirect()->to('/');
118+
return redirect()->to('/');
119+
} else {
120+
$this->userModel->setPostError();
121+
}
105122
}
106123

107124
try {
@@ -135,9 +152,8 @@ private function getCaptchaRules(): array
135152
throw new \Exception("The selected captcha type does not exists", 1);
136153
break;
137154
}
138-
139-
return $validationRules;
140155
}
156+
return $validationRules;
141157
}
142158

143159
private function setUserMethod($user): void

src/Language/en/Validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
'validate_recaptcha_v3' => 'Captcha is not valid',
66
'validateUser' => 'Email or password are not valid',
77
'validateXss' => 'The {field} field contains invalid characters',
8+
'user_cant_post' => 'You have exceeded the maximum number of errors. Please try again later.',
89
];

src/Language/it/Validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
'validate_recaptcha_v3' => 'Il captcha non è valido',
66
'validateUser' => 'Email o password non sono validi',
77
'validateXss' => 'Il campo {field} contiene caratteri non consentiti',
8+
'user_cant_post' => 'Hai superato il numero massimo di errori. Per favore riprova più tardi.',
89
];

src/Models/User.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,50 @@ protected function passowrdHash(array $data): array
3838
}
3939
return $data;
4040
}
41+
42+
public function setPostError(): void
43+
{
44+
$clientIp = getClientIp();
45+
$cache = service('cache');
46+
47+
$clientIpCacheId = "client_ip_error_" . $clientIp;
48+
$clientIpCacheVal = $cache->get($clientIpCacheId);
49+
if ($clientIpCacheVal) {
50+
$clientIpCacheVal++;
51+
} else {
52+
$clientIpCacheVal = 1;
53+
}
54+
$config = config(\Franky5831\CodeIgniter4UserLibrary\Config\App::class);
55+
$userErrorTimeout = $config->userErrorTimeout;
56+
$cache->save($clientIpCacheId, $clientIpCacheVal, $userErrorTimeout);
57+
}
58+
59+
private function getPostError(): int
60+
{
61+
$clientIp = getClientIp();
62+
$cache = service('cache');
63+
64+
$clientIpCacheId = "client_ip_error_" . $clientIp;
65+
$clientIpCacheVal = $cache->get($clientIpCacheId);
66+
if (!$clientIpCacheVal) {
67+
$clientIpCacheVal = 0;
68+
}
69+
70+
return $clientIpCacheVal;
71+
}
72+
73+
public function getUserCanPost(): bool
74+
{
75+
$config = config(\Franky5831\CodeIgniter4UserLibrary\Config\App::class);
76+
$userPostErrorLogger = $config->userPostErrorLogger;
77+
if ($userPostErrorLogger) {
78+
$maxPostErrors = $config->maxPostErrors;
79+
$userCanPost = $this->getPostError() < $maxPostErrors;
80+
if (!$userCanPost) {
81+
\Config\Services::validation()->setError("user_cant_post", lang('Validation.user_cant_post'));
82+
}
83+
return $userCanPost;
84+
}
85+
return true;
86+
}
4187
}

0 commit comments

Comments
 (0)