Skip to content

Commit 9a4f8d3

Browse files
committed
Simplify the action for enabling/disabling a user's avatar/signature/cover photo.
1 parent 1bcb2b3 commit 9a4f8d3

File tree

5 files changed

+134
-224
lines changed

5 files changed

+134
-224
lines changed

wcfsetup/install/files/lib/action/UserBanAction.class.php

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@
22

33
namespace wcf\action;
44

5-
use Laminas\Diactoros\Response\JsonResponse;
6-
use Psr\Http\Message\ResponseInterface;
7-
use Psr\Http\Message\ServerRequestInterface;
8-
use Psr\Http\Server\RequestHandlerInterface;
9-
use wcf\data\user\group\UserGroup;
105
use wcf\data\user\UserProfile;
11-
use wcf\http\Helper;
12-
use wcf\system\cache\runtime\UserProfileRuntimeCache;
136
use wcf\system\exception\IllegalLinkException;
147
use wcf\system\exception\PermissionDeniedException;
158
use wcf\system\form\builder\field\BooleanFormField;
@@ -28,73 +21,37 @@
2821
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
2922
* @since 6.3
3023
*/
31-
final class UserBanAction implements RequestHandlerInterface
24+
final class UserBanAction extends UserManagementAction
3225
{
3326
#[\Override]
34-
public function handle(ServerRequestInterface $request): ResponseInterface
27+
protected function performAction(UserProfile $userProfile, array $data): void
3528
{
36-
$parameters = Helper::mapQueryParameters(
37-
$request->getQueryParams(),
38-
<<<'EOT'
39-
array {
40-
id: positive-int
41-
}
42-
EOT
43-
);
44-
45-
$user = UserProfileRuntimeCache::getInstance()->getObject($parameters['id']);
46-
$this->assertUserCanBeBanned($user);
47-
48-
$form = $this->getForm();
49-
50-
if ($request->getMethod() === 'GET') {
51-
return $form->toResponse();
52-
} elseif ($request->getMethod() === 'POST') {
53-
$response = $form->validateRequest($request);
54-
if ($response !== null) {
55-
return $response;
56-
}
57-
58-
$data = $form->getData()['data'];
59-
$reason = $data['reason'];
60-
if ($data['neverExpires']) {
61-
$expires = null;
62-
} else {
63-
$expires = $data['expires'];
64-
}
65-
66-
(new Ban($user->getDecoratedObject(), $reason, $expires))();
67-
68-
return new JsonResponse([]);
29+
$reason = $data['reason'];
30+
if ($data['neverExpires']) {
31+
$expires = null;
6932
} else {
70-
throw new \LogicException('Unreachable');
33+
$expires = $data['expires'];
7134
}
35+
36+
(new Ban($userProfile->getDecoratedObject(), $reason, $expires))();
7237
}
7338

74-
private function assertUserCanBeBanned(?UserProfile $userProfile): void
39+
#[\Override]
40+
protected function assertUserCanBeManaged(?UserProfile $userProfile): void
7541
{
76-
if (!$userProfile) {
77-
throw new IllegalLinkException();
78-
}
79-
80-
if ($userProfile->userID === WCF::getUser()->userID) {
81-
throw new IllegalLinkException();
82-
}
42+
parent::assertUserCanBeManaged($userProfile);
8343

8444
if (!WCF::getSession()->getPermission('admin.user.canBanUser')) {
8545
throw new PermissionDeniedException();
8646
}
8747

88-
if (!UserGroup::isAccessibleGroup($userProfile->getGroupIDs())) {
89-
throw new PermissionDeniedException();
90-
}
91-
9248
if ($userProfile->banned !== 0) {
9349
throw new IllegalLinkException();
9450
}
9551
}
9652

97-
private function getForm(): Psr15DialogForm
53+
#[\Override]
54+
protected function getForm(): Psr15DialogForm
9855
{
9956
$form = new Psr15DialogForm(
10057
static::class,

wcfsetup/install/files/lib/action/UserDisableAvatarAction.class.php

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@
22

33
namespace wcf\action;
44

5-
use Laminas\Diactoros\Response\JsonResponse;
6-
use Psr\Http\Message\ResponseInterface;
7-
use Psr\Http\Message\ServerRequestInterface;
8-
use Psr\Http\Server\RequestHandlerInterface;
9-
use wcf\data\user\group\UserGroup;
105
use wcf\data\user\UserProfile;
11-
use wcf\http\Helper;
12-
use wcf\system\cache\runtime\UserProfileRuntimeCache;
136
use wcf\system\exception\IllegalLinkException;
147
use wcf\system\exception\PermissionDeniedException;
158
use wcf\system\form\builder\field\BooleanFormField;
@@ -28,73 +21,37 @@
2821
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
2922
* @since 6.3
3023
*/
31-
final class UserDisableAvatarAction implements RequestHandlerInterface
24+
final class UserDisableAvatarAction extends UserManagementAction
3225
{
3326
#[\Override]
34-
public function handle(ServerRequestInterface $request): ResponseInterface
27+
protected function performAction(UserProfile $userProfile, array $data): void
3528
{
36-
$parameters = Helper::mapQueryParameters(
37-
$request->getQueryParams(),
38-
<<<'EOT'
39-
array {
40-
id: positive-int
41-
}
42-
EOT
43-
);
44-
45-
$user = UserProfileRuntimeCache::getInstance()->getObject($parameters['id']);
46-
$this->assertAvatarCanBeDisabled($user);
47-
48-
$form = $this->getForm();
49-
50-
if ($request->getMethod() === 'GET') {
51-
return $form->toResponse();
52-
} elseif ($request->getMethod() === 'POST') {
53-
$response = $form->validateRequest($request);
54-
if ($response !== null) {
55-
return $response;
56-
}
57-
58-
$data = $form->getData()['data'];
59-
$reason = $data['reason'];
60-
if ($data['neverExpires']) {
61-
$expires = null;
62-
} else {
63-
$expires = $data['expires'];
64-
}
65-
66-
(new DisableAvatar($user->getDecoratedObject(), $reason, $expires))();
67-
68-
return new JsonResponse([]);
29+
$reason = $data['reason'];
30+
if ($data['neverExpires']) {
31+
$expires = null;
6932
} else {
70-
throw new \LogicException('Unreachable');
33+
$expires = $data['expires'];
7134
}
35+
36+
(new DisableAvatar($userProfile->getDecoratedObject(), $reason, $expires))();
7237
}
7338

74-
private function assertAvatarCanBeDisabled(?UserProfile $userProfile): void
39+
#[\Override]
40+
protected function assertUserCanBeManaged(?UserProfile $userProfile): void
7541
{
76-
if (!$userProfile) {
77-
throw new IllegalLinkException();
78-
}
79-
80-
if ($userProfile->userID === WCF::getUser()->userID) {
81-
throw new IllegalLinkException();
82-
}
42+
parent::assertUserCanBeManaged($userProfile);
8343

8444
if (!WCF::getSession()->getPermission('admin.user.canDisableAvatar')) {
8545
throw new PermissionDeniedException();
8646
}
8747

88-
if (!UserGroup::isAccessibleGroup($userProfile->getGroupIDs())) {
89-
throw new PermissionDeniedException();
90-
}
91-
9248
if ($userProfile->disableAvatar !== 0) {
9349
throw new IllegalLinkException();
9450
}
9551
}
9652

97-
private function getForm(): Psr15DialogForm
53+
#[\Override]
54+
protected function getForm(): Psr15DialogForm
9855
{
9956
$form = new Psr15DialogForm(
10057
static::class,

wcfsetup/install/files/lib/action/UserDisableCoverPhotoAction.class.php

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@
22

33
namespace wcf\action;
44

5-
use Laminas\Diactoros\Response\JsonResponse;
6-
use Psr\Http\Message\ResponseInterface;
7-
use Psr\Http\Message\ServerRequestInterface;
8-
use Psr\Http\Server\RequestHandlerInterface;
9-
use wcf\data\user\group\UserGroup;
105
use wcf\data\user\UserProfile;
11-
use wcf\http\Helper;
12-
use wcf\system\cache\runtime\UserProfileRuntimeCache;
136
use wcf\system\exception\IllegalLinkException;
147
use wcf\system\exception\PermissionDeniedException;
158
use wcf\system\form\builder\field\BooleanFormField;
@@ -28,73 +21,37 @@
2821
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
2922
* @since 6.3
3023
*/
31-
final class UserDisableCoverPhotoAction implements RequestHandlerInterface
24+
final class UserDisableCoverPhotoAction extends UserManagementAction
3225
{
3326
#[\Override]
34-
public function handle(ServerRequestInterface $request): ResponseInterface
27+
protected function performAction(UserProfile $userProfile, array $data): void
3528
{
36-
$parameters = Helper::mapQueryParameters(
37-
$request->getQueryParams(),
38-
<<<'EOT'
39-
array {
40-
id: positive-int
41-
}
42-
EOT
43-
);
44-
45-
$user = UserProfileRuntimeCache::getInstance()->getObject($parameters['id']);
46-
$this->assertCoverPhotoCanBeDisabled($user);
47-
48-
$form = $this->getForm();
49-
50-
if ($request->getMethod() === 'GET') {
51-
return $form->toResponse();
52-
} elseif ($request->getMethod() === 'POST') {
53-
$response = $form->validateRequest($request);
54-
if ($response !== null) {
55-
return $response;
56-
}
57-
58-
$data = $form->getData()['data'];
59-
$reason = $data['reason'];
60-
if ($data['neverExpires']) {
61-
$expires = null;
62-
} else {
63-
$expires = $data['expires'];
64-
}
65-
66-
(new DisableCoverPhoto($user->getDecoratedObject(), $reason, $expires))();
67-
68-
return new JsonResponse([]);
29+
$reason = $data['reason'];
30+
if ($data['neverExpires']) {
31+
$expires = null;
6932
} else {
70-
throw new \LogicException('Unreachable');
33+
$expires = $data['expires'];
7134
}
35+
36+
(new DisableCoverPhoto($userProfile->getDecoratedObject(), $reason, $expires))();
7237
}
7338

74-
private function assertCoverPhotoCanBeDisabled(?UserProfile $userProfile): void
39+
#[\Override]
40+
protected function assertUserCanBeManaged(?UserProfile $userProfile): void
7541
{
76-
if (!$userProfile) {
77-
throw new IllegalLinkException();
78-
}
79-
80-
if ($userProfile->userID === WCF::getUser()->userID) {
81-
throw new IllegalLinkException();
82-
}
42+
parent::assertUserCanBeManaged($userProfile);
8343

8444
if (!WCF::getSession()->getPermission('admin.user.canDisableCoverPhoto')) {
8545
throw new PermissionDeniedException();
8646
}
8747

88-
if (!UserGroup::isAccessibleGroup($userProfile->getGroupIDs())) {
89-
throw new PermissionDeniedException();
90-
}
91-
9248
if ($userProfile->disableCoverPhoto !== 0) {
9349
throw new IllegalLinkException();
9450
}
9551
}
9652

97-
private function getForm(): Psr15DialogForm
53+
#[\Override]
54+
protected function getForm(): Psr15DialogForm
9855
{
9956
$form = new Psr15DialogForm(
10057
static::class,

0 commit comments

Comments
 (0)