c5240cce4c83cea5c9694ecb8ca665585af1d36e
[GitHub/WoltLab/WCF.git] /
1 <?php
2 namespace wcf\system\bulk\processing\user;
3 use wcf\data\user\group\UserGroup;
4 use wcf\data\user\UserEditor;
5 use wcf\data\user\UserList;
6 use wcf\data\DatabaseObject;
7 use wcf\data\DatabaseObjectList;
8 use wcf\system\exception\UserInputException;
9 use wcf\system\user\storage\UserStorageHandler;
10 use wcf\system\WCF;
11 use wcf\util\ArrayUtil;
12
13 /**
14 * Abstract implementation of a user bulk processing action related to selecting
15 * user groups.
16 *
17 * @author Matthias Schmidt
18 * @copyright 2001-2019 WoltLab GmbH
19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
20 * @package WoltLabSuite\Core\System\Bulk\Processing\User
21 * @since 3.0
22 */
23 abstract class AbstractUserGroupsUserBulkProcessingAction extends AbstractUserBulkProcessingAction {
24 /**
25 * list of available user groups
26 * @var UserGroup[]
27 */
28 public $availableUserGroups = [];
29
30 /**
31 * name of the inputs used to store the selected user group ids
32 * @var string
33 */
34 public $inputName = '';
35
36 /**
37 * ids of selected user groups
38 * @var integer[]
39 */
40 public $userGroupIDs = [];
41
42 /**
43 * @inheritDoc
44 */
45 public function __construct(DatabaseObject $object) {
46 parent::__construct($object);
47
48 $this->availableUserGroups = UserGroup::getSortedAccessibleGroups([], [UserGroup::GUESTS, UserGroup::EVERYONE, UserGroup::OWNER, UserGroup::USERS]);
49 }
50
51 /**
52 * @inheritDoc
53 */
54 public function executeAction(DatabaseObjectList $objectList) {
55 if (!($objectList instanceof UserList)) {
56 throw new \InvalidArgumentException("Object list is no instance of '".UserList::class."', instance of '".get_class($objectList)."' given.");
57 }
58
59 $users = $this->getAccessibleUsers($objectList);
60
61 if (!empty($users)) {
62 WCF::getDB()->beginTransaction();
63 foreach ($users as $user) {
64 $user = new UserEditor($user);
65 $this->executeUserAction($user);
66 }
67 WCF::getDB()->commitTransaction();
68
69 UserStorageHandler::getInstance()->reset(array_keys($users), 'groupIDs');
70 }
71 }
72
73 /**
74 * Execute the action for the given user.
75 *
76 * @param UserEditor $user
77 */
78 abstract protected function executeUserAction(UserEditor $user);
79
80 /**
81 * @inheritDoc
82 */
83 public function getHTML() {
84 return WCF::getTPL()->fetch('userGroupListUserBulkProcessing', 'wcf', [
85 'availableUserGroups' => $this->availableUserGroups,
86 'inputName' => $this->inputName,
87 'selectedUserGroupIDs' => $this->userGroupIDs
88 ]);
89 }
90
91 /**
92 * @inheritDoc
93 */
94 public function isAvailable() {
95 return !empty($this->availableUserGroups);
96 }
97
98 /**
99 * @inheritDoc
100 */
101 public function readFormParameters() {
102 if (isset($_POST[$this->inputName])) $this->userGroupIDs = ArrayUtil::toIntegerArray($_POST[$this->inputName]);
103 }
104
105 /**
106 * @inheritDoc
107 */
108 public function reset() {
109 $this->userGroupIDs = [];
110 }
111
112 /**
113 * @inheritDoc
114 */
115 public function validate() {
116 if (empty($this->userGroupIDs)) {
117 throw new UserInputException($this->inputName);
118 }
119
120 foreach ($this->userGroupIDs as $groupID) {
121 if (!isset($this->availableUserGroups[$groupID])) {
122 throw new UserInputException($this->inputName, 'noValidSelection');
123 }
124 }
125 }
126 }