Skip to content

Commit

Permalink
记录 操作日志
Browse files Browse the repository at this point in the history
  • Loading branch information
liufei-ereach committed Dec 8, 2023
1 parent 258e52e commit 9eba26f
Show file tree
Hide file tree
Showing 15 changed files with 326 additions and 26 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"type": "flarum-extension",
"license": "LPL-1.02",
"require": {
"flarum/core": "^1.2.0"
"flarum/core": "^1.2.0",
"antoinefr/flarum-ext-money": "^1.3"
},
"authors": [
{
Expand Down
49 changes: 46 additions & 3 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,31 @@
*/

use Flarum\Extend;
use Mattoid\MoneyHistory\Middleware\MoneyMiddleware;
use Flarum\Post\Event\Posted;
use Flarum\Post\Event\Restored as PostRestored;
use Flarum\Post\Event\Hidden as PostHidden;
use Flarum\Post\Event\Deleted as PostDeleted;
use Flarum\Discussion\Event\Started;
use Flarum\Discussion\Event\Restored as DiscussionRestored;
use Flarum\Discussion\Event\Hidden as DiscussionHidden;
use Flarum\Discussion\Event\Deleted as DiscussionDeleted;
use Flarum\User\Event\Saving;
use Flarum\Likes\Event\PostWasLiked;
use Flarum\Likes\Event\PostWasUnliked;
use Mattoid\MoneyHistory\Listeners\DiscussionWasDeletedHistory;
use Mattoid\MoneyHistory\Listeners\DiscussionWasHiddenHistory;
use Mattoid\MoneyHistory\Listeners\DiscussionWasRestoredHistory;
use Mattoid\MoneyHistory\Listeners\DiscussionWasStartedHistory;
use Mattoid\MoneyHistory\Listeners\PostWasDeletedHistory;
use Mattoid\MoneyHistory\Listeners\PostWasHiddenHistory;
use Mattoid\MoneyHistory\Listeners\PostWasLikedHistory;
use Mattoid\MoneyHistory\Listeners\PostWasPostedHistory;
use Mattoid\MoneyHistory\Listeners\PostWasRestoredHistory;
use Mattoid\MoneyHistory\Listeners\PostWasUnlikedHistory;
use Mattoid\MoneyHistory\Listeners\UserWillBeSavedHistory;
use Mattoid\MoneyHistory\Middleware\HistoryMiddleware;

return [
$extend = [
(new Extend\Frontend('forum'))
->js(__DIR__.'/js/dist/forum.js')
->css(__DIR__.'/less/forum.less'),
Expand All @@ -21,5 +43,26 @@
->css(__DIR__.'/less/admin.less'),
new Extend\Locales(__DIR__.'/locale'),

(new Extend\Middleware("api"))->add(MoneyMiddleware::class),
(new Extend\Middleware("api"))->add(HistoryMiddleware::class),

(new Extend\Event())
->listen(Posted::class, PostWasPostedHistory::class)
->listen(PostRestored::class, PostWasRestoredHistory::class)
->listen(PostHidden::class, PostWasHiddenHistory::class)
->listen(PostDeleted::class, PostWasDeletedHistory::class)
->listen(Started::class, DiscussionWasStartedHistory::class)
->listen(DiscussionRestored::class, DiscussionWasRestoredHistory::class)
->listen(DiscussionHidden::class, DiscussionWasHiddenHistory::class)
->listen(DiscussionDeleted::class, DiscussionWasDeletedHistory::class)
->listen(Saving::class, UserWillBeSavedHistory::class),
];

if (class_exists('Flarum\Likes\Event\PostWasLiked')) {
$extend[] =
(new Extend\Event())
->listen(PostWasLiked::class, PostWasLikedHistory::class)
->listen(PostWasUnliked::class, PostWasUnlikedHistory::class)
;
}

return $extend;
20 changes: 20 additions & 0 deletions src/Listeners/DiscussionWasDeletedHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Mattoid\MoneyHistory\Listeners;

use Flarum\Notification\NotificationSyncer;
use Flarum\Post\Event\Restored as PostRestored;

class DiscussionWasDeletedHistory
{
private $sourceDesc = "帖子恢复";

public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}

public function handle(PostRestored $event) {

}
}
20 changes: 20 additions & 0 deletions src/Listeners/DiscussionWasHiddenHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Mattoid\MoneyHistory\Listeners;

use Flarum\Notification\NotificationSyncer;
use Flarum\Post\Event\Restored as PostRestored;

class DiscussionWasHiddenHistory
{
private $sourceDesc = "帖子恢复";

public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}

public function handle(PostRestored $event) {

}
}
20 changes: 20 additions & 0 deletions src/Listeners/DiscussionWasRestoredHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Mattoid\MoneyHistory\Listeners;

use Flarum\Notification\NotificationSyncer;
use Flarum\Post\Event\Restored as PostRestored;

class DiscussionWasRestoredHistory
{
private $sourceDesc = "帖子恢复";

public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}

public function handle(PostRestored $event) {

}
}
20 changes: 20 additions & 0 deletions src/Listeners/DiscussionWasStartedHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Mattoid\MoneyHistory\Listeners;

use Flarum\Notification\NotificationSyncer;
use Flarum\Post\Event\Restored as PostRestored;

class DiscussionWasStartedHistory
{
private $sourceDesc = "帖子恢复";

public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}

public function handle(PostRestored $event) {

}
}
20 changes: 20 additions & 0 deletions src/Listeners/PostWasDeletedHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Mattoid\MoneyHistory\Listeners;

use Flarum\Notification\NotificationSyncer;
use Flarum\Post\Event\Restored as PostRestored;

class PostWasDeletedHistory
{
private $sourceDesc = "帖子恢复";

public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}

public function handle(PostRestored $event) {

}
}
19 changes: 19 additions & 0 deletions src/Listeners/PostWasHiddenHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Mattoid\MoneyHistory\Listeners;

use Flarum\Notification\NotificationSyncer;
use Flarum\Post\Event\Hidden as PostHidden;
class PostWasHiddenHistory
{
private $sourceDesc = "隐藏帖子";

public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}

public function handle(PostHidden $event) {

}
}
20 changes: 20 additions & 0 deletions src/Listeners/PostWasLikedHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Mattoid\MoneyHistory\Listeners;

use Flarum\Notification\NotificationSyncer;
use Flarum\Post\Event\Restored as PostRestored;

class PostWasLikedHistory
{
private $sourceDesc = "帖子恢复";

public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}

public function handle(PostRestored $event) {

}
}
20 changes: 20 additions & 0 deletions src/Listeners/PostWasPostedHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Mattoid\MoneyHistory\Listeners;

use Flarum\Notification\NotificationSyncer;
use Flarum\Post\Event\Posted;

class PostWasPostedHistory
{
private $sourceDesc = "发帖奖励";

public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}

public function handle(Posted $event) {

}
}
20 changes: 20 additions & 0 deletions src/Listeners/PostWasRestoredHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Mattoid\MoneyHistory\Listeners;

use Flarum\Notification\NotificationSyncer;
use Flarum\Post\Event\Restored as PostRestored;

class PostWasRestoredHistory
{
private $sourceDesc = "帖子恢复";

public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}

public function handle(PostRestored $event) {

}
}
20 changes: 20 additions & 0 deletions src/Listeners/PostWasUnlikedHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Mattoid\MoneyHistory\Listeners;

use Flarum\Notification\NotificationSyncer;
use Flarum\Post\Event\Restored as PostRestored;

class PostWasUnlikedHistory
{
private $sourceDesc = "帖子恢复";

public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}

public function handle(PostRestored $event) {

}
}
20 changes: 20 additions & 0 deletions src/Listeners/UserWillBeSavedHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Mattoid\MoneyHistory\Listeners;

use Flarum\Notification\NotificationSyncer;
use Flarum\Post\Event\Restored as PostRestored;

class UserWillBeSavedHistory
{
private $sourceDesc = "帖子恢复";

public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}

public function handle(PostRestored $event) {

}
}
57 changes: 57 additions & 0 deletions src/Middleware/HistoryMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Mattoid\MoneyHistory\Middleware;

use Flarum\Http\RequestUtil;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Support\Arr;
use Mattoid\MoneyHistory\model\UserOperateLog;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class HistoryMiddleware implements MiddlewareInterface
{
public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$typesAllowed = [];
$actor = RequestUtil::getActor($request);
$userId = Arr::get($actor, 'id');

if ($this->settings->get('mattoid-money-history.request-type-get')) {
$typesAllowed[] = "GET";
}
if ($this->settings->get('mattoid-money-history.request-type-post')) {
$typesAllowed[] = "POST";
}
if ($this->settings->get('mattoid-money-history.request-type-put')) {
$typesAllowed[] = "PUT";
$typesAllowed[] = "PATCH";
}
if ($this->settings->get('mattoid-money-history.request-type-delete')) {
$typesAllowed[] = "DELETE";
}

$response = $handler->handle($request);

if (!in_array($request->getMethod(), $typesAllowed)) {
return $response;
}

$operateLog = new UserOperateLog();
$operateLog->user_id = $userId;
$operateLog->method = $request->getMethod();
$operateLog->uri = $request->getUri();
$operateLog->request = json_encode($request->getParsedBody());
$operateLog->response = $response->getBody();
$operateLog->save();

return $response;
}
}
Loading

0 comments on commit 9eba26f

Please sign in to comment.