Skip to content

Commit 70175de

Browse files
committed
Migrate the function to disable/enable user group assignments to a command
1 parent ba863b0 commit 70175de

File tree

7 files changed

+140
-21
lines changed

7 files changed

+140
-21
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace wcf\command\user\group\assignment;
4+
5+
use wcf\data\user\group\assignment\UserGroupAssignment;
6+
use wcf\data\user\group\assignment\UserGroupAssignmentEditor;
7+
use wcf\event\user\group\assignment\UserGroupAssignmentDisabled;
8+
use wcf\system\event\EventHandler;
9+
10+
/**
11+
* Disables a user group assignment.
12+
*
13+
* @author Olaf Braun
14+
* @copyright 2001-2025 WoltLab GmbH
15+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16+
* @since 6.3
17+
*/
18+
final class DisableUserGroupAssignment
19+
{
20+
public function __construct(private readonly UserGroupAssignment $assignment) {}
21+
22+
public function __invoke(): void
23+
{
24+
if ($this->assignment->isDisabled) {
25+
return;
26+
}
27+
28+
(new UserGroupAssignmentEditor($this->assignment))->update([
29+
'isDisabled' => 1,
30+
]);
31+
32+
$event = new UserGroupAssignmentDisabled($this->assignment);
33+
EventHandler::getInstance()->fire($event);
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace wcf\command\user\group\assignment;
4+
5+
use wcf\data\user\group\assignment\UserGroupAssignment;
6+
use wcf\data\user\group\assignment\UserGroupAssignmentEditor;
7+
use wcf\event\user\group\assignment\UserGroupAssignmentEnabled;
8+
use wcf\system\event\EventHandler;
9+
10+
/**
11+
* Enables a user group assignment.
12+
*
13+
* @author Olaf Braun
14+
* @copyright 2001-2025 WoltLab GmbH
15+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16+
* @since 6.3
17+
*/
18+
final class EnableUserGroupAssignment
19+
{
20+
public function __construct(private readonly UserGroupAssignment $assignment) {}
21+
22+
public function __invoke(): void
23+
{
24+
if (!$this->assignment->isDisabled) {
25+
return;
26+
}
27+
28+
(new UserGroupAssignmentEditor($this->assignment))->update([
29+
'isDisabled' => 0,
30+
]);
31+
32+
$event = new UserGroupAssignmentEnabled($this->assignment);
33+
EventHandler::getInstance()->fire($event);
34+
}
35+
}

wcfsetup/install/files/lib/data/user/group/assignment/UserGroupAssignmentAction.class.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace wcf\data\user\group\assignment;
44

5+
use wcf\command\user\group\assignment\DisableUserGroupAssignment;
6+
use wcf\command\user\group\assignment\EnableUserGroupAssignment;
57
use wcf\data\AbstractDatabaseObjectAction;
68
use wcf\data\IToggleAction;
7-
use wcf\data\TDatabaseObjectToggle;
89
use wcf\system\condition\ConditionHandler;
910

1011
/**
@@ -18,8 +19,6 @@
1819
*/
1920
class UserGroupAssignmentAction extends AbstractDatabaseObjectAction implements IToggleAction
2021
{
21-
use TDatabaseObjectToggle;
22-
2322
/**
2423
* @inheritDoc
2524
*/
@@ -47,4 +46,26 @@ public function delete()
4746

4847
return parent::delete();
4948
}
49+
50+
/**
51+
* @deprecated 6.3
52+
*/
53+
public function validateToggle()
54+
{
55+
$this->validateUpdate();
56+
}
57+
58+
/**
59+
* @deprecated 6.3 use the `EnableUserGroupAssignment` or `DisableUserGroupAssignment` commands instead.
60+
*/
61+
public function toggle()
62+
{
63+
foreach ($this->objects as $editor) {
64+
if ($editor->isDisabled) {
65+
(new EnableUserGroupAssignment($editor->getDecoratedObject()))();
66+
} else {
67+
(new DisableUserGroupAssignment($editor->getDecoratedObject()))();
68+
}
69+
}
70+
}
5071
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace wcf\event\user\group\assignment;
4+
5+
use wcf\data\user\group\assignment\UserGroupAssignment;
6+
use wcf\event\IPsr14Event;
7+
8+
/**
9+
* Indicates that a user group assignment has been disabled.
10+
*
11+
* @author Olaf Braun
12+
* @copyright 2001-2025 WoltLab GmbH
13+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14+
* @since 6.3
15+
*/
16+
final class UserGroupAssignmentDisabled implements IPsr14Event
17+
{
18+
public function __construct(public readonly UserGroupAssignment $assignment) {}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace wcf\event\user\group\assignment;
4+
5+
use wcf\data\user\group\assignment\UserGroupAssignment;
6+
use wcf\event\IPsr14Event;
7+
8+
/**
9+
* Indicates that a user group assignment has been enabled.
10+
*
11+
* @author Olaf Braun
12+
* @copyright 2001-2025 WoltLab GmbH
13+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14+
* @since 6.3
15+
*/
16+
final class UserGroupAssignmentEnabled implements IPsr14Event
17+
{
18+
public function __construct(public readonly UserGroupAssignment $assignment) {}
19+
}

wcfsetup/install/files/lib/system/endpoint/controller/core/users/groups/assignment/DisableAssignment.class.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
use Laminas\Diactoros\Response\JsonResponse;
66
use Psr\Http\Message\ResponseInterface;
77
use Psr\Http\Message\ServerRequestInterface;
8+
use wcf\command\user\group\assignment\DisableUserGroupAssignment;
89
use wcf\data\user\group\assignment\UserGroupAssignment;
9-
use wcf\data\user\group\assignment\UserGroupAssignmentAction;
1010
use wcf\http\Helper;
1111
use wcf\system\endpoint\IController;
1212
use wcf\system\endpoint\PostRequest;
13-
use wcf\system\exception\PermissionDeniedException;
1413
use wcf\system\WCF;
1514

1615
/**
@@ -28,19 +27,15 @@ public function __invoke(ServerRequestInterface $request, array $variables): Res
2827
{
2928
$assignment = Helper::fetchObjectFromRequestParameter($variables['id'], UserGroupAssignment::class);
3029

31-
$this->assertAssignmentCanBeDisabled($assignment);
30+
$this->assertAssignmentCanBeDisabled();
3231

33-
(new UserGroupAssignmentAction([$assignment], 'toggle'))->executeAction();
32+
(new DisableUserGroupAssignment($assignment))();
3433

3534
return new JsonResponse([]);
3635
}
3736

38-
private function assertAssignmentCanBeDisabled(UserGroupAssignment $assignment): void
37+
private function assertAssignmentCanBeDisabled(): void
3938
{
4039
WCF::getSession()->checkPermissions(['admin.management.canManageCronjob']);
41-
42-
if ($assignment->isDisabled) {
43-
throw new PermissionDeniedException();
44-
}
4540
}
4641
}

wcfsetup/install/files/lib/system/endpoint/controller/core/users/groups/assignment/EnableAssignment.class.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
use Laminas\Diactoros\Response\JsonResponse;
66
use Psr\Http\Message\ResponseInterface;
77
use Psr\Http\Message\ServerRequestInterface;
8+
use wcf\command\user\group\assignment\EnableUserGroupAssignment;
89
use wcf\data\user\group\assignment\UserGroupAssignment;
9-
use wcf\data\user\group\assignment\UserGroupAssignmentAction;
1010
use wcf\http\Helper;
1111
use wcf\system\endpoint\IController;
1212
use wcf\system\endpoint\PostRequest;
13-
use wcf\system\exception\PermissionDeniedException;
1413
use wcf\system\WCF;
1514

1615
/**
@@ -28,19 +27,15 @@ public function __invoke(ServerRequestInterface $request, array $variables): Res
2827
{
2928
$assignment = Helper::fetchObjectFromRequestParameter($variables['id'], UserGroupAssignment::class);
3029

31-
$this->assertAssignmentCanBeEnabled($assignment);
30+
$this->assertAssignmentCanBeEnabled();
3231

33-
(new UserGroupAssignmentAction([$assignment], 'toggle'))->executeAction();
32+
(new EnableUserGroupAssignment($assignment))();
3433

3534
return new JsonResponse([]);
3635
}
3736

38-
private function assertAssignmentCanBeEnabled(UserGroupAssignment $assignment): void
37+
private function assertAssignmentCanBeEnabled(): void
3938
{
4039
WCF::getSession()->checkPermissions(['admin.management.canManageCronjob']);
41-
42-
if (!$assignment->isDisabled) {
43-
throw new PermissionDeniedException();
44-
}
4540
}
4641
}

0 commit comments

Comments
 (0)