From 0c594058e9b65769456a19c3a74a383b350a82cb Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Sat, 16 Mar 2024 15:26:12 +0800 Subject: [PATCH] add user destroyed/disabled/enabled event --- app/Console/Commands/FireEvent.php | 8 ++++- app/Events/UserDestroyed.php | 38 ++++++++++++++++++++++ app/Events/UserDisabled.php | 38 ++++++++++++++++++++++ app/Events/UserEnabled.php | 38 ++++++++++++++++++++++ app/Http/Middleware/BootNexus.php | 8 ++--- app/Listeners/RemoveOauthTokens.php | 44 ++++++++++++++++++++++++++ app/Providers/EventServiceProvider.php | 9 ++++++ app/Repositories/UserRepository.php | 3 ++ include/globalfunctions.php | 5 +++ 9 files changed, 186 insertions(+), 5 deletions(-) create mode 100644 app/Events/UserDestroyed.php create mode 100644 app/Events/UserDisabled.php create mode 100644 app/Events/UserEnabled.php create mode 100644 app/Listeners/RemoveOauthTokens.php diff --git a/app/Console/Commands/FireEvent.php b/app/Console/Commands/FireEvent.php index 96c1c864..4d58284a 100644 --- a/app/Console/Commands/FireEvent.php +++ b/app/Console/Commands/FireEvent.php @@ -3,6 +3,9 @@ namespace App\Console\Commands; use App\Events\TorrentCreated; +use App\Events\UserDestroyed; +use App\Events\UserDisabled; +use App\Events\UserEnabled; use Illuminate\Console\Command; class FireEvent extends Command @@ -22,7 +25,10 @@ class FireEvent extends Command protected $description = 'Fire a event, options: --name, --id'; protected array $eventMaps = [ - "torrent_created" => TorrentCreated::class + "torrent_created" => TorrentCreated::class, + "user_destroyed" => UserDestroyed::class, + "user_disabled" => UserDisabled::class, + "user_enabled" => UserEnabled::class, ]; /** diff --git a/app/Events/UserDestroyed.php b/app/Events/UserDestroyed.php new file mode 100644 index 00000000..84756866 --- /dev/null +++ b/app/Events/UserDestroyed.php @@ -0,0 +1,38 @@ +id = $id; + } + + /** + * Get the channels the event should broadcast on. + * + * @return \Illuminate\Broadcasting\Channel|array + */ + public function broadcastOn() + { + return new PrivateChannel('channel-name'); + } +} diff --git a/app/Events/UserDisabled.php b/app/Events/UserDisabled.php new file mode 100644 index 00000000..6d388141 --- /dev/null +++ b/app/Events/UserDisabled.php @@ -0,0 +1,38 @@ +id = $id; + } + + /** + * Get the channels the event should broadcast on. + * + * @return \Illuminate\Broadcasting\Channel|array + */ + public function broadcastOn() + { + return new PrivateChannel('channel-name'); + } +} diff --git a/app/Events/UserEnabled.php b/app/Events/UserEnabled.php new file mode 100644 index 00000000..31de6df6 --- /dev/null +++ b/app/Events/UserEnabled.php @@ -0,0 +1,38 @@ +id = $id; + } + + /** + * Get the channels the event should broadcast on. + * + * @return \Illuminate\Broadcasting\Channel|array + */ + public function broadcastOn() + { + return new PrivateChannel('channel-name'); + } +} diff --git a/app/Http/Middleware/BootNexus.php b/app/Http/Middleware/BootNexus.php index 80e79fa2..42710b07 100644 --- a/app/Http/Middleware/BootNexus.php +++ b/app/Http/Middleware/BootNexus.php @@ -18,10 +18,10 @@ class BootNexus public function handle(Request $request, Closure $next) { Nexus::boot(); - do_log(sprintf( - "Nexus booted. request.server: %s, request.header: %s, request.query: %s, request.input: %s", - nexus_json_encode($request->server()), nexus_json_encode($request->header()), nexus_json_encode($request->query()), nexus_json_encode($request->input()) - )); +// do_log(sprintf( +// "Nexus booted. request.server: %s, request.header: %s, request.query: %s, request.input: %s", +// nexus_json_encode($request->server()), nexus_json_encode($request->header()), nexus_json_encode($request->query()), nexus_json_encode($request->input()) +// )); return $next($request); } diff --git a/app/Listeners/RemoveOauthTokens.php b/app/Listeners/RemoveOauthTokens.php new file mode 100644 index 00000000..d1d82de9 --- /dev/null +++ b/app/Listeners/RemoveOauthTokens.php @@ -0,0 +1,44 @@ +id; + $modelNames = [ + Passport::$authCodeModel, + Passport::$tokenModel, + ]; + foreach ($modelNames as $name) { + /** + * @var $model Model + */ + $model = new $name(); + $model::query()->where("user_id", $uid)->forceDelete(); + } + do_log(sprintf("success remove user: %d oauth tokens related.", $uid)); + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 996ba5a9..c8df45a3 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -5,7 +5,10 @@ namespace App\Providers; use App\Events\SeedBoxRecordUpdated; use App\Events\TorrentCreated; use App\Events\TorrentUpdated; +use App\Events\UserDestroyed; +use App\Events\UserDisabled; use App\Listeners\FetchTorrentImdb; +use App\Listeners\RemoveOauthTokens; use App\Listeners\RemoveSeedBoxRecordCache; use App\Listeners\SyncTorrentToEs; use Illuminate\Auth\Events\Registered; @@ -33,6 +36,12 @@ class EventServiceProvider extends ServiceProvider TorrentCreated::class => [ FetchTorrentImdb::class, ], + UserDestroyed::class => [ + RemoveOauthTokens::class, + ], + UserDisabled::class => [ + RemoveOauthTokens::class, + ], ]; /** diff --git a/app/Repositories/UserRepository.php b/app/Repositories/UserRepository.php index 791a4d17..7071ebbf 100644 --- a/app/Repositories/UserRepository.php +++ b/app/Repositories/UserRepository.php @@ -198,6 +198,7 @@ class UserRepository extends BaseRepository }); do_log("user: $uid, $modCommentText"); $this->clearCache($targetUser); + fire_event("user_disabled", $uid); return true; } @@ -224,6 +225,7 @@ class UserRepository extends BaseRepository $targetUser->updateWithModComment($update, $modCommentText); do_log("user: $uid, $modCommentText, update: " . nexus_json_encode($update)); $this->clearCache($targetUser); + fire_event("user_enabled", $uid); return true; } @@ -654,6 +656,7 @@ class UserRepository extends BaseRepository } UserBanLog::query()->insert($userBanLogs); do_action("user_delete", $id); + fire_event("user_destroyed", $id); return true; } diff --git a/include/globalfunctions.php b/include/globalfunctions.php index cf994879..6f8b8b07 100644 --- a/include/globalfunctions.php +++ b/include/globalfunctions.php @@ -1226,3 +1226,8 @@ function get_snatch_info($torrentId, $userId) { return mysql_fetch_assoc(sql_query(sprintf('select * from snatched where torrentid = %s and userid = %s order by id desc limit 1', $torrentId, $userId))); } + +function fire_event(string $name, int $id): void +{ + executeCommand("event:fire --name=$name --id=$id", "string", true, false); +}