Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / worker / SendNewPasswordWorker.class.php
1 <?php
2 namespace wcf\system\worker;
3 use wcf\data\user\UserAction;
4 use wcf\data\user\UserEditor;
5 use wcf\data\user\UserList;
6 use wcf\system\clipboard\ClipboardHandler;
7 use wcf\system\exception\SystemException;
8 use wcf\system\mail\Mail;
9 use wcf\system\request\LinkHandler;
10 use wcf\system\WCF;
11 use wcf\util\PasswordUtil;
12
13 /**
14 * Worker implementation for sending new passwords.
15 *
16 * @author Matthias Schmidt
17 * @copyright 2001-2014 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @package com.woltlab.wcf
20 * @subpackage system.worker
21 * @category Community Framework
22 */
23 class SendNewPasswordWorker extends AbstractWorker {
24 /**
25 * @see \wcf\system\worker\AbstractWorker::$limit
26 */
27 protected $limit = 50;
28
29 /**
30 * @see \wcf\system\worker\IWorker::countObjects()
31 */
32 public function countObjects() {
33 $userList = new UserList();
34 $userList->getConditionBuilder()->add('user_table.userID IN (?)', array($this->parameters['userIDs']));
35
36 return $userList->countObjects();
37 }
38
39 /**
40 * @see \wcf\system\worker\IWorker::execute()
41 */
42 public function execute() {
43 $userList = new UserList();
44 $userList->decoratorClassName = 'wcf\data\user\UserEditor';
45 $userList->getConditionBuilder()->add('user_table.userID IN (?)', array($this->parameters['userIDs']));
46 $userList->sqlLimit = $this->limit;
47 $userList->sqlOffset = $this->limit * $this->loopCount;
48 $userList->readObjects();
49
50 foreach ($userList as $userEditor) {
51 $this->sendNewPassword($userEditor);
52 }
53 }
54
55 /**
56 * @see \wcf\system\worker\IWorker::getProceedURL()
57 */
58 public function getProceedURL() {
59 return LinkHandler::getInstance()->getLink('UserList');
60 }
61
62 /**
63 * @see \wcf\system\worker\IWorker::getProgress()
64 */
65 public function getProgress() {
66 $progress = parent::getProgress();
67
68 if ($progress == 100) {
69 // unmark users
70 ClipboardHandler::getInstance()->unmark($this->parameters['userIDs'], ClipboardHandler::getInstance()->getObjectTypeID('com.woltlab.wcf.user'));
71 }
72
73 return $progress;
74 }
75
76 /**
77 * Sends a new password to the given user.
78 *
79 * @param \wcf\data\user\UserEditor $userEditor
80 */
81 protected function sendNewPassword(UserEditor $userEditor) {
82 $newPassword = PasswordUtil::getRandomPassword();
83
84 $userAction = new UserAction(array($userEditor), 'update', array(
85 'data' => array(
86 'password' => $newPassword
87 )
88 ));
89 $userAction->executeAction();
90
91 // send mail
92 $mail = new Mail(array($userEditor->username => $userEditor->email), $userEditor->getLanguage()->getDynamicVariable('wcf.acp.user.sendNewPassword.mail.subject'), $userEditor->getLanguage()->getDynamicVariable('wcf.acp.user.sendNewPassword.mail', array(
93 'password' => $newPassword,
94 'username' => $userEditor->username,
95 )));
96 $mail->send();
97 }
98
99 /**
100 * @see \wcf\system\worker\IWorker::validate()
101 */
102 public function validate() {
103 WCF::getSession()->checkPermissions(array('admin.user.canEditPassword'));
104
105 if (!isset($this->parameters['userIDs']) || !is_array($this->parameters['userIDs']) || empty($this->parameters['userIDs'])) {
106 throw new SystemException("'userIDs' parameter is missing or invalid");
107 }
108 }
109 }