From 8d34bc1acd4bb6f91f614cd4290a6fd0d3a68438 Mon Sep 17 00:00:00 2001 From: gyorudayo Date: Fri, 20 Jan 2023 11:54:07 +0900 Subject: [PATCH 1/6] =?UTF-8?q?-=20=EC=82=AC=EC=9A=A9=ED=95=98=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EB=8A=94=20=EB=82=B4=EC=9A=A9=20=EC=82=AD=EC=A0=9C=20?= =?UTF-8?q?=EB=B0=8F=20=EC=A0=84=EC=B2=B4=EC=A0=81=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/menu.js | 4 +- plugin.php | 45 ++++++++----------- src/ClaimException.php | 4 +- src/Controllers/UserController.php | 6 +-- src/Exceptions/AlreadyClaimedException.php | 3 ++ .../AlreadyClaimedHttpException.php | 37 --------------- src/Models/ClaimLog.php | 20 +++------ src/ToggleMenus/AbstractClaimItem.php | 4 +- 8 files changed, 38 insertions(+), 85 deletions(-) delete mode 100644 src/Exceptions/AlreadyClaimedHttpException.php diff --git a/assets/menu.js b/assets/menu.js index 1407809..5c923ab 100644 --- a/assets/menu.js +++ b/assets/menu.js @@ -1,5 +1,5 @@ var ClaimToggleMenu = { - storeBoard: function(e, url, from, targetId, shortCut) { + storeClaim: function(e, url, from, targetId, shortCut) { e.preventDefault(); var _o = $(e.target)[0]; @@ -14,7 +14,7 @@ var ClaimToggleMenu = { } }); }, - destroyBoard: function(e, url, from, targetId) { + destroyClaim: function(e, url, from, targetId) { e.preventDefault(); var _o = $(e.target)[0]; diff --git a/plugin.php b/plugin.php index ca4521a..43885c0 100644 --- a/plugin.php +++ b/plugin.php @@ -19,9 +19,10 @@ use Schema; use XeToggleMenu; use Xpressengine\Plugin\AbstractPlugin; +use Xpressengine\Plugin\PluginRegister; /** - * Plugin + * Claim Plugin * * @category Claim * @package Xpressengine\Plugins\Claim @@ -34,7 +35,6 @@ class Plugin extends AbstractPlugin { /** * boot - * * @return void */ public function boot() @@ -53,7 +53,6 @@ public function boot() /** * activate - * * @param null $installedVersion installed version * @return void */ @@ -62,6 +61,7 @@ public function activate($installedVersion = null) } /** + * check for updates * @return boolean */ public function checkUpdated($installedVersion = NULL) @@ -88,35 +88,29 @@ public function checkUpdated($installedVersion = NULL) public function update($installedVersion = null) { $this->putLang(); - $this->setToggleMenuConfig(); + $this->setToggleMenuConfigs(); } public function install() { $this->createClaimLogTable(); $this->putLang(); - $this->setToggleMenuConfig(); + $this->setToggleMenuConfigs(); } /** - * set toggle menu item to board, comment - * + * set toggle menu item to board, comment, user * @return void */ - protected function setToggleMenuConfig() + protected function setToggleMenuConfigs() { - $toggleMenuId = 'module/board@board'; - $activated = XeToggleMenu::getActivated($toggleMenuId); - $itemId = 'module/board@board/toggleMenu/claim@boardClaimItem'; - if (isset($activated[$itemId]) === false) { - $setActivate = array_keys($activated); - $setActivate[] = $itemId; - XeToggleMenu::setActivates($toggleMenuId, null, $setActivate); - } + $this->setToggleMenuConfig('module/board@board', 'module/board@board/toggleMenu/claim@boardClaimItem'); + $this->setToggleMenuConfig('comment', 'comment/toggleMenu/claim@commentClaimItem'); + } - $toggleMenuId = 'comment'; + protected function setToggleMenuConfig(string $toggleMenuId, string $itemId) + { $activated = XeToggleMenu::getActivated($toggleMenuId); - $itemId = 'comment/toggleMenu/claim@commentClaimItem'; if (isset($activated[$itemId]) === false) { $setActivate = array_keys($activated); $setActivate[] = $itemId; @@ -151,18 +145,19 @@ protected function putLang() /** * register toggle menu - * * @return void */ protected function registerToggleMenu() { - app('xe.pluginRegister')->add(ToggleMenus\BoardClaimItem::class); - app('xe.pluginRegister')->add(ToggleMenus\CommentClaimItem::class); + /** @var PluginRegister $pluginRegister */ + $pluginRegister = app('xe.pluginRegister'); + $pluginRegister->add(ToggleMenus\BoardClaimItem::class); + $pluginRegister->add(ToggleMenus\CommentClaimItem::class); + $pluginRegister->add(ToggleMenus\UserClaimItem::class); } /** - * Register Plugin Settings Route - * + * register plugin settings routes * @return void */ protected function registerManageRoute() @@ -191,7 +186,6 @@ protected function registerManageRoute() /** * register fixed route - * * @return void */ protected function registerFixedRoute() @@ -205,7 +199,6 @@ protected function registerFixedRoute() /** * register interception - * * @return void */ public static function registerSettingsMenu() @@ -215,7 +208,7 @@ public static function registerSettingsMenu() 'contents.claim' => [ 'title' => 'xe::claim', 'display' => true, - 'description' => 'blur blur~', + 'description' => 'xe::claim', 'ordering' => 5000 ], ]; diff --git a/src/ClaimException.php b/src/ClaimException.php index 307dec9..56d17dc 100644 --- a/src/ClaimException.php +++ b/src/ClaimException.php @@ -16,7 +16,7 @@ namespace Xpressengine\Plugins\Claim; -use Xpressengine\Support\Exceptions\XpressengineException; +use Xpressengine\Support\Exceptions\HttpXpressengineException; /** * ClaimException @@ -28,7 +28,7 @@ * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL * @link https://xpressengine.io */ -class ClaimException extends XpressengineException +class ClaimException extends HttpXpressengineException { } diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index f6e3890..b4a1bf0 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -20,9 +20,9 @@ use XePresenter; use Auth; use App\Http\Controllers\Controller; -use Xpressengine\Plugins\Claim\Exceptions\AlreadyClaimedHttpException; -use Xpressengine\Plugins\Claim\Handler; use Xpressengine\Support\Exceptions\LoginRequiredHttpException; +use Xpressengine\Plugins\Claim\Handler; +use Xpressengine\Plugins\Claim\Exceptions\AlreadyClaimedException; /** * UserController @@ -90,7 +90,7 @@ public function store() try { $this->handler->add($targetId, Auth::user(), $shortCut); } catch (\Exception $e) { - throw new AlreadyClaimedHttpException; + throw new AlreadyClaimedException; } return $this->index(); } diff --git a/src/Exceptions/AlreadyClaimedException.php b/src/Exceptions/AlreadyClaimedException.php index cd1d14f..3197248 100644 --- a/src/Exceptions/AlreadyClaimedException.php +++ b/src/Exceptions/AlreadyClaimedException.php @@ -16,6 +16,7 @@ namespace Xpressengine\Plugins\Claim\Exceptions; +use Symfony\Component\HttpFoundation\Response; use Xpressengine\Plugins\Claim\ClaimException; /** @@ -30,5 +31,7 @@ */ class AlreadyClaimedException extends ClaimException { + protected $message = 'claim::AlreadyClaimed'; + protected $statusCode = Response::HTTP_INTERNAL_SERVER_ERROR; } diff --git a/src/Exceptions/AlreadyClaimedHttpException.php b/src/Exceptions/AlreadyClaimedHttpException.php deleted file mode 100644 index 80daaa0..0000000 --- a/src/Exceptions/AlreadyClaimedHttpException.php +++ /dev/null @@ -1,37 +0,0 @@ - - * @copyright 2019 Copyright XEHub Corp. - * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL - * @link https://xpressengine.io - */ - -namespace Xpressengine\Plugins\Claim\Exceptions; - -use Xpressengine\Plugins\Board\HttpBoardException; -use Symfony\Component\HttpFoundation\Response; - -/** - * AlreadyClaimedHttpException - * - * @category Claim - * @package Xpressengine\Plugins\Claim - * @author XE Developers - * @copyright 2019 Copyright XEHub Corp. - * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL - * @link https://xpressengine.io - */ -class AlreadyClaimedHttpException extends HttpBoardException -{ - protected $message = 'claim::AlreadyClaimed'; - - protected $statusCode = Response::HTTP_INTERNAL_SERVER_ERROR; -} diff --git a/src/Models/ClaimLog.php b/src/Models/ClaimLog.php index d5948e4..bb5b4b5 100644 --- a/src/Models/ClaimLog.php +++ b/src/Models/ClaimLog.php @@ -16,27 +16,21 @@ namespace Xpressengine\Plugins\Claim\Models; -use Xpressengine\Config\ConfigEntity; -use Xpressengine\Database\Eloquent\DynamicModel; -use Xpressengine\Document\Exceptions\NotAllowedTypeException; -use Xpressengine\Document\Exceptions\DocumentNotFoundException; -use Xpressengine\Document\Exceptions\ReplyLimitationException; -use Xpressengine\Document\Exceptions\ValueRequiredException; -use Illuminate\Database\Eloquent\Builder as OriginBuilder; use Xpressengine\User\Models\User; +use Xpressengine\Database\Eloquent\DynamicModel; /** * ClaimLog * * @property int id - * @property string claimType - * @property string shortCut - * @property string targetId - * @property string uiserId + * @property string claim_type + * @property string short_cut + * @property string target_id + * @property string user_id * @property string ipaddress * @property string message - * @property string createdAt - * @property string updatedAt + * @property string created_at + * @property string updated_at * * @category Claim * @package Xpressengine\Plugins\Claim diff --git a/src/ToggleMenus/AbstractClaimItem.php b/src/ToggleMenus/AbstractClaimItem.php index c59d184..5381e4d 100644 --- a/src/ToggleMenus/AbstractClaimItem.php +++ b/src/ToggleMenus/AbstractClaimItem.php @@ -65,7 +65,7 @@ public function getAction() if ($handler->has($this->identifier, Auth::user()) === true) { return sprintf( - 'ClaimToggleMenu.destroyBoard(event, "%s", "%s", "%s")', + 'ClaimToggleMenu.destroyClaim(event, "%s", "%s", "%s")', route('fixed.claim.destroy'), $this->componentType, $this->identifier @@ -73,7 +73,7 @@ public function getAction() } return sprintf( - 'ClaimToggleMenu.storeBoard(event, "%s", "%s", "%s", "%s")', + 'ClaimToggleMenu.storeClaim(event, "%s", "%s", "%s", "%s")', route('fixed.claim.store'), $this->componentType, $this->identifier, From 90357780507cbe5b9c6829e56774e63d204b8563 Mon Sep 17 00:00:00 2001 From: gyorudayo Date: Thu, 26 Jan 2023 00:00:51 +0900 Subject: [PATCH 2/6] =?UTF-8?q?-=20#16=20#17=20=EC=9C=A0=EC=A0=80=20?= =?UTF-8?q?=EC=8B=A0=EA=B3=A0=20=EA=B8=B0=EB=8A=A5=20=EB=B0=8F=20=EC=8B=A0?= =?UTF-8?q?=EA=B3=A0=20=EC=82=AC=EC=9C=A0=20=EC=B6=94=EA=B0=80,=20?= =?UTF-8?q?=EC=A0=84=EC=B2=B4=EC=A0=81=20=EC=BD=94=EB=93=9C=20=EC=A0=95?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/menu.js | 24 ++-- composer.json | 5 + langs/lang.php | 40 +++++- plugin.php | 37 ++++- src/Controllers/ClaimController.php | 135 ++++++++++++++++++ src/Controllers/UserController.php | 114 --------------- src/Exceptions/AlreadyClaimedException.php | 1 - src/Exceptions/CantReportAdminException.php | 7 + src/Exceptions/CantReportMyselfException.php | 7 + .../CantReportWithdrawnUserException.php | 7 + src/{ => Exceptions}/ClaimException.php | 4 +- src/Exceptions/GuestCannotReportException.php | 7 + .../NotFoundClaimTargetException.php | 7 + .../NotSupportClaimTypeException.php | 7 + src/Handler.php | 95 ++++++------ src/ToggleMenus/AbstractClaimItem.php | 2 +- src/ToggleMenus/UserClaimItem.php | 6 + src/Types/AbstractClaimType.php | 100 +++++++++++++ src/Types/ClaimTypeBoard.php | 75 ++++++++++ src/Types/ClaimTypeComment.php | 75 ++++++++++ src/Types/ClaimTypeUser.php | 90 ++++++++++++ 21 files changed, 668 insertions(+), 177 deletions(-) create mode 100644 src/Controllers/ClaimController.php delete mode 100644 src/Controllers/UserController.php create mode 100644 src/Exceptions/CantReportAdminException.php create mode 100644 src/Exceptions/CantReportMyselfException.php create mode 100644 src/Exceptions/CantReportWithdrawnUserException.php rename src/{ => Exceptions}/ClaimException.php (89%) create mode 100644 src/Exceptions/GuestCannotReportException.php create mode 100644 src/Exceptions/NotFoundClaimTargetException.php create mode 100644 src/Exceptions/NotSupportClaimTypeException.php create mode 100644 src/ToggleMenus/UserClaimItem.php create mode 100644 src/Types/AbstractClaimType.php create mode 100644 src/Types/ClaimTypeBoard.php create mode 100644 src/Types/ClaimTypeComment.php create mode 100644 src/Types/ClaimTypeUser.php diff --git a/assets/menu.js b/assets/menu.js index 5c923ab..18d03a6 100644 --- a/assets/menu.js +++ b/assets/menu.js @@ -2,17 +2,19 @@ var ClaimToggleMenu = { storeClaim: function(e, url, from, targetId, shortCut) { e.preventDefault(); var _o = $(e.target)[0]; - - XE.ajax({ - url: url, - type: 'post', - dataType: 'json', - data: {from: from, targetId:targetId, shortCut:shortCut}, - success: function (json) { - $('body').trigger('click'); - XE.toast('success', XE.Lang.trans('claim::msgClaimReceived')); - } - }); + let reason = prompt(XE.Lang.trans('claim::enterClaimReason'), ''); + if (reason != null) { + XE.ajax({ + url: url, + type: 'post', + dataType: 'json', + data: {from: from, targetId: targetId, shortCut: shortCut, message: reason}, + success: function (json) { + $('body').trigger('click'); + XE.toast('success', XE.Lang.trans('claim::msgClaimReceived')); + } + }); + } }, destroyClaim: function(e, url, from, targetId) { e.preventDefault(); diff --git a/composer.json b/composer.json index 909c160..5d12dc5 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,11 @@ "class": "Xpressengine\\Plugins\\Claim\\ToggleMenus\\CommentClaimItem", "name": "댓글 신고 토글 메뉴", "description": "" + }, + "user/toggleMenu/claim@userClaimItem": { + "class": "Xpressengine\\Plugins\\Claim\\ToggleMenus\\UserClaimItem", + "name": "유저 신고 토글 메뉴", + "description": "" } } } diff --git a/langs/lang.php b/langs/lang.php index 8b0519c..acb3b98 100644 --- a/langs/lang.php +++ b/langs/lang.php @@ -7,18 +7,50 @@ ], 'AlreadyClaimed' => [ 'ko' => '이미 신고되었습니다.', - 'en' => 'Already Claimed.', + 'en' => "It's already been reported", ], 'msgClaimReceived' => [ 'ko' => '신고가 접수 되었습니다.', - 'en' => 'The report has been received.', + 'en' => 'The report has been received', ], 'msgClaimCanceled' => [ 'ko' => '신고가 취소 되었습니다.', - 'en' => 'The report has been canceled.', + 'en' => 'The report has been canceled', ], 'unknownUser' => [ 'ko' => '알 수 없는 회원', 'en' => 'Unknown user' - ] + ], + 'enterClaimReason' => [ + 'ko' => '신고 사유를 입력해주세요.', + 'en' => 'Please enter reason for report' + ], + 'claimException' => [ + 'ko' => '신고에 실패하였습니다.
계속 실패하는 경우 관리자에게 문의해주세요.', + 'en' => 'Failed to report.
If you continue to receive this error please contact Administrator.' + ], + 'cantReportAdminException' => [ + 'ko' => '관리자는 신고할 수 없습니다.', + 'en' => "You can't report admin" + ], + 'cantReportMyselfException' => [ + 'ko' => '자기 자신을 신고할 수 없습니다', + 'en' => "You can't report yourself" + ], + 'cantReportWithdrawnUserException' => [ + 'ko' => '탈퇴한 사용자입니다.', + 'en' => "You can't report withdrawn user" + ], + 'guestCannotReportException' => [ + 'ko' => '비회원은 신고할 수 없습니다.', + 'en' => 'Guest cannot report' + ], + 'notSupportClaimTypeException' => [ + 'ko' => '지원하지 않는 신고 유형 입니다.', + 'en' => 'Not supported report type' + ], + 'notFoundTargetException' => [ + 'ko' => '잘못된 신고 대상 입니다.', + 'en' => 'Invalid report target' + ], ]; diff --git a/plugin.php b/plugin.php index 43885c0..2839023 100644 --- a/plugin.php +++ b/plugin.php @@ -20,6 +20,7 @@ use XeToggleMenu; use Xpressengine\Plugin\AbstractPlugin; use Xpressengine\Plugin\PluginRegister; +use Xpressengine\Plugins\Claim\Types; /** * Claim Plugin @@ -33,6 +34,16 @@ */ class Plugin extends AbstractPlugin { + /** + * claim types + * @var string[] + */ + protected $claimTypes = [ + Types\ClaimTypeUser::class, + Types\ClaimTypeBoard::class, + Types\ClaimTypeComment::class, + ]; + /** * boot * @return void @@ -46,7 +57,7 @@ public function boot() app()->singleton(Handler::class, function () { $proxyClass = app('xe.interception')->proxy(Handler::class); - return new $proxyClass(app('xe.config')); + return new $proxyClass(app('xe.config'), $this->getActivateClaimTypes()); }); app()->alias(Handler::class, 'xe.claim.handler'); } @@ -106,6 +117,7 @@ protected function setToggleMenuConfigs() { $this->setToggleMenuConfig('module/board@board', 'module/board@board/toggleMenu/claim@boardClaimItem'); $this->setToggleMenuConfig('comment', 'comment/toggleMenu/claim@commentClaimItem'); + $this->setToggleMenuConfig('user', 'user/toggleMenu/claim@userClaimItem'); } protected function setToggleMenuConfig(string $toggleMenuId, string $itemId) @@ -191,9 +203,9 @@ protected function registerManageRoute() protected function registerFixedRoute() { Route::fixed('claim', function () { - Route::get('', ['as' => 'fixed.claim.index', 'uses' => 'UserController@index']); - Route::post('store', ['as' => 'fixed.claim.store', 'uses' => 'UserController@store']); - Route::post('destroy', ['as' => 'fixed.claim.destroy', 'uses' => 'UserController@destroy']); + Route::get('', ['as' => 'fixed.claim.index', 'uses' => 'ClaimController@index']); + Route::post('store', ['as' => 'fixed.claim.store', 'uses' => 'ClaimController@store']); + Route::post('destroy', ['as' => 'fixed.claim.destroy', 'uses' => 'ClaimController@destroy']); }, ['namespace' => 'Xpressengine\Plugins\Claim\Controllers']); } @@ -216,4 +228,21 @@ public static function registerSettingsMenu() app('xe.register')->push('settings/menu', $id, $menu); } } + + /** + * get activate claim types + * @return array + */ + public function getActivateClaimTypes() + { + $types = []; + foreach ($this->claimTypes as $claimType) { + $type = app($claimType); + if ($type instanceof Types\AbstractClaimType && class_exists($type->getClass())) { + $types[$type->getName()] = $type; + } + } + + return $types; + } } diff --git a/src/Controllers/ClaimController.php b/src/Controllers/ClaimController.php new file mode 100644 index 0000000..b68b93b --- /dev/null +++ b/src/Controllers/ClaimController.php @@ -0,0 +1,135 @@ + + * @copyright 2019 Copyright XEHub Corp. + * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL + * @link https://xpressengine.io + */ + +namespace Xpressengine\Plugins\Claim\Controllers; + +use Auth; +use XePresenter; +use Illuminate\Http\Request; +use Illuminate\Database\Eloquent\ModelNotFoundException; +use App\Http\Controllers\Controller; +use Xpressengine\Support\Exceptions\LoginRequiredHttpException; +use Xpressengine\Plugins\Claim\Handler; +use Xpressengine\Plugins\Claim\Exceptions\ClaimException; +use Xpressengine\Plugins\Claim\Exceptions\NotSupportClaimTypeException; + +/** + * ClaimController + * + * @category Claim + * @package Xpressengine\Plugins\Claim + * @author XE Developers + * @copyright 2019 Copyright XEHub Corp. + * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL + * @link https://xpressengine.io + */ +class ClaimController extends Controller +{ + /** + * @var Handler + */ + protected $handler; + + /** + * create instance + */ + public function __construct() + { + $this->handler = app('xe.claim.handler'); + } + + /** + * index + * + * @return \Xpressengine\Presenter\Presentable + */ + public function index(Request $request) + { + $targetId = $request->get('targetId'); + $from = $request->get('from'); + + try { + $this->handler->set($from); + $invoked = $this->handler->has($targetId, Auth::user()); + $count = $this->handler->count($targetId); + } catch (ClaimException $e) { + throw $e; + } catch (ModelNotFoundException $e) { + throw new NotSupportClaimTypeException(); + } catch (\Exception $e) { + throw new ClaimException(); + } + + return XePresenter::makeApi([ + 'invoked' => $invoked, + 'count' => $count, + ]); + } + + /** + * store + * + * @return \Xpressengine\Presenter\Presentable + */ + public function store(Request $request) + { + if (Auth::check() === false) { + throw new LoginRequiredHttpException; + } + + $targetId = $request->get('targetId'); + $shortCut = $request->get('shortCut'); + $from = $request->get('from'); + $message = $request->get('message') ?: ''; + + try { + $this->handler->set($from); + $this->handler->report($targetId, Auth::user(), $shortCut, $message); + } catch (ClaimException $e) { + throw $e; + } catch (ModelNotFoundException $e) { + throw new NotSupportClaimTypeException(); + } catch (\Exception $e) { + throw new ClaimException(); + } + + return $this->index($request); + } + + /** + * destroy + * + * @return \Xpressengine\Presenter\Presentable + */ + public function destroy(Request $request) + { + $targetId = $request->get('targetId'); + $from = $request->get('from'); + + try { + $this->handler->set($from); + $this->handler->removeByTargetId($targetId, Auth::user()); + } catch (ClaimException $e) { + throw $e; + } catch (ModelNotFoundException $e) { + throw new NotSupportClaimTypeException(); + } catch (\Exception $e) { + throw new ClaimException(); + } + + return $this->index($request); + } +} diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php deleted file mode 100644 index b4a1bf0..0000000 --- a/src/Controllers/UserController.php +++ /dev/null @@ -1,114 +0,0 @@ - - * @copyright 2019 Copyright XEHub Corp. - * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL - * @link https://xpressengine.io - */ - -namespace Xpressengine\Plugins\Claim\Controllers; - -use Request; -use XePresenter; -use Auth; -use App\Http\Controllers\Controller; -use Xpressengine\Support\Exceptions\LoginRequiredHttpException; -use Xpressengine\Plugins\Claim\Handler; -use Xpressengine\Plugins\Claim\Exceptions\AlreadyClaimedException; - -/** - * UserController - * - * @category Claim - * @package Xpressengine\Plugins\Claim - * @author XE Developers - * @copyright 2019 Copyright XEHub Corp. - * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL - * @link https://xpressengine.io - */ -class UserController extends Controller -{ - /** - * @var Handler - */ - protected $handler; - - /** - * create instance - */ - public function __construct() - { - $this->handler = app('xe.claim.handler'); - } - - /** - * index - * - * @return \Xpressengine\Presenter\Presentable - */ - public function index() - { - $targetId = Request::get('targetId'); - $from = Request::get('from'); - - $this->handler->set($from); - - $invoked = $this->handler->has($targetId, Auth::user()); - $count = $this->handler->count($targetId); - - return XePresenter::makeApi([ - 'invoked' => $invoked, - 'count' => $count, - ]); - } - - /** - * store - * - * @return \Xpressengine\Presenter\Presentable - */ - public function store() - { - if (Auth::check() === false) { - throw new LoginRequiredHttpException; - } - - $targetId = Request::get('targetId'); - $shortCut = Request::get('shortCut'); - $from = Request::get('from'); - - $this->handler->set($from); - - try { - $this->handler->add($targetId, Auth::user(), $shortCut); - } catch (\Exception $e) { - throw new AlreadyClaimedException; - } - return $this->index(); - } - - /** - * destroy - * - * @return \Xpressengine\Presenter\Presentable - */ - public function destroy() - { - $targetId = Request::get('targetId'); - $from = Request::get('from'); - - $this->handler->set($from); - - $this->handler->removeByTargetId($targetId, Auth::user()); - - return $this->index(); - } -} diff --git a/src/Exceptions/AlreadyClaimedException.php b/src/Exceptions/AlreadyClaimedException.php index 3197248..3085385 100644 --- a/src/Exceptions/AlreadyClaimedException.php +++ b/src/Exceptions/AlreadyClaimedException.php @@ -17,7 +17,6 @@ namespace Xpressengine\Plugins\Claim\Exceptions; use Symfony\Component\HttpFoundation\Response; -use Xpressengine\Plugins\Claim\ClaimException; /** * AlreadyClaimedException diff --git a/src/Exceptions/CantReportAdminException.php b/src/Exceptions/CantReportAdminException.php new file mode 100644 index 0000000..2031443 --- /dev/null +++ b/src/Exceptions/CantReportAdminException.php @@ -0,0 +1,7 @@ + + */ + protected $activateClaimTypes = []; + /** * @var string */ @@ -50,100 +55,110 @@ class Handler const CONFIG_NAME = 'Claim'; /** - * create instance - * - * @param ConfigManager $configManager config manager + * @param ConfigManager $configManager + * @param array $activateClaimTypes */ - public function __construct(ConfigManager $configManager) + public function __construct(ConfigManager $configManager, array $activateClaimTypes) { $this->configManager = $configManager; + $this->activateClaimTypes = $activateClaimTypes; } /** * claim 에서 사용 할 type name 설정 - * - * @param string $claimType claim type - * + * @param string $claimType * @return void */ - public function set($claimType) + public function set(string $claimType) { + if (!array_key_exists($claimType, $this->activateClaimTypes)) { + throw new NotSupportClaimTypeException(); + } + $this->claimType = $claimType; } /** * 신고 수 - * - * @param string $targetId targetId - * + * @param string $targetId * @return int */ - public function count($targetId) + public function count(string $targetId) { return ClaimLog::where('target_id', $targetId)->where('claim_type', $this->claimType)->count(); } /** - * 신고 삭제 - * + * 신고 로그 삭제 * @param int $id id - * * @return void */ - public function remove($id) + public function remove(int $id) { ClaimLog::find($id)->delete(); } /** - * 신고 삭제 - * - * @param string $targetId targetId - * @param UserInterface $author user instance - * + * 신고 로그 삭제 + * @param string $targetId + * @param UserInterface $author * @return void */ - public function removeByTargetId($targetId, UserInterface $author) + public function removeByTargetId(string $targetId, UserInterface $author) { ClaimLog::where('user_id', $author->getId())->where('target_id', $targetId) ->where('claim_type', $this->claimType)->delete(); } /** - * 신고 추가 - * - * @param string $targetId targetId - * @param UserInterface $author user instance - * @param string $shortCut 바로가기 - * + * 대상을 신고 + * @param string $targetId + * @param UserInterface $author + * @param string $shortCut + * @param string $message + * @return void + */ + public function report(string $targetId, UserInterface $author, string $shortCut, string $message = '') + { + if (!array_key_exists($this->claimType, $this->activateClaimTypes)) { + throw new NotSupportClaimTypeException(); + } + + $claimType = $this->activateClaimTypes[$this->claimType]; + $claimType->report($this, $author, $targetId, $shortCut, $message); + } + + /** + * 신고 로그 등록 + * @param string $targetId + * @param UserInterface $author + * @param string $shortCut + * @param string $message * @return ClaimLog */ - public function add($targetId, UserInterface $author, $shortCut) + public function add(string $targetId, UserInterface $author, string $shortCut, string $message = '') { if ($this->has($targetId, $author) === true) { throw new Exceptions\AlreadyClaimedException; } - $item = ClaimLog::create([ + return ClaimLog::create([ 'claim_type' => $this->claimType, 'short_cut' => $shortCut, 'target_id' => $targetId, 'user_id' => $author->getId(), + 'message' => $message, 'ipaddress' => $_SERVER['REMOTE_ADDR'], ]); - - return $item; } /** * 신고 여부 - * - * @param string $targetId targetId - * @param UserInterface $author user instance - * + * @param string $targetId + * @param UserInterface $author * @return bool */ - public function has($targetId, UserInterface $author) + public function has(string $targetId, UserInterface $author) { return ClaimLog::where('user_id', $author->getId())->where('target_id', $targetId) ->where('claim_type', $this->claimType)->first() !== null; diff --git a/src/ToggleMenus/AbstractClaimItem.php b/src/ToggleMenus/AbstractClaimItem.php index 5381e4d..1cb8993 100644 --- a/src/ToggleMenus/AbstractClaimItem.php +++ b/src/ToggleMenus/AbstractClaimItem.php @@ -60,9 +60,9 @@ public function getAction() XeFrontend::translation([ 'claim::msgClaimReceived', 'claim::msgClaimCanceled', + 'claim::enterClaimReason' ]); - if ($handler->has($this->identifier, Auth::user()) === true) { return sprintf( 'ClaimToggleMenu.destroyClaim(event, "%s", "%s", "%s")', diff --git a/src/ToggleMenus/UserClaimItem.php b/src/ToggleMenus/UserClaimItem.php new file mode 100644 index 0000000..b678b10 --- /dev/null +++ b/src/ToggleMenus/UserClaimItem.php @@ -0,0 +1,6 @@ +name; + } + + /** + * get text + * @return string + */ + public function getText() + { + return $this->text; + } + + /** + * get class + * @return string + */ + public function getClass() + { + return $this->class; + } + + /** + * register claim type + * @return void + */ + public function register() + { + + } + + /** + * report target + * @return void + */ + public function report( + ClaimHandler $handler, + UserInterface $author, + string $targetId, + string $shortCut, + string $message = '' + ) { + $handler->add($targetId, $author, $shortCut, $message); + } + + /** + * check report conditions + * @param ClaimHandler $handler + * @param UserInterface $author + * @param T $target + * @return void + */ + public function checkReportConditions( + ClaimHandler $handler, + UserInterface $author, + $target + ) { + // TODO 중복 신고 ON/OFF 기능 추가 후 적용 + /*if ($handler->has($target, $author) === true) { + throw new AlreadyClaimedException(); + }*/ + } +} diff --git a/src/Types/ClaimTypeBoard.php b/src/Types/ClaimTypeBoard.php new file mode 100644 index 0000000..c832d13 --- /dev/null +++ b/src/Types/ClaimTypeBoard.php @@ -0,0 +1,75 @@ +checkReportConditions($handler, $author, $target); + + parent::report($handler, $author, $targetId, $shortCut, $message); + } + + /** + * check report conditions + * @param ClaimHandler $handler + * @param UserInterface $author + * @param $target + * @return void + */ + public function checkReportConditions( + ClaimHandler $handler, + UserInterface $author, + $target + ) { + /** @var \Xpressengine\Plugins\Board\Models\Board $target */ + if (($targetUser = $target->getAuthor()) && $targetUser instanceof User) { + if ($targetUser->getId() === $author->getId()) { + throw new CantReportMyselfException(); + } + + if ($targetUser->isAdmin()) { + throw new CantReportAdminException(); + } + } + + parent::checkReportConditions($handler, $author, $target->getId()); + } +} diff --git a/src/Types/ClaimTypeComment.php b/src/Types/ClaimTypeComment.php new file mode 100644 index 0000000..591eae3 --- /dev/null +++ b/src/Types/ClaimTypeComment.php @@ -0,0 +1,75 @@ +checkReportConditions($handler, $author, $target); + + parent::report($handler, $author, $targetId, $shortCut, $message); + } + + /** + * check report conditions + * @param ClaimHandler $handler + * @param UserInterface $author + * @param $target + * @return void + */ + public function checkReportConditions( + ClaimHandler $handler, + UserInterface $author, + $target + ) { + /** @var \Xpressengine\Plugins\Comment\Models\Comment $target */ + if (($targetUser = $target->getAuthor()) && $targetUser instanceof User) { + if ($targetUser->getId() === $author->getId()) { + throw new CantReportMyselfException(); + } + + if ($targetUser->isAdmin()) { + throw new CantReportAdminException(); + } + } + + parent::checkReportConditions($handler, $author, $target->id); + } +} diff --git a/src/Types/ClaimTypeUser.php b/src/Types/ClaimTypeUser.php new file mode 100644 index 0000000..9c26b66 --- /dev/null +++ b/src/Types/ClaimTypeUser.php @@ -0,0 +1,90 @@ +checkReportConditions($handler, $author, $target); + + parent::report($handler, $author, $targetId, $shortCut, $message); + } + + /** + * check report conditions + * @param ClaimHandler $handler + * @param UserInterface $author + * @param $target + * @return void + */ + public function checkReportConditions( + ClaimHandler $handler, + UserInterface $author, + $target + ) { + if (($author instanceof User) === false) { + throw new GuestCannotReportException(); + } + + if ($target->getId() === $author->getId()) { + throw new CantReportMyselfException(); + } + + if ($target->isAdmin()) { + throw new CantReportAdminException(); + } + + if ($target->getStatus() !== User::STATUS_ACTIVATED) { + throw new CantReportWithdrawnUserException(); + } + + parent::checkReportConditions($handler, $author, $target->getId()); + } +} From 8fd770ff06b14322d531208233f3363b5af03e0b Mon Sep 17 00:00:00 2001 From: gyorudayo Date: Sun, 29 Jan 2023 23:53:44 +0900 Subject: [PATCH 3/6] =?UTF-8?q?Refactor:=20=EB=9D=BC=EC=9A=B0=ED=84=B0=20?= =?UTF-8?q?=EB=B0=8F=20=EB=A7=88=EC=9D=B4=EA=B7=B8=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=85=98=20=EC=BD=94=EB=93=9C=20=EB=B0=8F=20=EC=8B=A0=EA=B3=A0?= =?UTF-8?q?=20=EA=B8=B0=EB=8A=A5=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 라우터, 마이그레이션 코드 구분 - 신고 기능 관련 공통된 예외처리를 핸들러에서 처리하도록 수정 (같은 코드 중복 선언 방지) - 핸들러 자체에 claim type을 지정하여 사용하는 방식을 제거 --- plugin.php | 140 +++++++++---------------- routes.php | 33 ++++++ src/Controllers/ClaimController.php | 54 ++-------- src/Handler.php | 109 +++++++++++++------ src/Migrations/MigrationResource.php | 94 +++++++++++++++++ src/Migrations/Table/ClaimLogTable.php | 72 +++++++++++++ src/Models/ClaimLog.php | 2 + src/ToggleMenus/AbstractClaimItem.php | 37 +++---- src/Types/AbstractClaimType.php | 21 ++-- src/Types/ClaimTypeBoard.php | 15 ++- src/Types/ClaimTypeComment.php | 15 ++- src/Types/ClaimTypeUser.php | 21 ++-- 12 files changed, 372 insertions(+), 241 deletions(-) create mode 100644 routes.php create mode 100644 src/Migrations/MigrationResource.php create mode 100644 src/Migrations/Table/ClaimLogTable.php diff --git a/plugin.php b/plugin.php index 2839023..f04affa 100644 --- a/plugin.php +++ b/plugin.php @@ -14,12 +14,10 @@ namespace Xpressengine\Plugins\Claim; -use Illuminate\Database\Schema\Blueprint; use Route; use Schema; use XeToggleMenu; use Xpressengine\Plugin\AbstractPlugin; -use Xpressengine\Plugin\PluginRegister; use Xpressengine\Plugins\Claim\Types; /** @@ -38,45 +36,53 @@ class Plugin extends AbstractPlugin * claim types * @var string[] */ - protected $claimTypes = [ + protected $defaultClaimTypes = [ Types\ClaimTypeUser::class, Types\ClaimTypeBoard::class, Types\ClaimTypeComment::class, ]; + /** + * @var Migrations\MigrationResource + */ + protected $migrationResource; + + /** + * @return void + */ + public function __construct() + { + $this->migrationResource = app(Migrations\MigrationResource::class); + + parent::__construct(); + } + /** * boot * @return void */ public function boot() { - $this->registerToggleMenu(); - $this->registerManageRoute(); - $this->registerFixedRoute(); + $this->registerRoute(); $this->registerSettingsMenu(); app()->singleton(Handler::class, function () { $proxyClass = app('xe.interception')->proxy(Handler::class); - return new $proxyClass(app('xe.config'), $this->getActivateClaimTypes()); + return new $proxyClass(app('xe.config'), $this->getActivatableClaimTypes()); }); app()->alias(Handler::class, 'xe.claim.handler'); } - /** - * activate - * @param null $installedVersion installed version - * @return void - */ - public function activate($installedVersion = null) - { - } - /** * check for updates * @return boolean */ - public function checkUpdated($installedVersion = NULL) + public function checkUpdated($installedVersion = null) { + if ($this->migrationResource->checkInstalled() === false) { + return false; + } + if (version_compare($installedVersion, '0.9.1', '<=')) { $toggleMenuId = 'module/board@board'; $activated = XeToggleMenu::getActivated($toggleMenuId); @@ -98,13 +104,20 @@ public function checkUpdated($installedVersion = NULL) public function update($installedVersion = null) { + if ($this->migrationResource->checkInstalled() === false) { + $this->migrationResource->install(); + } + $this->putLang(); $this->setToggleMenuConfigs(); } public function install() { - $this->createClaimLogTable(); + if ($this->migrationResource->checkInstalled() === false) { + $this->migrationResource->install(); + } + $this->putLang(); $this->setToggleMenuConfigs(); } @@ -115,9 +128,9 @@ public function install() */ protected function setToggleMenuConfigs() { - $this->setToggleMenuConfig('module/board@board', 'module/board@board/toggleMenu/claim@boardClaimItem'); - $this->setToggleMenuConfig('comment', 'comment/toggleMenu/claim@commentClaimItem'); $this->setToggleMenuConfig('user', 'user/toggleMenu/claim@userClaimItem'); + $this->setToggleMenuConfig('comment', 'comment/toggleMenu/claim@commentClaimItem'); + $this->setToggleMenuConfig('module/board@board', 'module/board@board/toggleMenu/claim@boardClaimItem'); } protected function setToggleMenuConfig(string $toggleMenuId, string $itemId) @@ -130,92 +143,33 @@ protected function setToggleMenuConfig(string $toggleMenuId, string $itemId) } } - public function createClaimLogTable() - { - if (Schema::hasTable('claim_logs') === false) { - Schema::create('claim_logs', function (Blueprint $table) { - $table->bigIncrements('id'); - $table->string('claim_type', 36); - $table->string('short_cut', 255); - $table->string('target_id', 36); - $table->string('user_id', 36); - $table->string('ipaddress', 16); - $table->string('message', 255); - $table->timestamp('created_at'); - $table->timestamp('updated_at'); - - $table->index(['target_id', 'user_id']); - $table->index(['target_id', 'claim_type']); - }); - } - } - - protected function putLang() - { - app('xe.translator')->putFromLangDataSource('claim', base_path('plugins/claim/langs/lang.php')); - } - - /** - * register toggle menu - * @return void - */ - protected function registerToggleMenu() - { - /** @var PluginRegister $pluginRegister */ - $pluginRegister = app('xe.pluginRegister'); - $pluginRegister->add(ToggleMenus\BoardClaimItem::class); - $pluginRegister->add(ToggleMenus\CommentClaimItem::class); - $pluginRegister->add(ToggleMenus\UserClaimItem::class); - } - /** - * register plugin settings routes + * put lang * @return void */ - protected function registerManageRoute() + protected function putLang() { - Route::settings(self::getId(), function () { - Route::get( - '/', - [ - 'as' => 'manage.claim.claim.index', - 'uses' => 'ManagerController@index', - 'settings_menu' => 'contents.claim' - ] - ); - Route::post('delete', ['as' => 'manage.claim.claim.delete', 'uses' => 'ManagerController@delete']); - Route::get('config', ['as' => 'manage.claim.claim.config', 'uses' => 'ManagerController@config']); - Route::get( - 'config/edit', - ['as' => 'manage.claim.claim.config.edit', 'uses' => 'ManagerController@configEdit'] - ); - Route::post( - 'config/update', - ['as' => 'manage.claim.claim.config.update', 'uses' => 'ManagerController@configUpdate'] - ); - }, ['namespace' => 'Xpressengine\Plugins\Claim\Controllers']); + app('xe.translator')->putFromLangDataSource( + static::getId(), + static::path('langs/lang.php') + ); } /** - * register fixed route + * register route * @return void */ - protected function registerFixedRoute() + protected function registerRoute() { - Route::fixed('claim', function () { - Route::get('', ['as' => 'fixed.claim.index', 'uses' => 'ClaimController@index']); - Route::post('store', ['as' => 'fixed.claim.store', 'uses' => 'ClaimController@store']); - Route::post('destroy', ['as' => 'fixed.claim.destroy', 'uses' => 'ClaimController@destroy']); - }, ['namespace' => 'Xpressengine\Plugins\Claim\Controllers']); + Route::middleware('web')->group(static::path('routes.php')); } /** * register interception * @return void */ - public static function registerSettingsMenu() + protected function registerSettingsMenu() { - // settings menu 등록 $menus = [ 'contents.claim' => [ 'title' => 'xe::claim', @@ -230,15 +184,15 @@ public static function registerSettingsMenu() } /** - * get activate claim types + * get activatable claim types * @return array */ - public function getActivateClaimTypes() + public function getActivatableClaimTypes() { $types = []; - foreach ($this->claimTypes as $claimType) { + foreach ($this->defaultClaimTypes as $claimType) { $type = app($claimType); - if ($type instanceof Types\AbstractClaimType && class_exists($type->getClass())) { + if (assert($type instanceof Types\AbstractClaimType) && class_exists($type->getClass())) { $types[$type->getName()] = $type; } } diff --git a/routes.php b/routes.php new file mode 100644 index 0000000..cdb96f7 --- /dev/null +++ b/routes.php @@ -0,0 +1,33 @@ + 'manage.claim.claim.index', + 'uses' => 'ManagerController@index', + 'settings_menu' => 'contents.claim' + ] + ); + Route::post('delete', ['as' => 'manage.claim.claim.delete', 'uses' => 'ManagerController@delete']); + Route::get('config', ['as' => 'manage.claim.claim.config', 'uses' => 'ManagerController@config']); + Route::get( + 'config/edit', + ['as' => 'manage.claim.claim.config.edit', 'uses' => 'ManagerController@configEdit'] + ); + Route::post( + 'config/update', + ['as' => 'manage.claim.claim.config.update', 'uses' => 'ManagerController@configUpdate'] + ); +}, ['namespace' => 'Xpressengine\Plugins\Claim\Controllers']); + + +// fixed routes +Route::fixed('claim', function () { + Route::get('', ['as' => 'fixed.claim.index', 'uses' => 'ClaimController@index']); + Route::post('store', ['as' => 'fixed.claim.store', 'uses' => 'ClaimController@store']); + Route::post('destroy', ['as' => 'fixed.claim.destroy', 'uses' => 'ClaimController@destroy']); +}, ['namespace' => 'Xpressengine\Plugins\Claim\Controllers']); diff --git a/src/Controllers/ClaimController.php b/src/Controllers/ClaimController.php index b68b93b..ccb463b 100644 --- a/src/Controllers/ClaimController.php +++ b/src/Controllers/ClaimController.php @@ -19,12 +19,9 @@ use Auth; use XePresenter; use Illuminate\Http\Request; -use Illuminate\Database\Eloquent\ModelNotFoundException; use App\Http\Controllers\Controller; -use Xpressengine\Support\Exceptions\LoginRequiredHttpException; use Xpressengine\Plugins\Claim\Handler; -use Xpressengine\Plugins\Claim\Exceptions\ClaimException; -use Xpressengine\Plugins\Claim\Exceptions\NotSupportClaimTypeException; +use Xpressengine\Support\Exceptions\LoginRequiredHttpException; /** * ClaimController @@ -44,7 +41,7 @@ class ClaimController extends Controller protected $handler; /** - * create instance + * @return void */ public function __construct() { @@ -52,26 +49,15 @@ public function __construct() } /** - * index - * * @return \Xpressengine\Presenter\Presentable */ public function index(Request $request) { $targetId = $request->get('targetId'); - $from = $request->get('from'); + $claimType = $request->get('from'); - try { - $this->handler->set($from); - $invoked = $this->handler->has($targetId, Auth::user()); - $count = $this->handler->count($targetId); - } catch (ClaimException $e) { - throw $e; - } catch (ModelNotFoundException $e) { - throw new NotSupportClaimTypeException(); - } catch (\Exception $e) { - throw new ClaimException(); - } + $invoked = $this->handler->has($claimType, $targetId, Auth::user()); + $count = $this->handler->count($claimType, $targetId); return XePresenter::makeApi([ 'invoked' => $invoked, @@ -80,8 +66,6 @@ public function index(Request $request) } /** - * store - * * @return \Xpressengine\Presenter\Presentable */ public function store(Request $request) @@ -92,43 +76,23 @@ public function store(Request $request) $targetId = $request->get('targetId'); $shortCut = $request->get('shortCut'); - $from = $request->get('from'); + $claimType = $request->get('from'); $message = $request->get('message') ?: ''; - try { - $this->handler->set($from); - $this->handler->report($targetId, Auth::user(), $shortCut, $message); - } catch (ClaimException $e) { - throw $e; - } catch (ModelNotFoundException $e) { - throw new NotSupportClaimTypeException(); - } catch (\Exception $e) { - throw new ClaimException(); - } + $this->handler->report($claimType, $targetId, Auth::user(), $shortCut, $message); return $this->index($request); } /** - * destroy - * * @return \Xpressengine\Presenter\Presentable */ public function destroy(Request $request) { $targetId = $request->get('targetId'); - $from = $request->get('from'); + $claimType = $request->get('from'); - try { - $this->handler->set($from); - $this->handler->removeByTargetId($targetId, Auth::user()); - } catch (ClaimException $e) { - throw $e; - } catch (ModelNotFoundException $e) { - throw new NotSupportClaimTypeException(); - } catch (\Exception $e) { - throw new ClaimException(); - } + $this->handler->removeByTargetId($claimType, $targetId, Auth::user()); return $this->index($request); } diff --git a/src/Handler.php b/src/Handler.php index ff4f545..102102d 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -16,6 +16,11 @@ namespace Xpressengine\Plugins\Claim; +use App\Facades\XeDB; +use Illuminate\Support\Facades\Log; +use Illuminate\Database\Eloquent\ModelNotFoundException; +use Xpressengine\Plugins\Claim\Exceptions\ClaimException; +use Xpressengine\Plugins\Claim\Exceptions\NotFoundClaimTargetException; use Xpressengine\User\UserInterface; use Xpressengine\Config\ConfigManager; use Xpressengine\Plugins\Claim\Models\ClaimLog; @@ -44,11 +49,6 @@ class Handler */ protected $activateClaimTypes = []; - /** - * @var string - */ - protected $claimType; - /** * @var string */ @@ -65,27 +65,42 @@ public function __construct(ConfigManager $configManager, array $activateClaimTy } /** - * claim 에서 사용 할 type name 설정 - * @param string $claimType + * claim type 등록 + * @param AbstractClaimType $claimType * @return void */ - public function set(string $claimType) + public function registerClaimType(AbstractClaimType $claimType) { - if (!array_key_exists($claimType, $this->activateClaimTypes)) { - throw new NotSupportClaimTypeException(); + if (!array_key_exists($claimType->getName(), $this->activateClaimTypes)) { + $this->activateClaimTypes[$claimType->getName()] = $claimType; } - - $this->claimType = $claimType; } /** - * 신고 수 + * 해당 컨텐츠가 신고된 횟수를 반환 + * @param string $claimType * @param string $targetId * @return int */ - public function count(string $targetId) + public function count(string $claimType, string $targetId) + { + return ClaimLog::where('target_id', $targetId) + ->where('claim_type', $claimType) + ->count(); + } + + /** + * 해당 키의 claim type을 반환 + * @param string $claimType + * @return AbstractClaimType + */ + public function getClaimTypeByKey(string $claimType) { - return ClaimLog::where('target_id', $targetId)->where('claim_type', $this->claimType)->count(); + if (!array_key_exists($claimType, $this->activateClaimTypes)) { + throw new NotSupportClaimTypeException(); + } + + return $this->activateClaimTypes[$claimType]; } /** @@ -100,53 +115,80 @@ public function remove(int $id) /** * 신고 로그 삭제 + * @param string $claimType * @param string $targetId * @param UserInterface $author * @return void */ - public function removeByTargetId(string $targetId, UserInterface $author) + public function removeByTargetId(string $claimType, string $targetId, UserInterface $author) { - ClaimLog::where('user_id', $author->getId())->where('target_id', $targetId) - ->where('claim_type', $this->claimType)->delete(); + ClaimLog::where('user_id', $author->getKey()) + ->where('target_id', $targetId) + ->where('claim_type', $claimType) + ->delete(); } /** * 대상을 신고 + * @param string $claimType * @param string $targetId * @param UserInterface $author * @param string $shortCut * @param string $message * @return void */ - public function report(string $targetId, UserInterface $author, string $shortCut, string $message = '') - { - if (!array_key_exists($this->claimType, $this->activateClaimTypes)) { - throw new NotSupportClaimTypeException(); + public function report( + string $claimType, + string $targetId, + UserInterface $author, + string $shortCut, + string $message = '' + ) { + XeDB::beginTransaction(); + + try { + $claimType = $this->getClaimTypeByKey($claimType); + $claimType->report($author, $targetId, $shortCut, $message); + } catch (ClaimException $e) { + XeDB::rollback(); + throw $e; + } catch (ModelNotFoundException $e) { + XeDB::rollback(); + throw new NotFoundClaimTargetException(); + } catch (\Exception $e) { + XeDB::rollback(); + Log::error($e); + throw new ClaimException(); } - $claimType = $this->activateClaimTypes[$this->claimType]; - $claimType->report($this, $author, $targetId, $shortCut, $message); + XeDB::commit(); } /** * 신고 로그 등록 + * @param string $claimType * @param string $targetId * @param UserInterface $author * @param string $shortCut * @param string $message * @return ClaimLog */ - public function add(string $targetId, UserInterface $author, string $shortCut, string $message = '') - { - if ($this->has($targetId, $author) === true) { + public function add( + string $claimType, + string $targetId, + UserInterface $author, + string $shortCut, + string $message = '' + ) { + if ($this->has($claimType, $targetId, $author) === true) { throw new Exceptions\AlreadyClaimedException; } return ClaimLog::create([ - 'claim_type' => $this->claimType, + 'claim_type' => $claimType, 'short_cut' => $shortCut, 'target_id' => $targetId, - 'user_id' => $author->getId(), + 'user_id' => $author->getKey(), 'message' => $message, 'ipaddress' => $_SERVER['REMOTE_ADDR'], ]); @@ -154,13 +196,16 @@ public function add(string $targetId, UserInterface $author, string $shortCut, s /** * 신고 여부 + * @param string $claimType * @param string $targetId * @param UserInterface $author * @return bool */ - public function has(string $targetId, UserInterface $author) + public function has(string $claimType, string $targetId, UserInterface $author) { - return ClaimLog::where('user_id', $author->getId())->where('target_id', $targetId) - ->where('claim_type', $this->claimType)->first() !== null; + return ClaimLog::where('user_id', $author->getKey()) + ->where('target_id', $targetId) + ->where('claim_type', $claimType) + ->exists(); } } diff --git a/src/Migrations/MigrationResource.php b/src/Migrations/MigrationResource.php new file mode 100644 index 0000000..9a360fa --- /dev/null +++ b/src/Migrations/MigrationResource.php @@ -0,0 +1,94 @@ +getSupportMigrations()->each( + function ($migration) { + $migration->install(); + } + ); + } + + /** + * check installed + * @return bool + */ + public function checkInstalled() + { + $isInstalled = true; + + $this->getSupportMigrations()->each( + function ($migration) use (&$isInstalled) { + if ($migration->checkInstalled() === false) { + $isInstalled = false; + return false; + } + + return true; + } + ); + + return $isInstalled; + } + + /** + * update migrations + * @param string|null $installedVersion + * @return void + */ + public function update(string $installedVersion = null) + { + $this->getSupportMigrations()->each( + function ($migration) use ($installedVersion) { + $migration->update($installedVersion); + } + ); + } + + /** + * check updated + * @param string|null $installedVersion + * @return bool + */ + public function checkUpdated(string $installedVersion = null) + { + $isUpdated = true; + + $this->getSupportMigrations()->each( + function ($migration) use (&$isUpdated, $installedVersion) { + if ($migration->checkUpdated() === false) { + $isUpdated = false; + return false; + } + + return true; + } + ); + + return $isUpdated; + } + + /** + * @return Collection + */ + protected function getSupportMigrations() + { + return collect([ + app(Table\ClaimLogTable::class) + ]); + } +} diff --git a/src/Migrations/Table/ClaimLogTable.php b/src/Migrations/Table/ClaimLogTable.php new file mode 100644 index 0000000..82e81a1 --- /dev/null +++ b/src/Migrations/Table/ClaimLogTable.php @@ -0,0 +1,72 @@ +checkInstalled() === false) { + $this->createTable(); + } + } + + /** + * @return bool + */ + public function checkInstalled() + { + return Schema::hasTable(ClaimLog::TABLE_NAME); + } + + /** + * @return void + */ + protected function createTable() + { + Schema::create(ClaimLog::TABLE_NAME, function (Blueprint $table) { + $table->bigIncrements('id'); + $table->string('claim_type', 36); + $table->string('short_cut', 255); + $table->string('target_id', 36); + $table->string('user_id', 36); + $table->string('ipaddress', 16); + $table->string('message', 255); + $table->timestamp('created_at'); + $table->timestamp('updated_at'); + + $table->index(['target_id', 'user_id']); + $table->index(['target_id', 'claim_type']); + }); + } + + /** + * @param $installedVersion + * @return bool + */ + public function checkUpdated($installedVersion = null) + { + return true; + } + + /** + * @param $installedVersion + * @return void + */ + public function update($installedVersion = null) + { + + } +} diff --git a/src/Models/ClaimLog.php b/src/Models/ClaimLog.php index bb5b4b5..586874e 100644 --- a/src/Models/ClaimLog.php +++ b/src/Models/ClaimLog.php @@ -41,6 +41,8 @@ */ class ClaimLog extends DynamicModel { + const TABLE_NAME = 'claim_logs'; + /** * @var string[] */ diff --git a/src/ToggleMenus/AbstractClaimItem.php b/src/ToggleMenus/AbstractClaimItem.php index 1cb8993..2284d7c 100644 --- a/src/ToggleMenus/AbstractClaimItem.php +++ b/src/ToggleMenus/AbstractClaimItem.php @@ -14,6 +14,17 @@ */ abstract class AbstractClaimItem extends AbstractToggleMenu { + /** @var Handler */ + protected $claimHandler; + + /** + * @return void + */ + public function __construct() + { + $this->claimHandler = app(Handler::class); + } + /** * get text * @@ -21,20 +32,11 @@ abstract class AbstractClaimItem extends AbstractToggleMenu */ public function getText() { - $handler = $this->claimHandler(); - - $count = $handler->count($this->identifier); - $invoked = $handler->has($this->identifier, Auth::user()); - $text = 'xe::claim'; - if ($invoked === true) { + if ($this->claimHandler->has($this->componentType, $this->identifier, Auth::user())) { $text = 'xe::cancelClaim'; } - if ($count > 0) { - return sprintf('%s (%s)', xe_trans($text), $count); - } - return xe_trans($text); } @@ -55,15 +57,13 @@ public function getType() */ public function getAction() { - $handler = $this->claimHandler(); - XeFrontend::translation([ 'claim::msgClaimReceived', 'claim::msgClaimCanceled', 'claim::enterClaimReason' ]); - if ($handler->has($this->identifier, Auth::user()) === true) { + if ($this->claimHandler->has($this->componentType, $this->identifier, Auth::user())) { return sprintf( 'ClaimToggleMenu.destroyClaim(event, "%s", "%s", "%s")', route('fixed.claim.destroy'), @@ -91,15 +91,4 @@ public function getScript() $path = '/plugins/claim/assets/menu.js'; return asset(str_replace(base_path(), '', $path)); } - - /** - * @return Handler - */ - private function claimHandler() - { - $handler = app('xe.claim.handler'); - $handler->set($this->componentType); - - return $handler; - } } diff --git a/src/Types/AbstractClaimType.php b/src/Types/AbstractClaimType.php index 3c200ad..7caf2b8 100644 --- a/src/Types/AbstractClaimType.php +++ b/src/Types/AbstractClaimType.php @@ -70,30 +70,21 @@ public function register() * report target * @return void */ - public function report( - ClaimHandler $handler, - UserInterface $author, - string $targetId, - string $shortCut, - string $message = '' - ) { - $handler->add($targetId, $author, $shortCut, $message); + public function report(UserInterface $author, string $targetId, string $shortCut, string $message = '') + { + app('xe.claim.handler')->add($this->name, $targetId, $author, $shortCut, $message); } /** * check report conditions - * @param ClaimHandler $handler * @param UserInterface $author * @param T $target * @return void */ - public function checkReportConditions( - ClaimHandler $handler, - UserInterface $author, - $target - ) { + public function checkReportConditions(UserInterface $author, $target) + { // TODO 중복 신고 ON/OFF 기능 추가 후 적용 - /*if ($handler->has($target, $author) === true) { + /*if (app('xe.claim.handler')->has($target, $author) === true) { throw new AlreadyClaimedException(); }*/ } diff --git a/src/Types/ClaimTypeBoard.php b/src/Types/ClaimTypeBoard.php index c832d13..2a5170a 100644 --- a/src/Types/ClaimTypeBoard.php +++ b/src/Types/ClaimTypeBoard.php @@ -4,7 +4,6 @@ use Xpressengine\User\Models\User; use Xpressengine\User\UserInterface; -use Xpressengine\Plugins\Claim\Handler as ClaimHandler; use Xpressengine\Plugins\Claim\Exceptions\CantReportAdminException; use Xpressengine\Plugins\Claim\Exceptions\CantReportMyselfException; @@ -33,7 +32,6 @@ class ClaimTypeBoard extends AbstractClaimType * @return void */ public function report( - ClaimHandler $handler, UserInterface $author, string $targetId, string $shortCut, @@ -42,26 +40,25 @@ public function report( /** @var \Xpressengine\Plugins\Board\Models\Board $target */ $target = \Xpressengine\Plugins\Board\Models\Board::findOrFail($targetId); - $this->checkReportConditions($handler, $author, $target); + $this->checkReportConditions($author, $target); - parent::report($handler, $author, $targetId, $shortCut, $message); + parent::report($author, $targetId, $shortCut, $message); } /** * check report conditions - * @param ClaimHandler $handler * @param UserInterface $author * @param $target * @return void */ public function checkReportConditions( - ClaimHandler $handler, UserInterface $author, $target ) { - /** @var \Xpressengine\Plugins\Board\Models\Board $target */ + assert($target instanceof \Xpressengine\Plugins\Board\Models\Board); + if (($targetUser = $target->getAuthor()) && $targetUser instanceof User) { - if ($targetUser->getId() === $author->getId()) { + if ($targetUser->getKey() === $author->getKey()) { throw new CantReportMyselfException(); } @@ -70,6 +67,6 @@ public function checkReportConditions( } } - parent::checkReportConditions($handler, $author, $target->getId()); + parent::checkReportConditions($author, $target->getKey()); } } diff --git a/src/Types/ClaimTypeComment.php b/src/Types/ClaimTypeComment.php index 591eae3..84512ef 100644 --- a/src/Types/ClaimTypeComment.php +++ b/src/Types/ClaimTypeComment.php @@ -4,7 +4,6 @@ use Xpressengine\User\Models\User; use Xpressengine\User\UserInterface; -use Xpressengine\Plugins\Claim\Handler as ClaimHandler; use Xpressengine\Plugins\Claim\Exceptions\CantReportAdminException; use Xpressengine\Plugins\Claim\Exceptions\CantReportMyselfException; @@ -33,7 +32,6 @@ class ClaimTypeComment extends AbstractClaimType * @return void */ public function report( - ClaimHandler $handler, UserInterface $author, string $targetId, string $shortCut, @@ -42,26 +40,25 @@ public function report( /** @var \Xpressengine\Plugins\Comment\Models\Comment $target */ $target = \Xpressengine\Plugins\Comment\Models\Comment::findOrFail($targetId); - $this->checkReportConditions($handler, $author, $target); + $this->checkReportConditions($author, $target); - parent::report($handler, $author, $targetId, $shortCut, $message); + parent::report($author, $targetId, $shortCut, $message); } /** * check report conditions - * @param ClaimHandler $handler * @param UserInterface $author * @param $target * @return void */ public function checkReportConditions( - ClaimHandler $handler, UserInterface $author, $target ) { - /** @var \Xpressengine\Plugins\Comment\Models\Comment $target */ + assert($target instanceof \Xpressengine\Plugins\Comment\Models\Comment); + if (($targetUser = $target->getAuthor()) && $targetUser instanceof User) { - if ($targetUser->getId() === $author->getId()) { + if ($targetUser->getKey() === $author->getKey()) { throw new CantReportMyselfException(); } @@ -70,6 +67,6 @@ public function checkReportConditions( } } - parent::checkReportConditions($handler, $author, $target->id); + parent::checkReportConditions($author, $target->id); } } diff --git a/src/Types/ClaimTypeUser.php b/src/Types/ClaimTypeUser.php index 9c26b66..0c8ff26 100644 --- a/src/Types/ClaimTypeUser.php +++ b/src/Types/ClaimTypeUser.php @@ -4,7 +4,6 @@ use Xpressengine\User\Models\User; use Xpressengine\User\UserInterface; -use Xpressengine\Plugins\Claim\Handler as ClaimHandler; use Xpressengine\Plugins\Claim\Exceptions\CantReportAdminException; use Xpressengine\Plugins\Claim\Exceptions\CantReportMyselfException; use Xpressengine\Plugins\Claim\Exceptions\GuestCannotReportException; @@ -42,9 +41,7 @@ public function register() * report target * @return void */ - public function report( - ClaimHandler $handler, - UserInterface $author, + public function report(UserInterface $author, string $targetId, string $shortCut, string $message = '' @@ -52,28 +49,24 @@ public function report( /** @var User $target */ $target = User::findOrFail($targetId); - $this->checkReportConditions($handler, $author, $target); + $this->checkReportConditions($author, $target); - parent::report($handler, $author, $targetId, $shortCut, $message); + parent::report($author, $targetId, $shortCut, $message); } /** * check report conditions - * @param ClaimHandler $handler * @param UserInterface $author * @param $target * @return void */ - public function checkReportConditions( - ClaimHandler $handler, - UserInterface $author, - $target - ) { + public function checkReportConditions(UserInterface $author, $target) + { if (($author instanceof User) === false) { throw new GuestCannotReportException(); } - if ($target->getId() === $author->getId()) { + if ($target->getKey() === $author->getKey()) { throw new CantReportMyselfException(); } @@ -85,6 +78,6 @@ public function checkReportConditions( throw new CantReportWithdrawnUserException(); } - parent::checkReportConditions($handler, $author, $target->getId()); + parent::checkReportConditions($author, $target->getKey()); } } From 686648d64899671120fb8e306be0a255d709dd78 Mon Sep 17 00:00:00 2001 From: gyorudayo Date: Wed, 22 Feb 2023 10:30:26 +0900 Subject: [PATCH 4/6] =?UTF-8?q?Refactor:=20=EA=B0=81=20=ED=83=80=EC=9E=85?= =?UTF-8?q?=20=EB=A1=9C=EC=A7=81=EC=9A=A9=20Handler=20=EC=9E=91=EC=84=B1?= =?UTF-8?q?=20=EB=B0=8F=20=EB=A6=AC=ED=8F=AC=EC=A7=80=ED=86=A0=EB=A6=AC=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80,=20=EA=B4=80=EB=A6=AC=EC=9E=90=EC=9A=A9=20?= =?UTF-8?q?=EC=8B=A0=EA=B3=A0=20=EA=B4=80=EB=A6=AC=20=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=A7=80=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 5 - langs/lang.php | 24 +++ plugin.php | 52 ++--- routes.php | 54 +++-- src/Controllers/ClaimController.php | 11 +- src/Controllers/ClaimSettingsController.php | 121 +++++++++++ src/Controllers/ManagerController.php | 88 -------- src/Handler.php | 201 ++++++++++++++---- src/Handlers/AbstractClaimTypeHandler.php | 57 +++++ src/Handlers/BoardClaimTypeHandler.php | 60 ++++++ src/Handlers/CommentClaimTypeHandler.php | 60 ++++++ src/Handlers/UserClaimTypeHandler.php | 64 ++++++ src/Migrations/Table/ClaimLogTable.php | 6 +- src/Models/ClaimLog.php | 29 ++- src/Repositories/ClaimRepository.php | 15 ++ src/Skin/SettingsSkin.php | 52 ----- src/ToggleMenus/AbstractClaimItem.php | 4 +- src/Types/AbstractClaimType.php | 41 +--- src/Types/ClaimTypeBoard.php | 55 +---- src/Types/ClaimTypeComment.php | 55 +---- src/Types/ClaimTypeUser.php | 64 +----- views/settings/edit.blade.php | 94 +++++++++ views/settings/index.blade.php | 221 ++++++++++++++++++++ views/settingsSkin/_frame.blade.php | 10 - views/settingsSkin/index.blade.php | 116 ---------- 25 files changed, 996 insertions(+), 563 deletions(-) create mode 100644 src/Controllers/ClaimSettingsController.php delete mode 100644 src/Controllers/ManagerController.php create mode 100644 src/Handlers/AbstractClaimTypeHandler.php create mode 100644 src/Handlers/BoardClaimTypeHandler.php create mode 100644 src/Handlers/CommentClaimTypeHandler.php create mode 100644 src/Handlers/UserClaimTypeHandler.php create mode 100644 src/Repositories/ClaimRepository.php delete mode 100644 src/Skin/SettingsSkin.php create mode 100644 views/settings/edit.blade.php create mode 100644 views/settings/index.blade.php delete mode 100644 views/settingsSkin/_frame.blade.php delete mode 100644 views/settingsSkin/index.blade.php diff --git a/composer.json b/composer.json index 5d12dc5..e2548bd 100644 --- a/composer.json +++ b/composer.json @@ -21,11 +21,6 @@ "title": "신고", "icon": "icon.png", "component": { - "claim/settingsSkin/claim@default": { - "class": "Xpressengine\\Plugins\\Claim\\Skin\\SettingsSkin", - "name": "신고하기 관리자 기본 스킨", - "description": "기본스킨." - }, "module/board@board/toggleMenu/claim@boardClaimItem": { "class": "Xpressengine\\Plugins\\Claim\\ToggleMenus\\BoardClaimItem", "name": "게시판 신고 토글 메뉴", diff --git a/langs/lang.php b/langs/lang.php index acb3b98..5e154b7 100644 --- a/langs/lang.php +++ b/langs/lang.php @@ -53,4 +53,28 @@ 'ko' => '잘못된 신고 대상 입니다.', 'en' => 'Invalid report target' ], + 'statusNew' => [ + 'ko' => '접수중', + 'en' => 'New' + ], + 'statusProcessing' => [ + 'ko' => '처리중', + 'en' => 'Processing' + ], + 'statusClosed' => [ + 'ko' => '처리됨', + 'en' => 'Closed' + ], + 'claimTypeBoard' => [ + 'ko' => '게시물', + 'en' => 'Board' + ], + 'claimTypeComment' => [ + 'ko' => '댓글', + 'en' => 'Comment' + ], + 'claimTypeUser' => [ + 'ko' => '유저', + 'en' => 'User' + ], ]; diff --git a/plugin.php b/plugin.php index f04affa..91aad51 100644 --- a/plugin.php +++ b/plugin.php @@ -1,17 +1,4 @@ - * @copyright 2019 Copyright XEHub Corp. - * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL - * @link https://xpressengine.io - */ - namespace Xpressengine\Plugins\Claim; use Route; @@ -19,21 +6,17 @@ use XeToggleMenu; use Xpressengine\Plugin\AbstractPlugin; use Xpressengine\Plugins\Claim\Types; +use Xpressengine\Plugins\Claim\Models\ClaimLog; +use Xpressengine\Plugins\Claim\Repositories\ClaimRepository; /** - * Claim Plugin - * - * @category Claim - * @package Xpressengine\Plugins\Claim - * @author XE Developers - * @copyright 2019 Copyright XEHub Corp. - * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL - * @link https://xpressengine.io + * Class Plugin + * @package Xpressengine\Plugins\Claim */ class Plugin extends AbstractPlugin { /** - * claim types + * default claim types * @var string[] */ protected $defaultClaimTypes = [ @@ -66,9 +49,15 @@ public function boot() $this->registerRoute(); $this->registerSettingsMenu(); + ClaimRepository::setModel(ClaimLog::class); + app()->singleton(Handler::class, function () { $proxyClass = app('xe.interception')->proxy(Handler::class); - return new $proxyClass(app('xe.config'), $this->getActivatableClaimTypes()); + return new $proxyClass( + app(ClaimRepository::class), + app('xe.config'), + $this->defaultClaimTypes + ); }); app()->alias(Handler::class, 'xe.claim.handler'); } @@ -182,21 +171,4 @@ protected function registerSettingsMenu() app('xe.register')->push('settings/menu', $id, $menu); } } - - /** - * get activatable claim types - * @return array - */ - public function getActivatableClaimTypes() - { - $types = []; - foreach ($this->defaultClaimTypes as $claimType) { - $type = app($claimType); - if (assert($type instanceof Types\AbstractClaimType) && class_exists($type->getClass())) { - $types[$type->getName()] = $type; - } - } - - return $types; - } } diff --git a/routes.php b/routes.php index cdb96f7..1ba9a79 100644 --- a/routes.php +++ b/routes.php @@ -4,24 +4,42 @@ // settings routes Route::settings(Plugin::getId(), function () { - Route::get( - '/', - [ - 'as' => 'manage.claim.claim.index', - 'uses' => 'ManagerController@index', - 'settings_menu' => 'contents.claim' - ] - ); - Route::post('delete', ['as' => 'manage.claim.claim.delete', 'uses' => 'ManagerController@delete']); - Route::get('config', ['as' => 'manage.claim.claim.config', 'uses' => 'ManagerController@config']); - Route::get( - 'config/edit', - ['as' => 'manage.claim.claim.config.edit', 'uses' => 'ManagerController@configEdit'] - ); - Route::post( - 'config/update', - ['as' => 'manage.claim.claim.config.update', 'uses' => 'ManagerController@configUpdate'] - ); + Route::get('/', [ + 'as' => 'settings.claim.index', + 'uses' => 'ClaimSettingsController@index', + 'settings_menu' => 'contents.claim' + ]); + + Route::get('/{id}', [ + 'as' => 'settings.claim.edit', + 'uses' => 'ClaimSettingsController@edit' + ]); + + Route::put('/{id}', [ + 'as' => 'settings.claim.update', + 'uses' => 'ClaimSettingsController@update' + ]); + + Route::post('/{id}/delete', [ + 'as' => 'settings.claim.delete', + 'uses' => 'ClaimSettingsController@delete' + ]); + + Route::get('config', [ + 'as' => 'settings.claim.config', + 'uses' => 'ManagerController@config' + ]); + + Route::get('config/edit', [ + 'as' => 'settings.claim.config.edit', + 'uses' => 'ManagerController@configEdit' + ]); + + Route::post('config/update', [ + 'as' => 'settings.claim.config.update', + 'uses' => 'ManagerController@configUpdate' + ]); + }, ['namespace' => 'Xpressengine\Plugins\Claim\Controllers']); diff --git a/src/Controllers/ClaimController.php b/src/Controllers/ClaimController.php index ccb463b..b46a801 100644 --- a/src/Controllers/ClaimController.php +++ b/src/Controllers/ClaimController.php @@ -16,10 +16,9 @@ namespace Xpressengine\Plugins\Claim\Controllers; -use Auth; -use XePresenter; use Illuminate\Http\Request; use App\Http\Controllers\Controller; +use App\Facades\XePresenter; use Xpressengine\Plugins\Claim\Handler; use Xpressengine\Support\Exceptions\LoginRequiredHttpException; @@ -56,7 +55,7 @@ public function index(Request $request) $targetId = $request->get('targetId'); $claimType = $request->get('from'); - $invoked = $this->handler->has($claimType, $targetId, Auth::user()); + $invoked = $this->handler->exists($claimType, $targetId, auth()->user()); $count = $this->handler->count($claimType, $targetId); return XePresenter::makeApi([ @@ -70,7 +69,7 @@ public function index(Request $request) */ public function store(Request $request) { - if (Auth::check() === false) { + if (auth()->check() === false) { throw new LoginRequiredHttpException; } @@ -79,7 +78,7 @@ public function store(Request $request) $claimType = $request->get('from'); $message = $request->get('message') ?: ''; - $this->handler->report($claimType, $targetId, Auth::user(), $shortCut, $message); + $this->handler->report($claimType, $targetId, auth()->user(), $shortCut, $message); return $this->index($request); } @@ -92,7 +91,7 @@ public function destroy(Request $request) $targetId = $request->get('targetId'); $claimType = $request->get('from'); - $this->handler->removeByTargetId($claimType, $targetId, Auth::user()); + $this->handler->removeLogByTargetId($claimType, $targetId, auth()->user()); return $this->index($request); } diff --git a/src/Controllers/ClaimSettingsController.php b/src/Controllers/ClaimSettingsController.php new file mode 100644 index 0000000..f6ed38d --- /dev/null +++ b/src/Controllers/ClaimSettingsController.php @@ -0,0 +1,121 @@ + + * @copyright 2019 Copyright XEHub Corp. + * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL + * @link https://xpressengine.io + */ + +namespace Xpressengine\Plugins\Claim\Controllers; + +use Illuminate\Http\Request; +use App\Http\Controllers\Controller; +use App\Facades\XeDB; +use App\Facades\XePresenter; +use Xpressengine\Plugins\Claim\Handler; +use Xpressengine\Plugins\Claim\Models\ClaimLog; + +/** + * ClaimSettingsController + * + * @category Claim + * @package Xpressengine\Plugins\Claim + * @author XE Developers + * @copyright 2019 Copyright XEHub Corp. + * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL + * @link https://xpressengine.io + */ +class ClaimSettingsController extends Controller +{ + /** + * @var Handler + */ + protected $handler; + + /** + * @return void + */ + public function __construct(Handler $handler) + { + $this->handler = $handler; + } + + /** + * @return \Xpressengine\Presenter\Presentable + */ + public function index(Request $request) + { + $paginate = $this->handler->paginateClaimLogs($request->all()); + $claimTypes = $this->handler->getActivateClaimTypes(); + $claimStatuses = ClaimLog::STATUSES; + + return XePresenter::make('claim::views.settings.index', [ + 'paginate' => $paginate, + 'claimTypes' => $claimTypes, + 'claimStatuses' => $claimStatuses + ]); + } + + /** + * @param string $id + * @return mixed + */ + public function edit(string $id) + { + $log = $this->handler->findLogOrFail($id); + $claimStatuses = ClaimLog::STATUSES; + $targetClaimTypeText = xe_trans($this->handler->getClaimTypeByKey($log->claim_type)->getText()); + + session()->flash('settings_intended_url', url()->previous()); + + return XePresenter::make('claim::views.settings.edit', [ + 'log' => $log, + 'claimStatuses' => $claimStatuses, + 'targetClaimTypeText' => $targetClaimTypeText + ]); + } + + /** + * @param Request $request + * @param string $id + * @return \Illuminate\Http\RedirectResponse + * @throws \Exception + */ + public function update(Request $request, string $id) + { + $status = $request->get('status'); + $adminMessage = $request->get('admin_message') ?: ''; + + XeDB::beginTransaction(); + try { + $this->handler->updateLog($id, $status, $adminMessage); + XeDB::commit(); + } catch (\Exception $e) { + XeDB::rollback(); + throw $e; + } + + return redirect()->intended(session()->get('settings_intended_url')) + ->with('alert', ['type' => 'success', 'message' => xe_trans('xe::saved')]); + } + + /** + * @param string $id + * @return \Illuminate\Http\RedirectResponse + * @throws \Exception + */ + public function delete(string $id) + { + $this->handler->removeLog($id); + + return redirect()->back()->with('alert', ['type' => 'success', 'message' => xe_trans('xe::deleted')]); + } +} diff --git a/src/Controllers/ManagerController.php b/src/Controllers/ManagerController.php deleted file mode 100644 index 38aa809..0000000 --- a/src/Controllers/ManagerController.php +++ /dev/null @@ -1,88 +0,0 @@ - - * @copyright 2019 Copyright XEHub Corp. - * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL - * @link https://xpressengine.io - */ - -namespace Xpressengine\Plugins\Claim\Controllers; - -use Request; -use View; -use Redirect; -use XePresenter; -use App; -use XeDB; -use Cfg; -use DynamicField; -use Validator; -use App\Http\Controllers\Controller; -use Xpressengine\Plugins\Claim\Handler; -use Xpressengine\Plugins\Claim\Models\ClaimLog; - -/** - * ManagerController - * - * @category Claim - * @package Xpressengine\Plugins\Claim - * @author XE Developers - * @copyright 2019 Copyright XEHub Corp. - * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL - * @link https://xpressengine.io - */ -class ManagerController extends Controller -{ - /** - * @var Handler - */ - protected $handler; - - /** - * ManagerController constructor. - */ - public function __construct() - { - XePresenter::setSettingsSkinTargetId('claim'); - } - - /** - * index - * - * @return \Xpressengine\Presenter\Presentable - */ - public function index() - { - $wheres = []; - $orders = []; - - $paginate = ClaimLog::orderBy('created_at', 'desc')->paginate(20)->appends(Request::except('page')); - - return XePresenter::make('index', [ - 'action' => 'index', - 'paginate' => $paginate, - ]); - } - - /** - * delete - * - * @return \Xpressengine\Presenter\Presentable - */ - public function delete() - { - $id = Request::get('id'); - - $this->handler->remove($id); - - return XePresenter::makeApi([]); - } -} diff --git a/src/Handler.php b/src/Handler.php index 102102d..0e6a22a 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -1,44 +1,26 @@ - * @copyright 2019 Copyright XEHub Corp. - * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL - * @link https://xpressengine.io - */ namespace Xpressengine\Plugins\Claim; use App\Facades\XeDB; +use Carbon\Carbon; use Illuminate\Support\Facades\Log; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\ModelNotFoundException; -use Xpressengine\Plugins\Claim\Exceptions\ClaimException; -use Xpressengine\Plugins\Claim\Exceptions\NotFoundClaimTargetException; use Xpressengine\User\UserInterface; use Xpressengine\Config\ConfigManager; use Xpressengine\Plugins\Claim\Models\ClaimLog; use Xpressengine\Plugins\Claim\Types\AbstractClaimType; +use Xpressengine\Plugins\Claim\Repositories\ClaimRepository; +use Xpressengine\Plugins\Claim\Exceptions\ClaimException; +use Xpressengine\Plugins\Claim\Exceptions\NotFoundClaimTargetException; use Xpressengine\Plugins\Claim\Exceptions\NotSupportClaimTypeException; -/** - * Handler - * - * @category Claim - * @package Xpressengine\Plugins\Claim - * @author XE Developers - * @copyright 2019 Copyright XEHub Corp. - * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL - * @link https://xpressengine.io - */ class Handler { + /** @var ClaimRepository */ + protected $repository; + /** * @var ConfigManager */ @@ -55,13 +37,24 @@ class Handler const CONFIG_NAME = 'Claim'; /** + * @param ClaimRepository $repository * @param ConfigManager $configManager - * @param array $activateClaimTypes + * @param array $defaultClaimTypeClasses */ - public function __construct(ConfigManager $configManager, array $activateClaimTypes) - { + public function __construct( + ClaimRepository $repository, + ConfigManager $configManager, + array $defaultClaimTypeClasses + ) { + $this->repository = $repository; $this->configManager = $configManager; - $this->activateClaimTypes = $activateClaimTypes; + $this->activateClaimTypes = []; + + foreach ($defaultClaimTypeClasses as $class) { + $targetClaimType = app($class); + assert($targetClaimType instanceof Types\AbstractClaimType); + $this->registerClaimType($targetClaimType); + } } /** @@ -71,11 +64,30 @@ public function __construct(ConfigManager $configManager, array $activateClaimTy */ public function registerClaimType(AbstractClaimType $claimType) { - if (!array_key_exists($claimType->getName(), $this->activateClaimTypes)) { + if (!array_key_exists($claimType->getName(), $this->activateClaimTypes) && class_exists($claimType->getClass())) { $this->activateClaimTypes[$claimType->getName()] = $claimType; } } + /** + * 활성화된 claim types 를 반환 + * @return array|AbstractClaimType[] + */ + public function getActivateClaimTypes() + { + return $this->activateClaimTypes; + } + + /** + * 해당 ID의 신고 로그를 반환 + * @param string $id + * @return ClaimLog + */ + public function findLogOrFail(string $id) + { + return $this->repository->findOrFail($id); + } + /** * 해당 컨텐츠가 신고된 횟수를 반환 * @param string $claimType @@ -84,7 +96,7 @@ public function registerClaimType(AbstractClaimType $claimType) */ public function count(string $claimType, string $targetId) { - return ClaimLog::where('target_id', $targetId) + return $this->repository->where('target_id', $targetId) ->where('claim_type', $claimType) ->count(); } @@ -105,12 +117,13 @@ public function getClaimTypeByKey(string $claimType) /** * 신고 로그 삭제 - * @param int $id id - * @return void + * @param string $id + * @return bool|null + * @throws \Exception */ - public function remove(int $id) + public function removeLog(string $id) { - ClaimLog::find($id)->delete(); + return $this->findLogOrFail($id)->delete(); } /** @@ -118,11 +131,11 @@ public function remove(int $id) * @param string $claimType * @param string $targetId * @param UserInterface $author - * @return void + * @return mixed */ - public function removeByTargetId(string $claimType, string $targetId, UserInterface $author) + public function removeLogByTargetId(string $claimType, string $targetId, UserInterface $author) { - ClaimLog::where('user_id', $author->getKey()) + return $this->repository->where('user_id', $author->getKey()) ->where('target_id', $targetId) ->where('claim_type', $claimType) ->delete(); @@ -148,7 +161,8 @@ public function report( try { $claimType = $this->getClaimTypeByKey($claimType); - $claimType->report($author, $targetId, $shortCut, $message); + $handler = $claimType->getHandler(); + $handler->report($author, $targetId, $shortCut, $message); } catch (ClaimException $e) { XeDB::rollback(); throw $e; @@ -168,28 +182,32 @@ public function report( * 신고 로그 등록 * @param string $claimType * @param string $targetId + * @param string $targetUserId * @param UserInterface $author * @param string $shortCut * @param string $message * @return ClaimLog */ - public function add( + public function addLog( string $claimType, string $targetId, + string $targetUserId, UserInterface $author, string $shortCut, string $message = '' ) { - if ($this->has($claimType, $targetId, $author) === true) { + if ($this->exists($claimType, $targetId, $author) === true) { throw new Exceptions\AlreadyClaimedException; } - return ClaimLog::create([ + return $this->repository->create([ 'claim_type' => $claimType, 'short_cut' => $shortCut, 'target_id' => $targetId, + 'target_user_id' => $targetUserId, 'user_id' => $author->getKey(), 'message' => $message, + 'status' => ClaimLog::STATUS_NEW, 'ipaddress' => $_SERVER['REMOTE_ADDR'], ]); } @@ -201,11 +219,104 @@ public function add( * @param UserInterface $author * @return bool */ - public function has(string $claimType, string $targetId, UserInterface $author) + public function exists(string $claimType, string $targetId, UserInterface $author) { - return ClaimLog::where('user_id', $author->getKey()) + return $this->repository->where('user_id', $author->getKey()) ->where('target_id', $targetId) ->where('claim_type', $claimType) ->exists(); } + + /** + * 신고 로그 수정 + * @param string $id + * @param string $status + * @param string $adminMessage + * @return ClaimLog + */ + public function updateLog(string $id, string $status, string $adminMessage = '') + { + $log = $this->findLogOrFail($id); + + if (!array_key_exists($status, ClaimLog::STATUSES)) { + throw new \UnexpectedValueException('Invalid Claim status'); + } + + $log->status = $status; + $log->admin_message = $adminMessage; + $log->save(); + + return $log; + } + + /** + * 신고 로그 페이지네이션 반환 + * @param array $inputs + * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator + */ + public function paginateClaimLogs(array $inputs = []) + { + $query = $this->repository->query(); + $this->applyFiltersToClaimLogs($query, $inputs); + $logs = $query->paginate(); + + $logs->setCollection($logs->getCollection()->map(function ($log) { + $log->claim_type_text = xe_trans($this->getClaimTypeByKey($log->claim_type)->getText()); + return $log; + })); + + return $logs; + } + + /** + * 신고 로그 검색 필터 적용 + * @param Builder $query + * @param array $inputs + * @return Builder + */ + protected function applyFiltersToClaimLogs(Builder $query, array $inputs = []) + { + $claimType = array_get($inputs, 'claim_type'); + if ($claimType !== null) { + $query->where('claim_type', $claimType); + } + + $claimStatus = array_get($inputs, 'claim_status'); + if ($claimStatus !== null) { + $query->where('status', $claimStatus); + } + + $keyField = array_get($inputs, 'keyfield'); + $keyword = array_get($inputs, 'keyword'); + if ($keyField !== null && $keyword !== null) { + if ($keyField === 'author_name') { + $query->whereHas('user', function ($query) use ($keyword) { + $query->where('display_name', 'like', "%{$keyword}%"); + }); + } + + else if ($keyField === 'target_name') { + $query->whereHas('targetUser', function ($query) use ($keyword) { + $query->where('display_name', 'like', "%{$keyword}%"); + }); + } + + else if ($keyField === 'message') { + $query->where('message', 'like', "%{$keyword}%"); + } + } + + $startDate = array_get($inputs, 'start_date'); + $endDate = array_get($inputs, 'end_date'); + if ($startDate !== null || $endDate !== null) { + $query->whereBetween('created_at', [ + $startDate ? $startDate . ' 00:00:00' : Carbon::minValue(), + $endDate ? $endDate . ' 23:59:59' : Carbon::maxValue() + ]); + } + + $query->orderByDesc('created_at'); + + return $query; + } } diff --git a/src/Handlers/AbstractClaimTypeHandler.php b/src/Handlers/AbstractClaimTypeHandler.php new file mode 100644 index 0000000..d12e083 --- /dev/null +++ b/src/Handlers/AbstractClaimTypeHandler.php @@ -0,0 +1,57 @@ +claimType = $claimType; + } + + /** + * report target + * @return void + */ + public function report( + UserInterface $author, + string $targetId, + string $shortCut, + string $message = '', + string $targetUserId = '' + ) { + app('xe.claim.handler')->addLog( + $this->claimType->getName(), + $targetId, + $targetUserId, + $author, + $shortCut, + $message + ); + } + + /** + * check report conditions + * @param UserInterface $author + * @param T $target + * @return void + */ + public function checkReportConditions(UserInterface $author, $target) + { + // TODO 중복 신고 ON/OFF 기능 추가 후 적용 + /*if (app('xe.claim.handler')->exists($target, $author) === true) { + throw new AlreadyClaimedException(); + }*/ + } +} diff --git a/src/Handlers/BoardClaimTypeHandler.php b/src/Handlers/BoardClaimTypeHandler.php new file mode 100644 index 0000000..aa99174 --- /dev/null +++ b/src/Handlers/BoardClaimTypeHandler.php @@ -0,0 +1,60 @@ +checkReportConditions($author, $target); + + parent::report($author, $targetId, $shortCut, $message, $target->user_id); + } + + /** + * check report conditions + * @param UserInterface $author + * @param $target + * @return void + */ + public function checkReportConditions( + UserInterface $author, + $target + ) { + assert($target instanceof \Xpressengine\Plugins\Board\Models\Board); + + if (($targetUser = $target->getAuthor()) && $targetUser instanceof User) { + if ($targetUser->getKey() === $author->getKey()) { + throw new CantReportMyselfException(); + } + + if ($targetUser->isAdmin()) { + throw new CantReportAdminException(); + } + } + + parent::checkReportConditions($author, $target->getKey()); + } +} diff --git a/src/Handlers/CommentClaimTypeHandler.php b/src/Handlers/CommentClaimTypeHandler.php new file mode 100644 index 0000000..29e70eb --- /dev/null +++ b/src/Handlers/CommentClaimTypeHandler.php @@ -0,0 +1,60 @@ +checkReportConditions($author, $target); + + parent::report($author, $targetId, $shortCut, $message, $target->user_id); + } + + /** + * check report conditions + * @param UserInterface $author + * @param $target + * @return void + */ + public function checkReportConditions( + UserInterface $author, + $target + ) { + assert($target instanceof \Xpressengine\Plugins\Comment\Models\Comment); + + if (($targetUser = $target->getAuthor()) && $targetUser instanceof User) { + if ($targetUser->getKey() === $author->getKey()) { + throw new CantReportMyselfException(); + } + + if ($targetUser->isAdmin()) { + throw new CantReportAdminException(); + } + } + + parent::checkReportConditions($author, $target->id); + } +} diff --git a/src/Handlers/UserClaimTypeHandler.php b/src/Handlers/UserClaimTypeHandler.php new file mode 100644 index 0000000..422131f --- /dev/null +++ b/src/Handlers/UserClaimTypeHandler.php @@ -0,0 +1,64 @@ +checkReportConditions($author, $target); + + parent::report($author, $targetId, $shortCut, $message, $target->getKey()); + } + + /** + * check report conditions + * @param UserInterface $author + * @param $target + * @return void + */ + public function checkReportConditions(UserInterface $author, $target) + { + if (($author instanceof User) === false) { + throw new GuestCannotReportException(); + } + + if ($target->getKey() === $author->getKey()) { + throw new CantReportMyselfException(); + } + + if ($target->isAdmin()) { + throw new CantReportAdminException(); + } + + if ($target->getStatus() !== User::STATUS_ACTIVATED) { + throw new CantReportWithdrawnUserException(); + } + + parent::checkReportConditions($author, $target->getKey()); + } +} diff --git a/src/Migrations/Table/ClaimLogTable.php b/src/Migrations/Table/ClaimLogTable.php index 82e81a1..7a21ebd 100644 --- a/src/Migrations/Table/ClaimLogTable.php +++ b/src/Migrations/Table/ClaimLogTable.php @@ -41,11 +41,13 @@ protected function createTable() $table->string('claim_type', 36); $table->string('short_cut', 255); $table->string('target_id', 36); + $table->string('target_user_id', 36); $table->string('user_id', 36); $table->string('ipaddress', 16); $table->string('message', 255); - $table->timestamp('created_at'); - $table->timestamp('updated_at'); + $table->string('admin_message', 255)->nullable(); + $table->string('status', 255); + $table->nullableTimestamps(); $table->index(['target_id', 'user_id']); $table->index(['target_id', 'claim_type']); diff --git a/src/Models/ClaimLog.php b/src/Models/ClaimLog.php index 586874e..2f42233 100644 --- a/src/Models/ClaimLog.php +++ b/src/Models/ClaimLog.php @@ -43,6 +43,25 @@ class ClaimLog extends DynamicModel { const TABLE_NAME = 'claim_logs'; + /** @var string claim status : new */ + const STATUS_NEW = 'new'; + /** @var string claim status : processing */ + const STATUS_PROCESSING = 'processing'; + /** @var string claim status : closed */ + const STATUS_CLOSED = 'closed'; + + /** @var string[] claim statuses */ + const STATUSES = [ + self::STATUS_NEW => 'claim::statusNew', + self::STATUS_PROCESSING => 'claim::statusProcessing', + self::STATUS_CLOSED => 'claim::statusClosed' + ]; + + /** + * @var bool + */ + public $timestamps = true; + /** * @var string[] */ @@ -50,7 +69,6 @@ class ClaimLog extends DynamicModel /** * user - * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function user() @@ -58,6 +76,15 @@ public function user() return $this->belongsTo(User::class, 'user_id'); } + /** + * target user + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function targetUser() + { + return $this->belongsTo(User::class, 'target_user_id'); + } + /** * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ diff --git a/src/Repositories/ClaimRepository.php b/src/Repositories/ClaimRepository.php new file mode 100644 index 0000000..118497b --- /dev/null +++ b/src/Repositories/ClaimRepository.php @@ -0,0 +1,15 @@ + - * @copyright 2019 Copyright XEHub Corp. - * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL - * @link https://xpressengine.io - */ - -namespace Xpressengine\Plugins\Claim\Skin; - -use Xpressengine\Skin\AbstractSkin; -use View; - -/** - * SettingsSkin - * - * @category Claim - * @package Xpressengine\Plugins\Claim - * @author XE Developers - * @copyright 2019 Copyright XEHub Corp. - * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL - * @link https://xpressengine.io - */ -class SettingsSkin extends AbstractSkin -{ - /** - * @var string - */ - public static $id = 'claim/settingsSkin/claim@default'; - - /** - * render - * - * @return \Illuminate\View\View - */ - public function render() - { - $view = View::make('claim::views.settingsSkin._frame', $this->data); - $content = View::make(sprintf('claim::views.settingsSkin.%s', $this->view), $this->data); - $view->content = $content->render(); - - return $view; - } -} diff --git a/src/ToggleMenus/AbstractClaimItem.php b/src/ToggleMenus/AbstractClaimItem.php index 2284d7c..13da76b 100644 --- a/src/ToggleMenus/AbstractClaimItem.php +++ b/src/ToggleMenus/AbstractClaimItem.php @@ -33,7 +33,7 @@ public function __construct() public function getText() { $text = 'xe::claim'; - if ($this->claimHandler->has($this->componentType, $this->identifier, Auth::user())) { + if ($this->claimHandler->exists($this->componentType, $this->identifier, Auth::user())) { $text = 'xe::cancelClaim'; } @@ -63,7 +63,7 @@ public function getAction() 'claim::enterClaimReason' ]); - if ($this->claimHandler->has($this->componentType, $this->identifier, Auth::user())) { + if ($this->claimHandler->exists($this->componentType, $this->identifier, Auth::user())) { return sprintf( 'ClaimToggleMenu.destroyClaim(event, "%s", "%s", "%s")', route('fixed.claim.destroy'), diff --git a/src/Types/AbstractClaimType.php b/src/Types/AbstractClaimType.php index 7caf2b8..00acc5c 100644 --- a/src/Types/AbstractClaimType.php +++ b/src/Types/AbstractClaimType.php @@ -2,8 +2,7 @@ namespace Xpressengine\Plugins\Claim\Types; -use Xpressengine\User\UserInterface; -use Xpressengine\Plugins\Claim\Handler as ClaimHandler; +use Xpressengine\Plugins\Claim\Handlers\AbstractClaimTypeHandler; /** * Class AbstractClaimType @@ -30,6 +29,11 @@ abstract class AbstractClaimType */ protected $class; + /** + * @var AbstractClaimTypeHandler + */ + protected $handler; + /** * get name * @return string @@ -49,7 +53,7 @@ public function getText() } /** - * get class + * get target class * @return string */ public function getClass() @@ -58,34 +62,11 @@ public function getClass() } /** - * register claim type - * @return void - */ - public function register() - { - - } - - /** - * report target - * @return void - */ - public function report(UserInterface $author, string $targetId, string $shortCut, string $message = '') - { - app('xe.claim.handler')->add($this->name, $targetId, $author, $shortCut, $message); - } - - /** - * check report conditions - * @param UserInterface $author - * @param T $target - * @return void + * get target claim type handler + * @return AbstractClaimTypeHandler */ - public function checkReportConditions(UserInterface $author, $target) + public function getHandler() { - // TODO 중복 신고 ON/OFF 기능 추가 후 적용 - /*if (app('xe.claim.handler')->has($target, $author) === true) { - throw new AlreadyClaimedException(); - }*/ + return $this->handler; } } diff --git a/src/Types/ClaimTypeBoard.php b/src/Types/ClaimTypeBoard.php index 2a5170a..356428a 100644 --- a/src/Types/ClaimTypeBoard.php +++ b/src/Types/ClaimTypeBoard.php @@ -2,11 +2,12 @@ namespace Xpressengine\Plugins\Claim\Types; -use Xpressengine\User\Models\User; -use Xpressengine\User\UserInterface; -use Xpressengine\Plugins\Claim\Exceptions\CantReportAdminException; -use Xpressengine\Plugins\Claim\Exceptions\CantReportMyselfException; +use Xpressengine\Plugins\Claim\Handlers\BoardClaimTypeHandler; +/** + * Class ClaimTypeBoard + * @package Xpressengine\Plugins\Claim\Types + */ class ClaimTypeBoard extends AbstractClaimType { /** @@ -19,7 +20,7 @@ class ClaimTypeBoard extends AbstractClaimType * text of claim type * @var string */ - protected $text = ''; + protected $text = 'claim::claimTypeBoard'; /** * class of claim type @@ -27,46 +28,8 @@ class ClaimTypeBoard extends AbstractClaimType */ protected $class = '\Xpressengine\Plugins\Board\Models\Board'; - /** - * report target - * @return void - */ - public function report( - UserInterface $author, - string $targetId, - string $shortCut, - string $message = '' - ) { - /** @var \Xpressengine\Plugins\Board\Models\Board $target */ - $target = \Xpressengine\Plugins\Board\Models\Board::findOrFail($targetId); - - $this->checkReportConditions($author, $target); - - parent::report($author, $targetId, $shortCut, $message); - } - - /** - * check report conditions - * @param UserInterface $author - * @param $target - * @return void - */ - public function checkReportConditions( - UserInterface $author, - $target - ) { - assert($target instanceof \Xpressengine\Plugins\Board\Models\Board); - - if (($targetUser = $target->getAuthor()) && $targetUser instanceof User) { - if ($targetUser->getKey() === $author->getKey()) { - throw new CantReportMyselfException(); - } - - if ($targetUser->isAdmin()) { - throw new CantReportAdminException(); - } - } - - parent::checkReportConditions($author, $target->getKey()); + public function __construct() + { + $this->handler = app(BoardClaimTypeHandler::class, ['claimType' => $this]); } } diff --git a/src/Types/ClaimTypeComment.php b/src/Types/ClaimTypeComment.php index 84512ef..f13dbb3 100644 --- a/src/Types/ClaimTypeComment.php +++ b/src/Types/ClaimTypeComment.php @@ -2,11 +2,12 @@ namespace Xpressengine\Plugins\Claim\Types; -use Xpressengine\User\Models\User; -use Xpressengine\User\UserInterface; -use Xpressengine\Plugins\Claim\Exceptions\CantReportAdminException; -use Xpressengine\Plugins\Claim\Exceptions\CantReportMyselfException; +use Xpressengine\Plugins\Claim\Handlers\CommentClaimTypeHandler; +/** + * Class ClaimTypeComment + * @package Xpressengine\Plugins\Claim\Types + */ class ClaimTypeComment extends AbstractClaimType { /** @@ -19,7 +20,7 @@ class ClaimTypeComment extends AbstractClaimType * text of claim type * @var string */ - protected $text = ''; + protected $text = 'claim::claimTypeComment'; /** * class of claim type @@ -27,46 +28,8 @@ class ClaimTypeComment extends AbstractClaimType */ protected $class = '\Xpressengine\Plugins\Comment\Models\Comment'; - /** - * report target - * @return void - */ - public function report( - UserInterface $author, - string $targetId, - string $shortCut, - string $message = '' - ) { - /** @var \Xpressengine\Plugins\Comment\Models\Comment $target */ - $target = \Xpressengine\Plugins\Comment\Models\Comment::findOrFail($targetId); - - $this->checkReportConditions($author, $target); - - parent::report($author, $targetId, $shortCut, $message); - } - - /** - * check report conditions - * @param UserInterface $author - * @param $target - * @return void - */ - public function checkReportConditions( - UserInterface $author, - $target - ) { - assert($target instanceof \Xpressengine\Plugins\Comment\Models\Comment); - - if (($targetUser = $target->getAuthor()) && $targetUser instanceof User) { - if ($targetUser->getKey() === $author->getKey()) { - throw new CantReportMyselfException(); - } - - if ($targetUser->isAdmin()) { - throw new CantReportAdminException(); - } - } - - parent::checkReportConditions($author, $target->id); + public function __construct() + { + $this->handler = app(CommentClaimTypeHandler::class, ['claimType' => $this]); } } diff --git a/src/Types/ClaimTypeUser.php b/src/Types/ClaimTypeUser.php index 0c8ff26..eaf428b 100644 --- a/src/Types/ClaimTypeUser.php +++ b/src/Types/ClaimTypeUser.php @@ -2,13 +2,12 @@ namespace Xpressengine\Plugins\Claim\Types; -use Xpressengine\User\Models\User; -use Xpressengine\User\UserInterface; -use Xpressengine\Plugins\Claim\Exceptions\CantReportAdminException; -use Xpressengine\Plugins\Claim\Exceptions\CantReportMyselfException; -use Xpressengine\Plugins\Claim\Exceptions\GuestCannotReportException; -use Xpressengine\Plugins\Claim\Exceptions\CantReportWithdrawnUserException; +use Xpressengine\Plugins\Claim\Handlers\UserClaimTypeHandler; +/** + * Class ClaimTypeUser + * @package Xpressengine\Plugins\Claim\Types + */ class ClaimTypeUser extends AbstractClaimType { /** @@ -21,7 +20,7 @@ class ClaimTypeUser extends AbstractClaimType * text of claim type * @var string */ - protected $text = ''; + protected $text = 'claim::claimTypeUser'; /** * class of claim type @@ -29,55 +28,8 @@ class ClaimTypeUser extends AbstractClaimType */ protected $class = '\Xpressengine\User\Models\User'; - /** - * register claim type - * @return void - */ - public function register() + public function __construct() { - } - - /** - * report target - * @return void - */ - public function report(UserInterface $author, - string $targetId, - string $shortCut, - string $message = '' - ) { - /** @var User $target */ - $target = User::findOrFail($targetId); - - $this->checkReportConditions($author, $target); - - parent::report($author, $targetId, $shortCut, $message); - } - - /** - * check report conditions - * @param UserInterface $author - * @param $target - * @return void - */ - public function checkReportConditions(UserInterface $author, $target) - { - if (($author instanceof User) === false) { - throw new GuestCannotReportException(); - } - - if ($target->getKey() === $author->getKey()) { - throw new CantReportMyselfException(); - } - - if ($target->isAdmin()) { - throw new CantReportAdminException(); - } - - if ($target->getStatus() !== User::STATUS_ACTIVATED) { - throw new CantReportWithdrawnUserException(); - } - - parent::checkReportConditions($author, $target->getKey()); + $this->handler = app(UserClaimTypeHandler::class, ['claimType' => $this]); } } diff --git a/views/settings/edit.blade.php b/views/settings/edit.blade.php new file mode 100644 index 0000000..2148946 --- /dev/null +++ b/views/settings/edit.blade.php @@ -0,0 +1,94 @@ + +@section('page_title') +

{{ xe_trans('xe::claim') }}

+@endsection + +
+
+
+ {{ method_field('put') }} + +
+
+
+
+
+

신고 관리 - No. {{ $log->id }}

+
+
+
+
+
+
+ + @if ($author = $log->user) + + + + @else + + @endif +
+ +
+ + +
+ +
+ + +
+ +
+ + + 바로가기 +
+ +
+ + @if ($targetUser = $log->targetUser) + + + + @else + + @endif +
+ +
+ + +
+
+ +
+
+ + +
+ +
+ + +
+
+
+
+ +
+
+
+
+
+
diff --git a/views/settings/index.blade.php b/views/settings/index.blade.php new file mode 100644 index 0000000..0ef5480 --- /dev/null +++ b/views/settings/index.blade.php @@ -0,0 +1,221 @@ + +{{ app('xe.frontend')->js('assets/core/xe-ui-component/js/xe-page.js')->load() }} +{{ app('xe.frontend')->js('assets/vendor/jqueryui/jquery-ui.min.js')->load() }} +{{ app('xe.frontend')->css('assets/vendor/jqueryui/jquery-ui.min.css')->load() }} + +@section('page_title') +

{{ xe_trans('xe::claim') }}

+@endsection + +
+
+
+
+
+
+

{{ xe_trans('xe::claim') }} {{ xe_trans('xe::management') }}

+
+
+
+
+
+
+
+ +
+ +
+ +
+ +
+
+ + +
+
+ +
+
+ +
+
+ 접수일 + + +
+
+ + +
+ 리셋 +
+ +
+ +
+
+
+
+
+
+
+ + + + + + + + + + + + + + + + + @foreach($paginate as $item) + + + + + + + + + + + {{----}} + + @endforeach + +
No.신고한 사람접수일유형신고 대상신고 사유상태관리삭제
{{ $item->id }} + @if($author = $item->user) + {{ $author->display_name }}
{{ $author->email }}
+ @else + {{ xe_trans('claim::unknownUser') }} + @endif +
{{ $item->created_at->format('Y-m-d') }}{{ $item->claim_type_text }} + @if($targetUser = $item->targetUser) + {{ $targetUser->display_name }}
{{ $targetUser->email }}
+ @else + {{ xe_trans('claim::unknownUser') }} + @endif +
{{ $item->message }}{{ xe_trans(array_get($claimStatuses, $item->status, $item->status)) }} + +
+
+
+ + +
+
+
+
+ +
+ {{ csrf_field() }} +
+ + diff --git a/views/settingsSkin/_frame.blade.php b/views/settingsSkin/_frame.blade.php deleted file mode 100644 index 1cea95a..0000000 --- a/views/settingsSkin/_frame.blade.php +++ /dev/null @@ -1,10 +0,0 @@ -@section('page_title') -

{{xe_trans('claim::claimManage')}}

-@stop - -{{-- include contents blade file --}} -@section('content') -
- {!! isset($content) ? $content : '' !!} -
-@show diff --git a/views/settingsSkin/index.blade.php b/views/settingsSkin/index.blade.php deleted file mode 100644 index 751ebb9..0000000 --- a/views/settingsSkin/index.blade.php +++ /dev/null @@ -1,116 +0,0 @@ -
-
-
-
-
-
-

{{ xe_trans('xe::claim') }} {{ xe_trans('xe::management') }}

-
-
-
-
-
-
- -
-
-
-
- - - - - - - - - - - - @foreach($paginate as $item) - - @if ($item->user != null) - - @else - - @endif - - - - {{----}} - {{----}} - - @endforeach - -
{{ xe_trans('xe::claim') }} {{ xe_trans('xe::user') }}{{ xe_trans('xe::shortcut') }}{{ xe_trans('xe::date') }}{{ xe_trans('xe::ipAddress') }}
[{{ $item->user->getDisplayName() }}][{{xe_trans('claim::unknownUser')}}]{{ $item['claim_type'] }} {{ $item['created_at'] }}{{ $item['ipaddress'] }}
-
-
- - -
-
-
-
- -{{----}} From aa2137576380119846d11e3e30327646be01c212 Mon Sep 17 00:00:00 2001 From: gyorudayo Date: Fri, 17 Mar 2023 18:59:25 +0900 Subject: [PATCH 5/6] =?UTF-8?q?Refactor:=20=EC=8B=A0=EA=B3=A0=20=ED=83=80?= =?UTF-8?q?=EC=9E=85=EC=9D=84=20=ED=8C=A9=ED=86=A0=EB=A6=AC=20=ED=8C=A8?= =?UTF-8?q?=ED=84=B4=20=ED=98=95=EC=8B=9D=EC=9C=BC=EB=A1=9C=20=EA=B4=80?= =?UTF-8?q?=EB=A6=AC=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin.php | 33 ++--- src/{Handler.php => ClaimHandler.php} | 125 +++--------------- src/Controllers/ClaimController.php | 15 ++- src/Controllers/ClaimSettingsController.php | 19 ++- src/Factory/ClaimFactory.php | 79 +++++++++++ src/Factory/Types/AbstractClaimType.php | 105 +++++++++++++++ .../Types/BoardClaim.php} | 27 +++- .../Types/CommentClaim.php} | 27 +++- .../Types/UserClaim.php} | 29 ++-- src/Handlers/AbstractClaimTypeHandler.php | 57 -------- src/ToggleMenus/AbstractClaimItem.php | 19 ++- src/Types/AbstractClaimType.php | 72 ---------- src/Types/ClaimTypeBoard.php | 35 ----- src/Types/ClaimTypeComment.php | 35 ----- src/Types/ClaimTypeUser.php | 35 ----- 15 files changed, 309 insertions(+), 403 deletions(-) rename src/{Handler.php => ClaimHandler.php} (62%) create mode 100644 src/Factory/ClaimFactory.php create mode 100644 src/Factory/Types/AbstractClaimType.php rename src/{Handlers/BoardClaimTypeHandler.php => Factory/Types/BoardClaim.php} (77%) rename src/{Handlers/CommentClaimTypeHandler.php => Factory/Types/CommentClaim.php} (77%) rename src/{Handlers/UserClaimTypeHandler.php => Factory/Types/UserClaim.php} (79%) delete mode 100644 src/Handlers/AbstractClaimTypeHandler.php delete mode 100644 src/Types/AbstractClaimType.php delete mode 100644 src/Types/ClaimTypeBoard.php delete mode 100644 src/Types/ClaimTypeComment.php delete mode 100644 src/Types/ClaimTypeUser.php diff --git a/plugin.php b/plugin.php index 91aad51..c08ff25 100644 --- a/plugin.php +++ b/plugin.php @@ -1,12 +1,11 @@ singleton(Handler::class, function () { - $proxyClass = app('xe.interception')->proxy(Handler::class); - return new $proxyClass( - app(ClaimRepository::class), - app('xe.config'), - $this->defaultClaimTypes - ); + app()->singleton(ClaimHandler::class, function () { + $proxyClass = app('xe.interception')->proxy(ClaimHandler::class); + return new $proxyClass(app(ClaimRepository::class), app('xe.config')); + }); + app()->alias(ClaimHandler::class, 'xe.claim.handler'); + + app()->singleton(ClaimFactory::class, function () { + return new ClaimFactory(app(ClaimHandler::class)); }); - app()->alias(Handler::class, 'xe.claim.handler'); } /** diff --git a/src/Handler.php b/src/ClaimHandler.php similarity index 62% rename from src/Handler.php rename to src/ClaimHandler.php index 0e6a22a..7c973d3 100644 --- a/src/Handler.php +++ b/src/ClaimHandler.php @@ -4,19 +4,14 @@ use App\Facades\XeDB; use Carbon\Carbon; -use Illuminate\Support\Facades\Log; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\ModelNotFoundException; +use Xpressengine\Plugins\Claim\Factory\ClaimFactory; use Xpressengine\User\UserInterface; use Xpressengine\Config\ConfigManager; use Xpressengine\Plugins\Claim\Models\ClaimLog; -use Xpressengine\Plugins\Claim\Types\AbstractClaimType; use Xpressengine\Plugins\Claim\Repositories\ClaimRepository; -use Xpressengine\Plugins\Claim\Exceptions\ClaimException; -use Xpressengine\Plugins\Claim\Exceptions\NotFoundClaimTargetException; -use Xpressengine\Plugins\Claim\Exceptions\NotSupportClaimTypeException; -class Handler +class ClaimHandler { /** @var ClaimRepository */ protected $repository; @@ -26,11 +21,6 @@ class Handler */ protected $configManager; - /** - * @var array - */ - protected $activateClaimTypes = []; - /** * @var string */ @@ -39,43 +29,11 @@ class Handler /** * @param ClaimRepository $repository * @param ConfigManager $configManager - * @param array $defaultClaimTypeClasses */ - public function __construct( - ClaimRepository $repository, - ConfigManager $configManager, - array $defaultClaimTypeClasses - ) { + public function __construct(ClaimRepository $repository, ConfigManager $configManager) + { $this->repository = $repository; $this->configManager = $configManager; - $this->activateClaimTypes = []; - - foreach ($defaultClaimTypeClasses as $class) { - $targetClaimType = app($class); - assert($targetClaimType instanceof Types\AbstractClaimType); - $this->registerClaimType($targetClaimType); - } - } - - /** - * claim type 등록 - * @param AbstractClaimType $claimType - * @return void - */ - public function registerClaimType(AbstractClaimType $claimType) - { - if (!array_key_exists($claimType->getName(), $this->activateClaimTypes) && class_exists($claimType->getClass())) { - $this->activateClaimTypes[$claimType->getName()] = $claimType; - } - } - - /** - * 활성화된 claim types 를 반환 - * @return array|AbstractClaimType[] - */ - public function getActivateClaimTypes() - { - return $this->activateClaimTypes; } /** @@ -83,7 +41,7 @@ public function getActivateClaimTypes() * @param string $id * @return ClaimLog */ - public function findLogOrFail(string $id) + public function findOrFail(string $id) { return $this->repository->findOrFail($id); } @@ -101,29 +59,15 @@ public function count(string $claimType, string $targetId) ->count(); } - /** - * 해당 키의 claim type을 반환 - * @param string $claimType - * @return AbstractClaimType - */ - public function getClaimTypeByKey(string $claimType) - { - if (!array_key_exists($claimType, $this->activateClaimTypes)) { - throw new NotSupportClaimTypeException(); - } - - return $this->activateClaimTypes[$claimType]; - } - /** * 신고 로그 삭제 * @param string $id * @return bool|null * @throws \Exception */ - public function removeLog(string $id) + public function delete(string $id) { - return $this->findLogOrFail($id)->delete(); + return $this->repository->findOrFail($id)->delete(); } /** @@ -133,7 +77,7 @@ public function removeLog(string $id) * @param UserInterface $author * @return mixed */ - public function removeLogByTargetId(string $claimType, string $targetId, UserInterface $author) + public function deleteByTargetId(string $claimType, string $targetId, UserInterface $author) { return $this->repository->where('user_id', $author->getKey()) ->where('target_id', $targetId) @@ -145,50 +89,13 @@ public function removeLogByTargetId(string $claimType, string $targetId, UserInt * 대상을 신고 * @param string $claimType * @param string $targetId + * @param string $targetUserId * @param UserInterface $author * @param string $shortCut * @param string $message * @return void */ public function report( - string $claimType, - string $targetId, - UserInterface $author, - string $shortCut, - string $message = '' - ) { - XeDB::beginTransaction(); - - try { - $claimType = $this->getClaimTypeByKey($claimType); - $handler = $claimType->getHandler(); - $handler->report($author, $targetId, $shortCut, $message); - } catch (ClaimException $e) { - XeDB::rollback(); - throw $e; - } catch (ModelNotFoundException $e) { - XeDB::rollback(); - throw new NotFoundClaimTargetException(); - } catch (\Exception $e) { - XeDB::rollback(); - Log::error($e); - throw new ClaimException(); - } - - XeDB::commit(); - } - - /** - * 신고 로그 등록 - * @param string $claimType - * @param string $targetId - * @param string $targetUserId - * @param UserInterface $author - * @param string $shortCut - * @param string $message - * @return ClaimLog - */ - public function addLog( string $claimType, string $targetId, string $targetUserId, @@ -196,7 +103,7 @@ public function addLog( string $shortCut, string $message = '' ) { - if ($this->exists($claimType, $targetId, $author) === true) { + if ($this->exists($claimType, $author, $targetId) === true) { throw new Exceptions\AlreadyClaimedException; } @@ -215,11 +122,11 @@ public function addLog( /** * 신고 여부 * @param string $claimType - * @param string $targetId * @param UserInterface $author + * @param string $targetId * @return bool */ - public function exists(string $claimType, string $targetId, UserInterface $author) + public function exists(string $claimType, UserInterface $author, string $targetId) { return $this->repository->where('user_id', $author->getKey()) ->where('target_id', $targetId) @@ -236,7 +143,7 @@ public function exists(string $claimType, string $targetId, UserInterface $autho */ public function updateLog(string $id, string $status, string $adminMessage = '') { - $log = $this->findLogOrFail($id); + $log = $this->findOrFail($id); if (!array_key_exists($status, ClaimLog::STATUSES)) { throw new \UnexpectedValueException('Invalid Claim status'); @@ -260,8 +167,10 @@ public function paginateClaimLogs(array $inputs = []) $this->applyFiltersToClaimLogs($query, $inputs); $logs = $query->paginate(); - $logs->setCollection($logs->getCollection()->map(function ($log) { - $log->claim_type_text = xe_trans($this->getClaimTypeByKey($log->claim_type)->getText()); + /** @var ClaimFactory $claimFactory */ + $claimFactory = app(ClaimFactory::class); + $logs->setCollection($logs->getCollection()->map(function ($log) use ($claimFactory) { + $log->claim_type_text = xe_trans($claimFactory->make($log->claim_type)->getText()); return $log; })); diff --git a/src/Controllers/ClaimController.php b/src/Controllers/ClaimController.php index b46a801..21a2a4a 100644 --- a/src/Controllers/ClaimController.php +++ b/src/Controllers/ClaimController.php @@ -19,7 +19,8 @@ use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Facades\XePresenter; -use Xpressengine\Plugins\Claim\Handler; +use Xpressengine\Plugins\Claim\ClaimHandler; +use Xpressengine\Plugins\Claim\Factory\ClaimFactory; use Xpressengine\Support\Exceptions\LoginRequiredHttpException; /** @@ -35,7 +36,7 @@ class ClaimController extends Controller { /** - * @var Handler + * @var ClaimHandler */ protected $handler; @@ -55,7 +56,7 @@ public function index(Request $request) $targetId = $request->get('targetId'); $claimType = $request->get('from'); - $invoked = $this->handler->exists($claimType, $targetId, auth()->user()); + $invoked = $this->handler->exists($claimType, auth()->user(), $targetId); $count = $this->handler->count($claimType, $targetId); return XePresenter::makeApi([ @@ -67,18 +68,20 @@ public function index(Request $request) /** * @return \Xpressengine\Presenter\Presentable */ - public function store(Request $request) + public function store(ClaimFactory $factory, Request $request) { if (auth()->check() === false) { throw new LoginRequiredHttpException; } + $author = auth()->user(); $targetId = $request->get('targetId'); $shortCut = $request->get('shortCut'); $claimType = $request->get('from'); $message = $request->get('message') ?: ''; - $this->handler->report($claimType, $targetId, auth()->user(), $shortCut, $message); + $targetClaimType = $factory->make($claimType); + $targetClaimType->report($author, $targetId, $shortCut, $message); return $this->index($request); } @@ -91,7 +94,7 @@ public function destroy(Request $request) $targetId = $request->get('targetId'); $claimType = $request->get('from'); - $this->handler->removeLogByTargetId($claimType, $targetId, auth()->user()); + $this->handler->deleteByTargetId($claimType, $targetId, auth()->user()); return $this->index($request); } diff --git a/src/Controllers/ClaimSettingsController.php b/src/Controllers/ClaimSettingsController.php index f6ed38d..6e23cb9 100644 --- a/src/Controllers/ClaimSettingsController.php +++ b/src/Controllers/ClaimSettingsController.php @@ -20,7 +20,8 @@ use App\Http\Controllers\Controller; use App\Facades\XeDB; use App\Facades\XePresenter; -use Xpressengine\Plugins\Claim\Handler; +use Xpressengine\Plugins\Claim\ClaimHandler; +use Xpressengine\Plugins\Claim\Factory\ClaimFactory; use Xpressengine\Plugins\Claim\Models\ClaimLog; /** @@ -36,16 +37,22 @@ class ClaimSettingsController extends Controller { /** - * @var Handler + * @var ClaimHandler */ protected $handler; + /** + * @var ClaimFactory + */ + protected $factory; + /** * @return void */ - public function __construct(Handler $handler) + public function __construct(ClaimHandler $handler, ClaimFactory $factory) { $this->handler = $handler; + $this->factory = $factory; } /** @@ -54,7 +61,7 @@ public function __construct(Handler $handler) public function index(Request $request) { $paginate = $this->handler->paginateClaimLogs($request->all()); - $claimTypes = $this->handler->getActivateClaimTypes(); + $claimTypes = $this->factory->getActivateTypes(); $claimStatuses = ClaimLog::STATUSES; return XePresenter::make('claim::views.settings.index', [ @@ -70,7 +77,7 @@ public function index(Request $request) */ public function edit(string $id) { - $log = $this->handler->findLogOrFail($id); + $log = $this->handler->findOrFail($id); $claimStatuses = ClaimLog::STATUSES; $targetClaimTypeText = xe_trans($this->handler->getClaimTypeByKey($log->claim_type)->getText()); @@ -114,7 +121,7 @@ public function update(Request $request, string $id) */ public function delete(string $id) { - $this->handler->removeLog($id); + $this->handler->delete($id); return redirect()->back()->with('alert', ['type' => 'success', 'message' => xe_trans('xe::deleted')]); } diff --git a/src/Factory/ClaimFactory.php b/src/Factory/ClaimFactory.php new file mode 100644 index 0000000..f7a9359 --- /dev/null +++ b/src/Factory/ClaimFactory.php @@ -0,0 +1,79 @@ + + */ + private $activateClaimTypes = []; + + /** + * default claim types + * @var string[] + */ + protected $defaultClaimTypes = [ + Types\UserClaim::class, + Types\BoardClaim::class, + Types\CommentClaim::class + ]; + + public function __construct(ClaimHandler $handler) + { + foreach ($this->defaultClaimTypes as $class) { + $targetClaimType = app($class, ['handler' => $handler]); + assert($targetClaimType instanceof Types\AbstractClaimType); + $this->register($targetClaimType); + } + } + + /** + * @param AbstractClaimType $type + * @return void + */ + public function register(AbstractClaimType $type) + { + if (!array_key_exists($type->getName(), $this->activateClaimTypes) && class_exists($type->getClass())) { + $this->activateClaimTypes[$type->getName()] = $type; + } + } + + /** + * @param string $name + * @return void + */ + public function remove(string $name) + { + if (array_key_exists($name, $this->activateClaimTypes)) { + unset($this->activateClaimTypes[$name]); + } + } + + /** + * @param string $name + * @return AbstractClaimType + */ + public function make(string $name) + { + $targetType = array_get($this->activateClaimTypes, $name); + if ($targetType === null) { + throw new NotSupportClaimTypeException(); + } + + return $targetType; + } + + /** + * @return array + */ + public function getActivateTypes() + { + return $this->activateClaimTypes; + } +} diff --git a/src/Factory/Types/AbstractClaimType.php b/src/Factory/Types/AbstractClaimType.php new file mode 100644 index 0000000..a5d7fca --- /dev/null +++ b/src/Factory/Types/AbstractClaimType.php @@ -0,0 +1,105 @@ +handler = $handler; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @return string + */ + public function getText() + { + return $this->text; + } + + /** + * @return string + */ + public function getClass() + { + return $this->class; + } + + /** + * @param UserInterface $author + * @param string $targetId + * @return bool + */ + public function exists(UserInterface $author, string $targetId) + { + return $this->handler->exists($this->name, $author, $targetId); + } + + /** + * report target + * @return void + */ + public function report( + UserInterface $author, + string $targetId, + string $shortCut, + string $message = '', + string $targetUserId = '' + ) { + $this->handler->report( + $this->name, + $targetId, + $targetUserId, + $author, + $shortCut, + $message + ); + } + + /** + * check report conditions + * @param UserInterface $author + * @param T $target + * @return void + */ + public function checkReportConditions(UserInterface $author, $target) { } +} diff --git a/src/Handlers/BoardClaimTypeHandler.php b/src/Factory/Types/BoardClaim.php similarity index 77% rename from src/Handlers/BoardClaimTypeHandler.php rename to src/Factory/Types/BoardClaim.php index aa99174..0632732 100644 --- a/src/Handlers/BoardClaimTypeHandler.php +++ b/src/Factory/Types/BoardClaim.php @@ -1,19 +1,32 @@ claimType = $claimType; - } - - /** - * report target - * @return void - */ - public function report( - UserInterface $author, - string $targetId, - string $shortCut, - string $message = '', - string $targetUserId = '' - ) { - app('xe.claim.handler')->addLog( - $this->claimType->getName(), - $targetId, - $targetUserId, - $author, - $shortCut, - $message - ); - } - - /** - * check report conditions - * @param UserInterface $author - * @param T $target - * @return void - */ - public function checkReportConditions(UserInterface $author, $target) - { - // TODO 중복 신고 ON/OFF 기능 추가 후 적용 - /*if (app('xe.claim.handler')->exists($target, $author) === true) { - throw new AlreadyClaimedException(); - }*/ - } -} diff --git a/src/ToggleMenus/AbstractClaimItem.php b/src/ToggleMenus/AbstractClaimItem.php index 13da76b..95b84c2 100644 --- a/src/ToggleMenus/AbstractClaimItem.php +++ b/src/ToggleMenus/AbstractClaimItem.php @@ -4,7 +4,7 @@ use Auth; use XeFrontend; -use Xpressengine\Plugins\Claim\Handler; +use Xpressengine\Plugins\Claim\ClaimHandler; use Xpressengine\ToggleMenu\AbstractToggleMenu; /** @@ -14,7 +14,7 @@ */ abstract class AbstractClaimItem extends AbstractToggleMenu { - /** @var Handler */ + /** @var ClaimHandler */ protected $claimHandler; /** @@ -22,7 +22,7 @@ abstract class AbstractClaimItem extends AbstractToggleMenu */ public function __construct() { - $this->claimHandler = app(Handler::class); + $this->claimHandler = app(ClaimHandler::class); } /** @@ -33,7 +33,7 @@ public function __construct() public function getText() { $text = 'xe::claim'; - if ($this->claimHandler->exists($this->componentType, $this->identifier, Auth::user())) { + if ($this->claimHandler->exists($this->componentType, Auth::user(), $this->identifier)) { $text = 'xe::cancelClaim'; } @@ -50,6 +50,15 @@ public function getType() return static::MENUTYPE_EXEC; } + /** + * get target class + * @return string + */ + public function getClass() + { + return $this->class; + } + /** * get toggle menu action * @@ -63,7 +72,7 @@ public function getAction() 'claim::enterClaimReason' ]); - if ($this->claimHandler->exists($this->componentType, $this->identifier, Auth::user())) { + if ($this->claimHandler->exists($this->componentType, Auth::user(), $this->identifier)) { return sprintf( 'ClaimToggleMenu.destroyClaim(event, "%s", "%s", "%s")', route('fixed.claim.destroy'), diff --git a/src/Types/AbstractClaimType.php b/src/Types/AbstractClaimType.php deleted file mode 100644 index 00acc5c..0000000 --- a/src/Types/AbstractClaimType.php +++ /dev/null @@ -1,72 +0,0 @@ -name; - } - - /** - * get text - * @return string - */ - public function getText() - { - return $this->text; - } - - /** - * get target class - * @return string - */ - public function getClass() - { - return $this->class; - } - - /** - * get target claim type handler - * @return AbstractClaimTypeHandler - */ - public function getHandler() - { - return $this->handler; - } -} diff --git a/src/Types/ClaimTypeBoard.php b/src/Types/ClaimTypeBoard.php deleted file mode 100644 index 356428a..0000000 --- a/src/Types/ClaimTypeBoard.php +++ /dev/null @@ -1,35 +0,0 @@ -handler = app(BoardClaimTypeHandler::class, ['claimType' => $this]); - } -} diff --git a/src/Types/ClaimTypeComment.php b/src/Types/ClaimTypeComment.php deleted file mode 100644 index f13dbb3..0000000 --- a/src/Types/ClaimTypeComment.php +++ /dev/null @@ -1,35 +0,0 @@ -handler = app(CommentClaimTypeHandler::class, ['claimType' => $this]); - } -} diff --git a/src/Types/ClaimTypeUser.php b/src/Types/ClaimTypeUser.php deleted file mode 100644 index eaf428b..0000000 --- a/src/Types/ClaimTypeUser.php +++ /dev/null @@ -1,35 +0,0 @@ -handler = app(UserClaimTypeHandler::class, ['claimType' => $this]); - } -} From a64d62239e52871a6f3b07c70463a0b9952a4c21 Mon Sep 17 00:00:00 2001 From: Gyorudayo Date: Thu, 29 Jun 2023 12:30:33 +0900 Subject: [PATCH 6/6] update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0da0678..2549cd3 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,9 @@ $ php artisan plugin:install claim - `claim` 디렉토리 이동 후 `composer dump` 명령 실행 # Usage -XE3에서 제공하는 게시판, 댓글에서 특별한 설정 없이 신고 기능을 제공합니다. +XE3에서 제공하는 유저, 게시판, 댓글에서 특별한 설정 없이 신고 기능을 제공합니다. -> 게시물 내용, 댓글의 토글 버튼을 클릭 > `신고`버튼을 통해서 신고합니다. +> 유저 프로필, 게시물 내용, 댓글의 토글 버튼을 클릭 > `신고`버튼을 통해서 신고합니다. > > `관리자 > 컨텐츠 > 신고` 메뉴를 통해서 신고 내역을 확인할 수 있습니다.