Skip to content

Commit

Permalink
add user destroyed/disabled/enabled event
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomlove committed Mar 16, 2024
1 parent 38ff708 commit 0c59405
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 5 deletions.
8 changes: 7 additions & 1 deletion app/Console/Commands/FireEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
];

/**
Expand Down
38 changes: 38 additions & 0 deletions app/Events/UserDestroyed.php
Original file line number Diff line number Diff line change
@@ -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');
}
}
38 changes: 38 additions & 0 deletions app/Events/UserDisabled.php
Original file line number Diff line number Diff line change
@@ -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');
}
}
38 changes: 38 additions & 0 deletions app/Events/UserEnabled.php
Original file line number Diff line number Diff line change
@@ -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');
}
}
8 changes: 4 additions & 4 deletions app/Http/Middleware/BootNexus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
44 changes: 44 additions & 0 deletions app/Listeners/RemoveOauthTokens.php
Original file line number Diff line number Diff line change
@@ -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));
}
}
9 changes: 9 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
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;
Expand Down Expand Up @@ -33,6 +36,12 @@ class EventServiceProvider extends ServiceProvider
TorrentCreated::class => [
FetchTorrentImdb::class,
],
UserDestroyed::class => [
RemoveOauthTokens::class,
],
UserDisabled::class => [
RemoveOauthTokens::class,
],
];

/**
Expand Down
3 changes: 3 additions & 0 deletions app/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ public function disableUser(User $operator, $uid, $reason = '')
});
do_log("user: $uid, $modCommentText");
$this->clearCache($targetUser);
fire_event("user_disabled", $uid);
return true;
}

Expand All @@ -224,6 +225,7 @@ public function enableUser(User $operator, $uid, $reason = '')
$targetUser->updateWithModComment($update, $modCommentText);
do_log("user: $uid, $modCommentText, update: " . nexus_json_encode($update));
$this->clearCache($targetUser);
fire_event("user_enabled", $uid);
return true;
}

Expand Down Expand Up @@ -654,6 +656,7 @@ public function destroy($id, $reasonKey = 'user.destroy_by_admin')
}
UserBanLog::query()->insert($userBanLogs);
do_action("user_delete", $id);
fire_event("user_destroyed", $id);
return true;
}

Expand Down
5 changes: 5 additions & 0 deletions include/globalfunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 0c59405

Please sign in to comment.