From faa0ecc808c85f73cd8d3ae5942bc15f6af0ee32 Mon Sep 17 00:00:00 2001 From: HDVinnie Date: Sun, 3 Oct 2021 11:10:48 -0400 Subject: [PATCH 1/3] add: auth caching --- app/Helpers/CacheUser.php | 28 +++++++++++++++++ app/Observers/UserObserver.php | 6 ++-- app/Providers/AppServiceProvider.php | 4 ++- app/Providers/AuthServiceProvider.php | 6 ++-- app/Providers/CacheUserProvider.php | 45 +++++++++++++++++++++++++++ config/app.php | 1 + config/auth.php | 18 +++++------ 7 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 app/Helpers/CacheUser.php create mode 100644 app/Providers/CacheUserProvider.php diff --git a/app/Helpers/CacheUser.php b/app/Helpers/CacheUser.php new file mode 100644 index 0000000000..be6008117f --- /dev/null +++ b/app/Helpers/CacheUser.php @@ -0,0 +1,28 @@ + + * @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 + */ + +namespace App\Helpers; + +use App\Models\User; + +class CacheUser { + public static function user($id){ + if(! $id || $id<=0 || ! is_numeric($id)) { + return; + } + + return \cache()->remember('cachedUser.'.$id, 30, function() use($id) { + return User::find($id); + }); + } +} \ No newline at end of file diff --git a/app/Observers/UserObserver.php b/app/Observers/UserObserver.php index beb591fbc6..3f8b9bcee2 100644 --- a/app/Observers/UserObserver.php +++ b/app/Observers/UserObserver.php @@ -29,14 +29,16 @@ public function created(User $user) } /** - * Handle the User "updated" event. + * Handle the User "saved" event. * * * @return void + * @throws \Exception */ - public function updated(User $user) + public function saved(User $user) { //\cache()->put(\sprintf('user:%s', $user->passkey), $user); + \cache()->forget('cachedUser.'.$user->id); } /** diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 02f46d84ff..d6a0430152 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -13,8 +13,10 @@ namespace App\Providers; +use App\Models\User; use App\Helpers\ByteUnits; use App\Helpers\HiddenCaptcha; +use App\Observers\UserObserver; use App\Interfaces\ByteUnitsInterface; use App\Interfaces\WishInterface; use App\Models\Page; @@ -53,7 +55,7 @@ public function register() public function boot() { // User Observer For Cache - //User::observe(UserObserver::class); + User::observe(UserObserver::class); // Torrent Observer For Cache //Torrent::observe(TorrentObserver::class); diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 6457d4eeb1..fea9ec3f3a 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -13,6 +13,7 @@ namespace App\Providers; +use Illuminate\Support\Facades\Auth; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; class AuthServiceProvider extends ServiceProvider @@ -34,7 +35,8 @@ class AuthServiceProvider extends ServiceProvider public function boot() { $this->registerPolicies(); - - // + Auth::provider('cache-user', function() { + return resolve(CacheUserProvider::class); + }); } } diff --git a/app/Providers/CacheUserProvider.php b/app/Providers/CacheUserProvider.php new file mode 100644 index 0000000000..02fcd4a0d3 --- /dev/null +++ b/app/Providers/CacheUserProvider.php @@ -0,0 +1,45 @@ + + * @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 + */ + +namespace App\Providers; + +use App\Models\User; +use Illuminate\Auth\EloquentUserProvider; +use Illuminate\Contracts\Hashing\Hasher as HasherContract; +use App\Helpers\CacheUser; + +class CacheUserProvider extends EloquentUserProvider +{ + public function __construct(HasherContract $hasher) + { + parent::__construct($hasher, User::class); + } + + public function retrieveById($identifier) + { + return CacheUser::user($identifier); + } + + public function retrieveByToken($identifier, $token) + { + $model = CacheUser::user($identifier); + + if (! $model) { + return null; + } + + $rememberToken = $model->getRememberToken(); + + return $rememberToken && hash_equals($rememberToken, $token) ? $model : null; + } +} \ No newline at end of file diff --git a/config/app.php b/config/app.php index eb9bf85ddb..5b82868892 100644 --- a/config/app.php +++ b/config/app.php @@ -215,6 +215,7 @@ 'Broadcast' => Illuminate\Support\Facades\Broadcast::class, 'Bus' => Illuminate\Support\Facades\Bus::class, 'Cache' => Illuminate\Support\Facades\Cache::class, + 'CacheUser' => App\Helpers\CacheUser::class, 'Config' => Illuminate\Support\Facades\Config::class, 'Cookie' => Illuminate\Support\Facades\Cookie::class, 'CookieConsent' => BrianFaust\CookieConsent\Facades\CookieConsent::class, diff --git a/config/auth.php b/config/auth.php index 2e91324a8e..afa6cb8bb2 100644 --- a/config/auth.php +++ b/config/auth.php @@ -67,7 +67,7 @@ 'providers' => [ 'users' => [ - 'driver' => 'eloquent', + 'driver' => 'cache-user', 'model' => App\Models\User::class, ], @@ -112,20 +112,20 @@ | */ - 'password_timeout' => 10800, + 'password_timeout' => 10800, - 'TwoStepEnabled' => true, + 'TwoStepEnabled' => true, - 'verificationEmailFrom' => env('MAIL_FROM_ADDRESS', env('MAIL_FROM_NAME')), + 'verificationEmailFrom' => env('MAIL_FROM_ADDRESS', env('MAIL_FROM_NAME')), - 'verificationEmailFromName' => ' 2-Step Verification', + 'verificationEmailFromName' => ' 2-Step Verification', - 'TwoStepExceededCount' => 3, + 'TwoStepExceededCount' => 3, - 'TwoStepExceededCountdownMinutes' => 60 * 24, + 'TwoStepExceededCountdownMinutes' => 60 * 24, - 'TwoStepVerifiedLifetimeMinutes' => 6 * 60, + 'TwoStepVerifiedLifetimeMinutes' => 6 * 60, - 'TwoStepTimeResetBufferSeconds' => 6 * 60, + 'TwoStepTimeResetBufferSeconds' => 6 * 60, ]; From 559c433fb1bf4ee594c433d1816e65284ca7d99e Mon Sep 17 00:00:00 2001 From: HDVinnie Date: Sun, 3 Oct 2021 15:13:36 +0000 Subject: [PATCH 2/3] Apply fixes from StyleCI [ci skip] [skip ci] --- app/Helpers/CacheUser.php | 12 +++++++----- app/Observers/UserObserver.php | 3 ++- app/Providers/AppServiceProvider.php | 4 ++-- app/Providers/AuthServiceProvider.php | 4 ++-- app/Providers/CacheUserProvider.php | 4 ++-- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/app/Helpers/CacheUser.php b/app/Helpers/CacheUser.php index be6008117f..657c9c46aa 100644 --- a/app/Helpers/CacheUser.php +++ b/app/Helpers/CacheUser.php @@ -15,14 +15,16 @@ use App\Models\User; -class CacheUser { - public static function user($id){ - if(! $id || $id<=0 || ! is_numeric($id)) { +class CacheUser +{ + public static function user($id) + { + if (! $id || $id <= 0 || ! is_numeric($id)) { return; } - return \cache()->remember('cachedUser.'.$id, 30, function() use($id) { + return \cache()->remember('cachedUser.'.$id, 30, function () use ($id) { return User::find($id); }); } -} \ No newline at end of file +} diff --git a/app/Observers/UserObserver.php b/app/Observers/UserObserver.php index 3f8b9bcee2..54be4756cf 100644 --- a/app/Observers/UserObserver.php +++ b/app/Observers/UserObserver.php @@ -32,8 +32,9 @@ public function created(User $user) * Handle the User "saved" event. * * - * @return void * @throws \Exception + * + * @return void */ public function saved(User $user) { diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index d6a0430152..8fd5a69378 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -13,13 +13,13 @@ namespace App\Providers; -use App\Models\User; use App\Helpers\ByteUnits; use App\Helpers\HiddenCaptcha; -use App\Observers\UserObserver; use App\Interfaces\ByteUnitsInterface; use App\Interfaces\WishInterface; use App\Models\Page; +use App\Models\User; +use App\Observers\UserObserver; use App\Repositories\WishRepository; use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index fea9ec3f3a..3bd18bada2 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -13,8 +13,8 @@ namespace App\Providers; -use Illuminate\Support\Facades\Auth; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; +use Illuminate\Support\Facades\Auth; class AuthServiceProvider extends ServiceProvider { @@ -35,7 +35,7 @@ class AuthServiceProvider extends ServiceProvider public function boot() { $this->registerPolicies(); - Auth::provider('cache-user', function() { + Auth::provider('cache-user', function () { return resolve(CacheUserProvider::class); }); } diff --git a/app/Providers/CacheUserProvider.php b/app/Providers/CacheUserProvider.php index 02fcd4a0d3..95fe16da25 100644 --- a/app/Providers/CacheUserProvider.php +++ b/app/Providers/CacheUserProvider.php @@ -13,10 +13,10 @@ namespace App\Providers; +use App\Helpers\CacheUser; use App\Models\User; use Illuminate\Auth\EloquentUserProvider; use Illuminate\Contracts\Hashing\Hasher as HasherContract; -use App\Helpers\CacheUser; class CacheUserProvider extends EloquentUserProvider { @@ -42,4 +42,4 @@ public function retrieveByToken($identifier, $token) return $rememberToken && hash_equals($rememberToken, $token) ? $model : null; } -} \ No newline at end of file +} From 20cf51f3a1b7d7b6a5127ecd031c3bc56d7015f0 Mon Sep 17 00:00:00 2001 From: HDVinnie Date: Sun, 3 Oct 2021 12:54:57 -0400 Subject: [PATCH 3/3] update: auth config --- config/auth.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/config/auth.php b/config/auth.php index afa6cb8bb2..d4946f9961 100644 --- a/config/auth.php +++ b/config/auth.php @@ -113,19 +113,12 @@ */ 'password_timeout' => 10800, - 'TwoStepEnabled' => true, - 'verificationEmailFrom' => env('MAIL_FROM_ADDRESS', env('MAIL_FROM_NAME')), - 'verificationEmailFromName' => ' 2-Step Verification', - 'TwoStepExceededCount' => 3, - 'TwoStepExceededCountdownMinutes' => 60 * 24, - 'TwoStepVerifiedLifetimeMinutes' => 6 * 60, - 'TwoStepTimeResetBufferSeconds' => 6 * 60, ];