From e5a0f99da477f1dbba20cc82fbcad7d28c0bedf2 Mon Sep 17 00:00:00 2001 From: HDVinnie Date: Sun, 21 Jul 2024 23:49:43 -0400 Subject: [PATCH] fix: #3735 - 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. --- .../Commands/AutoBanDisposableUsers.php | 84 ++++++++++--------- app/Rules/EmailBlacklist.php | 25 +----- config/email-blacklist.php | 14 +--- 3 files changed, 50 insertions(+), 73 deletions(-) diff --git a/app/Console/Commands/AutoBanDisposableUsers.php b/app/Console/Commands/AutoBanDisposableUsers.php index 8b3959710..e379d0e77 100644 --- a/app/Console/Commands/AutoBanDisposableUsers.php +++ b/app/Console/Commands/AutoBanDisposableUsers.php @@ -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!'); + } } } diff --git a/app/Rules/EmailBlacklist.php b/app/Rules/EmailBlacklist.php index 67b2ba40e..c8be458f7 100644 --- a/app/Rules/EmailBlacklist.php +++ b/app/Rules/EmailBlacklist.php @@ -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. */ diff --git a/config/email-blacklist.php b/config/email-blacklist.php index 1f90979f6..ada271103 100644 --- a/config/email-blacklist.php +++ b/config/email-blacklist.php @@ -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, ];