Allow overriding the envelope sender in Email::getJobs()
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / user / UserAction.class.php
CommitLineData
11ade432
AE
1<?php
2namespace wcf\data\user;
0dd6ea0c 3use wcf\data\user\avatar\UserAvatarAction;
11ade432 4use wcf\data\user\group\UserGroup;
931f6597 5use wcf\data\AbstractDatabaseObjectAction;
7918ddba 6use wcf\data\IClipboardAction;
a427a8c8 7use wcf\data\ISearchAction;
7f379ade 8use wcf\system\clipboard\ClipboardHandler;
97247661 9use wcf\system\comment\CommentHandler;
11ade432 10use wcf\system\database\util\PreparedStatementConditionBuilder;
781fe402 11use wcf\system\event\EventHandler;
a79cfb56 12use wcf\system\exception\PermissionDeniedException;
3631f7bd 13use wcf\system\exception\UserInputException;
11dccf1c 14use wcf\system\mail\Mail;
bae8dd1e 15use wcf\system\request\RequestHandler;
2bc9f31d 16use wcf\system\WCF;
2fe45e04 17use wcf\util\UserRegistrationUtil;
11ade432
AE
18
19/**
20 * Executes user-related actions.
21 *
22 * @author Alexander Ebert
7d739af0 23 * @copyright 2001-2016 WoltLab GmbH
11ade432
AE
24 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
25 * @package com.woltlab.wcf
26 * @subpackage data.user
9f959ced 27 * @category Community Framework
0e8867ac
MS
28 *
29 * @method UserEditor[] getObjects()
30 * @method UserEditor getSingleObject()
11ade432 31 */
7918ddba 32class UserAction extends AbstractDatabaseObjectAction implements IClipboardAction, ISearchAction {
11ade432 33 /**
b35f63d6 34 * @inheritDoc
11ade432 35 */
b35f63d6 36 public $className = UserEditor::class;
11ade432 37
8eb8876b 38 /**
b35f63d6 39 * @inheritDoc
8eb8876b 40 */
b35f63d6 41 protected $allowGuestAccess = ['getSearchResultList'];
8eb8876b 42
11ade432 43 /**
b35f63d6 44 * @inheritDoc
11ade432 45 */
b35f63d6 46 protected $permissionsCreate = ['admin.user.canAddUser'];
11ade432
AE
47
48 /**
b35f63d6 49 * @inheritDoc
11ade432 50 */
b35f63d6 51 protected $permissionsDelete = ['admin.user.canDeleteUser'];
11ade432
AE
52
53 /**
b35f63d6 54 * @inheritDoc
11ade432 55 */
b35f63d6 56 protected $permissionsUpdate = ['admin.user.canEditUser'];
11ade432 57
bae8dd1e 58 /**
b35f63d6 59 * @inheritDoc
bae8dd1e 60 */
b35f63d6 61 protected $requireACP = ['create', 'delete', 'disable', 'enable'];
bae8dd1e 62
11ade432
AE
63 /**
64 * Validates permissions and parameters.
65 */
66 public function validateCreate() {
a54f8d8f 67 $this->readString('password', false, 'data');
11ade432
AE
68 }
69
70 /**
11cf19be
MW
71 * Validates accessible groups.
72 *
73 * @param boolean $ignoreOwnUser
2b770bdd
MS
74 * @throws PermissionDeniedException
75 * @throws UserInputException
11ade432 76 */
11cf19be
MW
77 protected function __validateAccessibleGroups($ignoreOwnUser = true) {
78 if ($ignoreOwnUser) {
79 if (in_array(WCF::getUser()->userID, $this->objectIDs)) {
80 unset($this->objectIDs[array_search(WCF::getUser()->userID, $this->objectIDs)]);
81 if (isset($this->objects[WCF::getUser()->userID])) {
82 unset($this->objects[WCF::getUser()->userID]);
83 }
a7fd745e 84 }
48f9369a 85 }
11ade432 86
a7fd745e 87 // list might be empty because only our own user id was given
11cf19be 88 if (empty($this->objectIDs)) {
3631f7bd 89 throw new UserInputException('objectIDs');
a7fd745e
AE
90 }
91
11ade432
AE
92 // validate groups
93 $conditions = new PreparedStatementConditionBuilder();
b35f63d6 94 $conditions->add("userID IN (?)", [$this->objectIDs]);
11ade432
AE
95
96 $sql = "SELECT DISTINCT groupID
97 FROM wcf".WCF_N."_user_to_group
98 ".$conditions;
99 $statement = WCF::getDB()->prepareStatement($sql);
100 $statement->execute($conditions->getParameters());
cd975610 101 $groupIDs = $statement->fetchAll(\PDO::FETCH_COLUMN);
11ade432
AE
102
103 if (!UserGroup::isAccessibleGroup($groupIDs)) {
3631f7bd 104 throw new PermissionDeniedException();
11ade432
AE
105 }
106 }
107
11cf19be
MW
108 /**
109 * Validates permissions and parameters.
110 */
111 public function validateDelete() {
112 // read and validate user objects
113 parent::validateDelete();
114
115 $this->__validateAccessibleGroups();
116 }
117
0dd6ea0c 118 /**
b35f63d6 119 * @inheritDoc
0dd6ea0c
MW
120 */
121 public function delete() {
122 if (empty($this->objects)) {
123 $this->readObjects();
124 }
125
126 // delete avatars
b35f63d6 127 $avatarIDs = [];
4a130a51 128 foreach ($this->getObjects() as $user) {
0dd6ea0c
MW
129 if ($user->avatarID) $avatarIDs[] = $user->avatarID;
130 }
131 if (!empty($avatarIDs)) {
132 $action = new UserAvatarAction($avatarIDs, 'delete');
133 $action->executeAction();
134 }
135
136 // delete profile comments
137 if (!empty($this->objectIDs)) {
97247661 138 CommentHandler::getInstance()->deleteObjects('com.woltlab.wcf.user.profileComment', $this->objectIDs);
0dd6ea0c
MW
139 }
140
141 $returnValue = parent::delete();
142
143 return $returnValue;
144 }
145
11ade432
AE
146 /**
147 * Validates permissions and parameters.
11ade432
AE
148 */
149 public function validateUpdate() {
a79cfb56 150 // read objects
15fa2802 151 if (empty($this->objects)) {
a79cfb56 152 $this->readObjects();
15fa2802
MS
153
154 if (empty($this->objects)) {
3631f7bd 155 throw new UserInputException('objectIDs');
15fa2802 156 }
a79cfb56 157 }
11ade432 158
bae8dd1e
AE
159 // disallow updating of anything except for options outside of ACP
160 if (RequestHandler::getInstance()->isACPRequest() && (count($this->parameters) != 1 || !isset($this->parameters['options']))) {
161 throw new PermissionDeniedException();
162 }
163
a79cfb56
AE
164 try {
165 WCF::getSession()->checkPermissions($this->permissionsUpdate);
166 }
167 catch (PermissionDeniedException $e) {
168 // check if we're editing ourselves
169 if (count($this->objects) == 1 && ($this->objects[0]->userID == WCF::getUser()->userID)) {
67ca3261
AE
170 $count = count($this->parameters);
171 if ($count > 1 || ($count == 1 && !isset($this->parameters['options']))) {
3631f7bd 172 throw new PermissionDeniedException();
a79cfb56
AE
173 }
174 }
175
3631f7bd 176 throw new PermissionDeniedException();
a79cfb56 177 }
11ade432
AE
178 }
179
11cf19be
MW
180 /**
181 * Validates the ban action.
182 */
183 public function validateBan() {
f034d0ec 184 $this->validateUnban();
11cf19be 185
f034d0ec
MS
186 $this->readString('banReason', true);
187 $this->readString('banExpires', true);
11cf19be
MW
188 }
189
190 /**
191 * Validates the unban action.
192 */
193 public function validateUnban() {
b35f63d6 194 WCF::getSession()->checkPermissions(['admin.user.canBanUser']);
f034d0ec
MS
195
196 $this->__validateAccessibleGroups();
11cf19be
MW
197 }
198
199 /**
200 * Bans users.
201 */
202 public function ban() {
f034d0ec
MS
203 $banExpires = $this->parameters['banExpires'];
204 if ($banExpires) {
205 $banExpires = strtotime($banExpires);
206 }
207 else {
208 $banExpires = 0;
209 }
210
11cf19be 211 $conditionBuilder = new PreparedStatementConditionBuilder();
b35f63d6 212 $conditionBuilder->add('userID IN (?)', [$this->objectIDs]);
f034d0ec 213
11cf19be
MW
214 $sql = "UPDATE wcf".WCF_N."_user
215 SET banned = ?,
f034d0ec
MS
216 banReason = ?,
217 banExpires = ?
11cf19be
MW
218 ".$conditionBuilder;
219 $statement = WCF::getDB()->prepareStatement($sql);
220 $statement->execute(
b35f63d6 221 array_merge([
f034d0ec
MS
222 1,
223 $this->parameters['banReason'],
224 $banExpires
b35f63d6 225 ], $conditionBuilder->getParameters())
11cf19be 226 );
bbef7ed8
MW
227
228 $this->unmarkItems();
11cf19be
MW
229 }
230
231 /**
232 * Unbans users.
233 */
234 public function unban() {
235 $conditionBuilder = new PreparedStatementConditionBuilder();
b35f63d6 236 $conditionBuilder->add('userID IN (?)', [$this->objectIDs]);
f034d0ec 237
11cf19be 238 $sql = "UPDATE wcf".WCF_N."_user
f034d0ec
MS
239 SET banned = ?,
240 banExpires = ?
11cf19be
MW
241 ".$conditionBuilder;
242 $statement = WCF::getDB()->prepareStatement($sql);
f034d0ec 243 $statement->execute(
b35f63d6 244 array_merge([
f034d0ec
MS
245 0,
246 0
b35f63d6 247 ], $conditionBuilder->getParameters())
f034d0ec 248 );
11cf19be
MW
249 }
250
11ade432 251 /**
0e8867ac 252 * @inheritDoc
11ade432
AE
253 * @return User
254 */
255 public function create() {
85298945
AE
256 if (!isset($this->parameters['data']['socialNetworkPrivacySettings'])) {
257 $this->parameters['data']['socialNetworkPrivacySettings'] = '';
258 }
259
0e8867ac 260 /** @var User $user */
11ade432
AE
261 $user = parent::create();
262 $userEditor = new UserEditor($user);
263
264 // updates user options
265 if (isset($this->parameters['options'])) {
266 $userEditor->updateUserOptions($this->parameters['options']);
267 }
268
269 // insert user groups
2bb10466 270 $addDefaultGroups = (isset($this->parameters['addDefaultGroups'])) ? $this->parameters['addDefaultGroups'] : true;
b35f63d6 271 $groupIDs = (isset($this->parameters['groups'])) ? $this->parameters['groups'] : [];
2bb10466 272 $userEditor->addToGroups($groupIDs, false, $addDefaultGroups);
11ade432
AE
273
274 // insert visible languages
7623b12f
AE
275 if (!isset($this->parameters['languageIDs'])) {
276 // using the 'languages' key is deprecated since WCF 2.1, please use 'languageIDs' instead
b35f63d6 277 $this->parameters['languageIDs'] = (!empty($this->parameters['languages'])) ? $this->parameters['languages'] : [];
7623b12f
AE
278 }
279 $userEditor->addToLanguages($this->parameters['languageIDs'], false);
11ade432 280
320f4a6d
MW
281 if (PACKAGE_ID) {
282 // set default notifications
283 $sql = "INSERT INTO wcf".WCF_N."_user_notification_event_to_user
0ceb9e95
MW
284 (userID, eventID, mailNotificationType)
285 SELECT ?, eventID, presetMailNotificationType
695780d7
MW
286 FROM wcf".WCF_N."_user_notification_event
287 WHERE preset = ?";
320f4a6d 288 $statement = WCF::getDB()->prepareStatement($sql);
b35f63d6 289 $statement->execute([$user->userID, 1]);
c9d91afc
MW
290
291 // update user rank
292 if (MODULE_USER_RANK) {
b35f63d6 293 $action = new UserProfileAction([$userEditor], 'updateUserRank');
c9d91afc
MW
294 $action->executeAction();
295 }
296 // update user online marking
b35f63d6 297 $action = new UserProfileAction([$userEditor], 'updateUserOnlineMarking');
c9d91afc 298 $action->executeAction();
320f4a6d
MW
299 }
300
11ade432
AE
301 return $user;
302 }
835fa8c2
AE
303
304 /**
b35f63d6 305 * @inheritDoc
835fa8c2
AE
306 */
307 public function update() {
de7f211d 308 if (isset($this->parameters['data']) || isset($this->parameters['counters'])) {
881246d6 309 parent::update();
8a3258f5
MS
310
311 if (isset($this->parameters['data']['languageID'])) {
4a130a51 312 foreach ($this->getObjects() as $object) {
8a3258f5
MS
313 if ($object->userID == WCF::getUser()->userID) {
314 if ($this->parameters['data']['languageID'] != WCF::getUser()->languageID) {
315 WCF::setLanguage($this->parameters['data']['languageID']);
316 }
317
318 break;
319 }
320 }
321 }
881246d6
AE
322 }
323 else {
15fa2802 324 if (empty($this->objects)) {
881246d6
AE
325 $this->readObjects();
326 }
327 }
835fa8c2 328
b35f63d6
MS
329 $groupIDs = (isset($this->parameters['groups'])) ? $this->parameters['groups'] : [];
330 $languageIDs = (isset($this->parameters['languageIDs'])) ? $this->parameters['languageIDs'] : [];
331 $removeGroups = (isset($this->parameters['removeGroups'])) ? $this->parameters['removeGroups'] : [];
332 $userOptions = (isset($this->parameters['options'])) ? $this->parameters['options'] : [];
835fa8c2 333
c2000c5d 334 if (!empty($groupIDs)) {
b35f63d6 335 $action = new UserAction($this->objects, 'addToGroups', [
12f80a9d
MW
336 'groups' => $groupIDs,
337 'addDefaultGroups' => false
b35f63d6 338 ]);
c2000c5d
MW
339 $action->executeAction();
340 }
341
cc27b414 342 if (!empty($removeGroups)) {
b35f63d6 343 $action = new UserAction($this->objects, 'removeFromGroups', [
3ffea5e3 344 'groups' => $removeGroups
b35f63d6 345 ]);
cc27b414
JR
346 $action->executeAction();
347 }
348
4a130a51 349 foreach ($this->getObjects() as $userEditor) {
f277d540
AE
350 if (!empty($userOptions)) {
351 $userEditor->updateUserOptions($userOptions);
352 }
44adccf6
AE
353
354 if (!empty($languageIDs)) {
355 $userEditor->addToLanguages($languageIDs);
356 }
835fa8c2 357 }
83f2404b
AE
358
359 // handle user rename
360 if (count($this->objects) == 1 && !empty($this->parameters['data']['username'])) {
361 if ($this->objects[0]->username != $this->parameters['data']['username']) {
362 $userID = $this->objects[0]->userID;
363 $username = $this->parameters['data']['username'];
364
365 WCF::getDB()->beginTransaction();
366
367 // update comments
368 $sql = "UPDATE wcf".WCF_N."_comment
369 SET username = ?
370 WHERE userID = ?";
371 $statement = WCF::getDB()->prepareStatement($sql);
b35f63d6 372 $statement->execute([$username, $userID]);
83f2404b 373
b35f63d6 374 // update comment responses
83f2404b
AE
375 $sql = "UPDATE wcf".WCF_N."_comment_response
376 SET username = ?
377 WHERE userID = ?";
378 $statement = WCF::getDB()->prepareStatement($sql);
b35f63d6 379 $statement->execute([$username, $userID]);
83f2404b 380
b35f63d6
MS
381 // update media
382 $sql = "UPDATE wcf".WCF_N."_media
383 SET username = ?
384 WHERE userID = ?";
385 $statement = WCF::getDB()->prepareStatement($sql);
386 $statement->execute([$username, $userID]);
387
388 // update modification log
83f2404b
AE
389 $sql = "UPDATE wcf".WCF_N."_modification_log
390 SET username = ?
391 WHERE userID = ?";
392 $statement = WCF::getDB()->prepareStatement($sql);
b35f63d6 393 $statement->execute([$username, $userID]);
83f2404b
AE
394
395 WCF::getDB()->commitTransaction();
396
397 // fire event to handle other database tables
398 EventHandler::getInstance()->fireAction($this, 'rename');
399 }
400 }
835fa8c2 401 }
d5cab442 402
fe6d199c 403 /**
cc27b414 404 * Remove users from given groups.
fe6d199c
JR
405 */
406 public function removeFromGroups() {
407 if (empty($this->objects)) {
408 $this->readObjects();
409 }
410
411 $groupIDs = $this->parameters['groups'];
412
4a130a51 413 foreach ($this->getObjects() as $userEditor) {
fe6d199c
JR
414 $userEditor->removeFromGroups($groupIDs);
415 }
416
417 //reread objects
b35f63d6 418 $this->objects = [];
fe6d199c
JR
419 UserEditor::resetCache();
420 $this->readObjects();
421
422 if (MODULE_USER_RANK) {
423 $action = new UserProfileAction($this->objects, 'updateUserRank');
424 $action->executeAction();
425 }
426 if (MODULE_USERS_ONLINE) {
427 $action = new UserProfileAction($this->objects, 'updateUserOnlineMarking');
428 $action->executeAction();
429 }
430 }
431
0dd6ea0c
MW
432 /**
433 * Add users to given groups.
434 */
c2000c5d
MW
435 public function addToGroups() {
436 if (empty($this->objects)) {
437 $this->readObjects();
438 }
439
440 $groupIDs = $this->parameters['groups'];
441 $deleteOldGroups = $addDefaultGroups = true;
442 if (isset($this->parameters['deleteOldGroups'])) $deleteOldGroups = $this->parameters['deleteOldGroups'];
443 if (isset($this->parameters['addDefaultGroups'])) $addDefaultGroups = $this->parameters['addDefaultGroups'];
444
4a130a51 445 foreach ($this->getObjects() as $userEditor) {
c2000c5d
MW
446 $userEditor->addToGroups($groupIDs, $deleteOldGroups, $addDefaultGroups);
447 }
320f4a6d 448
6374f974 449 //reread objects
b35f63d6 450 $this->objects = [];
6374f974
JR
451 UserEditor::resetCache();
452 $this->readObjects();
453
320f4a6d
MW
454 if (MODULE_USER_RANK) {
455 $action = new UserProfileAction($this->objects, 'updateUserRank');
456 $action->executeAction();
457 }
458 if (MODULE_USERS_ONLINE) {
459 $action = new UserProfileAction($this->objects, 'updateUserOnlineMarking');
460 $action->executeAction();
461 }
c2000c5d
MW
462 }
463
a7fd745e 464 /**
b35f63d6 465 * @inheritDoc
a7fd745e 466 */
a427a8c8 467 public function validateGetSearchResultList() {
a54f8d8f
AE
468 $this->readBoolean('includeUserGroups', false, 'data');
469 $this->readString('searchString', false, 'data');
a7fd745e
AE
470
471 if (isset($this->parameters['data']['excludedSearchValues']) && !is_array($this->parameters['data']['excludedSearchValues'])) {
3631f7bd 472 throw new UserInputException('excludedSearchValues');
a7fd745e 473 }
d5cab442
AE
474 }
475
a7fd745e 476 /**
b35f63d6 477 * @inheritDoc
a7fd745e 478 */
a427a8c8 479 public function getSearchResultList() {
d5cab442 480 $searchString = $this->parameters['data']['searchString'];
b35f63d6 481 $excludedSearchValues = [];
c000b08a
MS
482 if (isset($this->parameters['data']['excludedSearchValues'])) {
483 $excludedSearchValues = $this->parameters['data']['excludedSearchValues'];
484 }
b35f63d6 485 $list = [];
9f959ced 486
d5cab442
AE
487 if ($this->parameters['data']['includeUserGroups']) {
488 $accessibleGroups = UserGroup::getAccessibleGroups();
489 foreach ($accessibleGroups as $group) {
18c05238 490 $groupName = $group->getName();
c000b08a 491 if (!in_array($groupName, $excludedSearchValues)) {
838e315b 492 $pos = mb_strripos($groupName, $searchString);
c000b08a 493 if ($pos !== false && $pos == 0) {
b35f63d6 494 $list[] = [
c000b08a
MS
495 'label' => $groupName,
496 'objectID' => $group->groupID,
497 'type' => 'group'
b35f63d6 498 ];
c000b08a 499 }
d5cab442
AE
500 }
501 }
502 }
c000b08a 503
c2d0b2d6
MS
504 // find users
505 $userProfileList = new UserProfileList();
b35f63d6 506 $userProfileList->getConditionBuilder()->add("username LIKE ?", [$searchString.'%']);
15fa2802 507 if (!empty($excludedSearchValues)) {
b35f63d6 508 $userProfileList->getConditionBuilder()->add("username NOT IN (?)", [$excludedSearchValues]);
c000b08a 509 }
c2d0b2d6
MS
510 $userProfileList->sqlLimit = 10;
511 $userProfileList->readObjects();
9f959ced 512
c2d0b2d6 513 foreach ($userProfileList as $userProfile) {
b35f63d6 514 $list[] = [
c2d0b2d6
MS
515 'icon' => $userProfile->getAvatar()->getImageTag(16),
516 'label' => $userProfile->username,
517 'objectID' => $userProfile->userID,
d5cab442 518 'type' => 'user'
b35f63d6 519 ];
d5cab442 520 }
9f959ced 521
d5cab442
AE
522 return $list;
523 }
49c164a8
AE
524
525 /**
b35f63d6 526 * @inheritDoc
49c164a8 527 */
fbb077d4
MS
528 public function validateUnmarkAll() {
529 // does nothing
530 }
49c164a8
AE
531
532 /**
b35f63d6 533 * @inheritDoc
49c164a8
AE
534 */
535 public function unmarkAll() {
536 ClipboardHandler::getInstance()->removeItems(ClipboardHandler::getInstance()->getObjectTypeID('com.woltlab.wcf.user'));
537 }
bbef7ed8
MW
538
539 /**
540 * Unmarks users.
59dc0db6 541 *
b35f63d6 542 * @param integer[] $userIDs
bbef7ed8 543 */
b35f63d6 544 protected function unmarkItems(array $userIDs = []) {
bbef7ed8
MW
545 if (empty($userIDs)) {
546 $userIDs = $this->objectIDs;
547 }
e3369fd2 548
bbef7ed8
MW
549 if (!empty($userIDs)) {
550 ClipboardHandler::getInstance()->unmark($userIDs, ClipboardHandler::getInstance()->getObjectTypeID('com.woltlab.wcf.user'));
551 }
552 }
2fe45e04
MW
553
554 /**
555 * Validates the enable action.
556 */
557 public function validateEnable() {
b35f63d6 558 WCF::getSession()->checkPermissions(['admin.user.canEnableUser']);
9927f711
MS
559
560 $this->__validateAccessibleGroups();
2fe45e04
MW
561 }
562
563 /**
564 * Validates the disable action.
565 */
566 public function validateDisable() {
567 $this->validateEnable();
568 }
569
570 /**
571 * Enables users.
572 */
573 public function enable() {
574 if (empty($this->objects)) $this->readObjects();
9927f711 575
b35f63d6
MS
576 $action = new UserAction($this->objects, 'update', [
577 'data' => [
2fe45e04 578 'activationCode' => 0
b35f63d6
MS
579 ],
580 'removeGroups' => UserGroup::getGroupIDsByType([UserGroup::GUESTS])
581 ]);
2fe45e04 582 $action->executeAction();
b35f63d6
MS
583 $action = new UserAction($this->objects, 'addToGroups', [
584 'groups' => UserGroup::getGroupIDsByType([UserGroup::USERS]),
2818981f 585 'deleteOldGroups' => false,
9927f711 586 'addDefaultGroups' => false
b35f63d6 587 ]);
2fe45e04 588 $action->executeAction();
00ce5cf8 589
11dccf1c 590 // send e-mail notification
723b4553 591 if (empty($this->parameters['skipNotification'])) {
4a130a51 592 foreach ($this->getObjects() as $user) {
b35f63d6 593 $mail = new Mail([$user->username => $user->email], $user->getLanguage()->getDynamicVariable('wcf.acp.user.activation.mail.subject'), $user->getLanguage()->getDynamicVariable('wcf.acp.user.activation.mail', [
11dccf1c 594 'username' => $user->username
b35f63d6 595 ]));
11dccf1c
MW
596 $mail->send();
597 }
598 }
599
00ce5cf8 600 $this->unmarkItems();
2fe45e04
MW
601 }
602
603 /**
604 * Disables users.
605 */
606 public function disable() {
607 if (empty($this->objects)) $this->readObjects();
9927f711 608
b35f63d6
MS
609 $action = new UserAction($this->objects, 'update', [
610 'data' => [
2fe45e04 611 'activationCode' => UserRegistrationUtil::getActivationCode()
b35f63d6 612 ],
3728a6bd 613 'removeGroups' => UserGroup::getGroupIDsByType([UserGroup::USERS])
b35f63d6 614 ]);
2fe45e04 615 $action->executeAction();
b35f63d6
MS
616 $action = new UserAction($this->objects, 'addToGroups', [
617 'groups' => UserGroup::getGroupIDsByType([UserGroup::GUESTS]),
2818981f
MW
618 'deleteOldGroups' => false,
619 'addDefaultGroups' => false
b35f63d6 620 ]);
2fe45e04 621 $action->executeAction();
00ce5cf8
AE
622
623 $this->unmarkItems();
2fe45e04 624 }
2ce24640
MW
625
626 /**
b35f63d6 627 * @inheritDoc
2ce24640
MW
628 */
629 protected function readObjects() {
630 if (empty($this->objectIDs)) {
631 return;
632 }
57f097e8 633
2ce24640 634 // get base class
b35f63d6 635 $baseClass = call_user_func([$this->className, 'getBaseClass']);
57f097e8 636
2ce24640
MW
637 // get objects
638 $sql = "SELECT user_option_value.*, user_table.*
639 FROM wcf".WCF_N."_user user_table
640 LEFT JOIN wcf".WCF_N."_user_option_value user_option_value
641 ON (user_option_value.userID = user_table.userID)
642 WHERE user_table.userID IN (".str_repeat('?,', count($this->objectIDs) - 1)."?)";
643 $statement = WCF::getDB()->prepareStatement($sql);
644 $statement->execute($this->objectIDs);
645 while ($object = $statement->fetchObject($baseClass)) {
646 $this->objects[] = new $this->className($object);
647 }
648 }
57f097e8
MS
649
650 /**
651 * Validates the 'disableSignature' action.
652 */
653 public function validateDisableSignature() {
3696fe93 654 $this->validateEnableSignature();
57f097e8
MS
655
656 $this->readString('disableSignatureReason', true);
f034d0ec 657 $this->readString('disableSignatureExpires', true);
57f097e8
MS
658 }
659
660 /**
661 * Disables the signature of the handled users.
662 */
663 public function disableSignature() {
664 if (empty($this->objects)) {
665 $this->readObjects();
666 }
667
f034d0ec
MS
668 $disableSignatureExpires = $this->parameters['disableSignatureExpires'];
669 if ($disableSignatureExpires) {
670 $disableSignatureExpires = strtotime($disableSignatureExpires);
671 }
672 else {
673 $disableSignatureExpires = 0;
674 }
675
4a130a51 676 foreach ($this->getObjects() as $userEditor) {
b35f63d6 677 $userEditor->update([
57f097e8 678 'disableSignature' => 1,
f034d0ec
MS
679 'disableSignatureReason' => $this->parameters['disableSignatureReason'],
680 'disableSignatureExpires' => $disableSignatureExpires
b35f63d6 681 ]);
57f097e8
MS
682 }
683 }
684
685 /**
686 * Validates the 'enableSignature' action.
687 */
688 public function validateEnableSignature() {
b35f63d6 689 WCF::getSession()->checkPermissions(['admin.user.canDisableSignature']);
57f097e8
MS
690
691 $this->__validateAccessibleGroups();
692
693 if (empty($this->objects)) {
694 $this->readObjects();
695
696 if (empty($this->objects)) {
697 throw new UserInputException('objectIDs');
698 }
699 }
700 }
701
702 /**
703 * Enables the signature of the handled users.
704 */
705 public function enableSignature() {
706 if (empty($this->objects)) {
707 $this->readObjects();
708 }
709
4a130a51 710 foreach ($this->getObjects() as $userEditor) {
b35f63d6 711 $userEditor->update([
57f097e8 712 'disableSignature' => 0
b35f63d6 713 ]);
57f097e8
MS
714 }
715 }
716
717 /**
718 * Validates the 'disableAvatar' action.
719 */
720 public function validateDisableAvatar() {
3696fe93 721 $this->validateEnableAvatar();
57f097e8
MS
722
723 $this->readString('disableAvatarReason', true);
f034d0ec 724 $this->readString('disableAvatarExpires', true);
57f097e8
MS
725 }
726
727 /**
728 * Disables the avatar of the handled users.
729 */
730 public function disableAvatar() {
731 if (empty($this->objects)) {
732 $this->readObjects();
733 }
1a6e8c52 734
f034d0ec
MS
735 $disableAvatarExpires = $this->parameters['disableAvatarExpires'];
736 if ($disableAvatarExpires) {
737 $disableAvatarExpires = strtotime($disableAvatarExpires);
738 }
739 else {
740 $disableAvatarExpires = 0;
741 }
57f097e8 742
4a130a51 743 foreach ($this->getObjects() as $userEditor) {
b35f63d6 744 $userEditor->update([
57f097e8 745 'disableAvatar' => 1,
f034d0ec
MS
746 'disableAvatarReason' => $this->parameters['disableAvatarReason'],
747 'disableAvatarExpires' => $disableAvatarExpires
b35f63d6 748 ]);
57f097e8
MS
749 }
750 }
751
752 /**
753 * Validates the 'enableAvatar' action.
754 */
755 public function validateEnableAvatar() {
b35f63d6 756 WCF::getSession()->checkPermissions(['admin.user.canDisableAvatar']);
57f097e8
MS
757
758 $this->__validateAccessibleGroups();
759
760 if (empty($this->objects)) {
761 $this->readObjects();
762
763 if (empty($this->objects)) {
764 throw new UserInputException('objectIDs');
765 }
766 }
767 }
768
769 /**
770 * Enables the avatar of the handled users.
771 */
772 public function enableAvatar() {
773 if (empty($this->objects)) {
774 $this->readObjects();
775 }
776
4a130a51 777 foreach ($this->getObjects() as $userEditor) {
b35f63d6 778 $userEditor->update([
57f097e8 779 'disableAvatar' => 0
b35f63d6 780 ]);
57f097e8
MS
781 }
782 }
9ed42d00
AE
783
784 /**
785 * Validates parameters to retrieve the social network privacy settings.
786 */
b35f63d6
MS
787 public function validateGetSocialNetworkPrivacySettings() {
788 // does nothing
789 }
9ed42d00
AE
790
791 /**
792 * Returns the social network privacy settings.
793 *
b35f63d6 794 * @return string[]
9ed42d00
AE
795 */
796 public function getSocialNetworkPrivacySettings() {
797 $settings = @unserialize(WCF::getUser()->socialNetworkPrivacySettings);
798 if (!is_array($settings)) {
b35f63d6 799 $settings = [
9ed42d00
AE
800 'facebook' => false,
801 'google' => false,
802 'reddit' => false,
803 'twitter' => false
b35f63d6 804 ];
9ed42d00
AE
805 }
806
b35f63d6 807 WCF::getTPL()->assign([
9ed42d00 808 'settings' => $settings
b35f63d6 809 ]);
9ed42d00 810
b35f63d6 811 return [
9ed42d00 812 'template' => WCF::getTPL()->fetch('shareButtonsPrivacySettings')
b35f63d6 813 ];
9ed42d00
AE
814 }
815
b35f63d6
MS
816 /**
817 * Validates the 'saveSocialNetworkPrivacySettings' action.
818 */
9ed42d00
AE
819 public function validateSaveSocialNetworkPrivacySettings() {
820 $this->readBoolean('facebook', true);
821 $this->readBoolean('google', true);
822 $this->readBoolean('reddit', true);
823 $this->readBoolean('twitter', true);
824 }
825
b35f63d6
MS
826 /**
827 * Saves the social network privacy settings.
828 *
829 * @return boolean[]
830 */
9ed42d00 831 public function saveSocialNetworkPrivacySettings() {
b35f63d6 832 $settings = [
9ed42d00
AE
833 'facebook' => $this->parameters['facebook'],
834 'google' => $this->parameters['google'],
835 'reddit' => $this->parameters['reddit'],
836 'twitter' => $this->parameters['twitter']
b35f63d6 837 ];
9ed42d00
AE
838
839 $userEditor = new UserEditor(WCF::getUser());
b35f63d6 840 $userEditor->update([
9ed42d00 841 'socialNetworkPrivacySettings' => serialize($settings)
b35f63d6 842 ]);
9ed42d00 843
b35f63d6 844 return [
9ed42d00 845 'settings' => $settings
b35f63d6 846 ];
9ed42d00 847 }
11ade432 848}