add user destroyed/disabled/enabled event

This commit is contained in:
xiaomlove
2024-03-16 15:26:12 +08:00
parent 38ff708a26
commit 0c594058e9
9 changed files with 186 additions and 5 deletions

View File

@@ -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,
];
/**

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class UserDestroyed
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public int $id;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(int $id)
{
$this->id = $id;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class UserDisabled
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public int $id;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(int $id)
{
$this->id = $id;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class UserEnabled
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public int $id;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(int $id)
{
$this->id = $id;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}

View File

@@ -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);
}

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Listeners;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\InteractsWithQueue;
use Laravel\Passport\Passport;
class RemoveOauthTokens implements ShouldQueue
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
{
$uid = $event->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));
}
}

View File

@@ -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,
],
];
/**

View File

@@ -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;
}

View File

@@ -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);
}