- We dont need to auto update anymore. This is killer in loops. We already have a command that updates on weekends. We wrap the command in a if else that only executes if cache is present in case of redis restart.
This commit is contained in:
HDVinnie
2024-07-21 23:49:43 -04:00
parent 0a2894eabb
commit e5a0f99da4
3 changed files with 50 additions and 73 deletions

View File

@@ -52,50 +52,54 @@ class AutoBanDisposableUsers extends Command
{
$bannedGroup = cache()->rememberForever('banned_group', fn () => Group::where('slug', '=', 'banned')->pluck('id'));
User::where('group_id', '!=', $bannedGroup[0])->chunkById(100, function ($users) use ($bannedGroup): void {
foreach ($users as $user) {
$v = validator([
'email' => $user->email,
], [
'email' => [
'required',
'string',
'email',
'max:70',
new EmailBlacklist(),
],
]);
if (cache()->has(config('email-blacklist.cache-key'))) {
User::where('group_id', '!=', $bannedGroup[0])->chunkById(100, function ($users) use ($bannedGroup): void {
foreach ($users as $user) {
$v = validator([
'email' => $user->email,
], [
'email' => [
'required',
'string',
'email',
'max:70',
new EmailBlacklist(),
],
]);
if ($v->fails()) {
// If User Is Using A Disposable Email Set The Users Group To Banned
$user->group_id = $bannedGroup[0];
$user->can_upload = 0;
$user->can_download = 0;
$user->can_comment = 0;
$user->can_invite = 0;
$user->can_request = 0;
$user->can_chat = 0;
$user->save();
if ($v->fails()) {
// If User Is Using A Disposable Email Set The Users Group To Banned
$user->group_id = $bannedGroup[0];
$user->can_upload = 0;
$user->can_download = 0;
$user->can_comment = 0;
$user->can_invite = 0;
$user->can_request = 0;
$user->can_chat = 0;
$user->save();
// Log The Ban To Ban Log
$domain = substr((string) strrchr((string) $user->email, '@'), 1);
$logban = new Ban();
$logban->owned_by = $user->id;
$logban->created_by = User::SYSTEM_USER_ID;
$logban->ban_reason = 'Detected disposable email, '.$domain.' not allowed.';
$logban->unban_reason = '';
$logban->save();
// Log The Ban To Ban Log
$domain = substr((string) strrchr((string) $user->email, '@'), 1);
$logban = new Ban();
$logban->owned_by = $user->id;
$logban->created_by = User::SYSTEM_USER_ID;
$logban->ban_reason = 'Detected disposable email, '.$domain.' not allowed.';
$logban->unban_reason = '';
$logban->save();
// Send Email
$user->notify(new UserBan($logban));
// Send Email
$user->notify(new UserBan($logban));
}
cache()->forget('user:'.$user->passkey);
Unit3dAnnounce::addUser($user);
}
});
cache()->forget('user:'.$user->passkey);
Unit3dAnnounce::addUser($user);
}
});
$this->comment('Automated User Banning Command Complete');
$this->comment('Automated User Banning Command Complete');
} else {
$this->comment('Email Blacklist Cache Key Not Found. Skipping!');
}
}
}

View File

@@ -36,7 +36,8 @@ class EmailBlacklist implements ValidationRule
public function validate(string $attribute, mixed $value, Closure $fail): void
{
// Load blacklisted domains
$this->refresh();
$this->domains = cache()->get(config('email-blacklist.cache-key'));
$this->appendCustomDomains();
// Extract domain from supplied email address
$domain = Str::after(strtolower((string) $value), '@');
@@ -47,28 +48,6 @@ class EmailBlacklist implements ValidationRule
}
}
/**
* Retrive the latest selection of blacklisted domains and cache them.
*/
public function refresh(): void
{
$this->shouldUpdate();
$this->domains = cache()->get(config('email-blacklist.cache-key'));
$this->appendCustomDomains();
}
/**
* Should update blacklist?.
*/
protected function shouldUpdate(): void
{
$autoupdate = config('email-blacklist.auto-update');
if ($autoupdate && !cache()->has(config('email-blacklist.cache-key'))) {
EmailBlacklistUpdater::update();
}
}
/**
* Append custom defined blacklisted domains.
*/

View File

@@ -33,20 +33,14 @@ return [
| You may change the cache key for the sourced blacklist.
| Keep null if you want to use the default value.
|
| auto-update: true|false
| Specify if should automatically get source when cache is empty.
| ADVICE: This may slow down the first request upon validation.
| Default: true
|
| append: string|null
| You may a string of pipe | separated domains list.
| Keep null if you don't want to append custom domains.
| Example: "example.com|example.net|foobar.com".
|
*/
'enabled' => true,
'source' => 'https://cdn.jsdelivr.net/gh/andreis/disposable-email-domains@master/domains.json',
'cache-key' => 'email.domains.blacklist',
'auto-update' => true,
'append' => null,
'enabled' => true,
'source' => 'https://cdn.jsdelivr.net/gh/andreis/disposable-email-domains@master/domains.json',
'cache-key' => 'email.domains.blacklist',
'append' => null,
];