Small optimization
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / user / UserEditor.class.php
1 <?php
2 namespace wcf\data\user;
3 use wcf\data\user\group\UserGroup;
4 use wcf\data\DatabaseObjectEditor;
5 use wcf\data\IEditableCachedObject;
6 use wcf\system\clipboard\ClipboardHandler;
7 use wcf\system\language\LanguageFactory;
8 use wcf\system\session\SessionHandler;
9 use wcf\system\WCF;
10 use wcf\util\PasswordUtil;
11 use wcf\util\StringUtil;
12
13 /**
14 * Provides functions to edit users.
15 *
16 * @author Alexander Ebert
17 * @copyright 2001-2012 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @package com.woltlab.wcf
20 * @subpackage data.user
21 * @category Community Framework
22 */
23 class UserEditor extends DatabaseObjectEditor implements IEditableCachedObject {
24 /**
25 * @see wcf\data\DatabaseObjectDecorator::$baseClass
26 */
27 protected static $baseClass = 'wcf\data\user\User';
28
29 /**
30 * @see wcf\data\IEditableObject::create()
31 */
32 public static function create(array $parameters = array()) {
33 // create salt and password hash
34 $parameters['password'] = PasswordUtil::getDoubleSaltedHash($parameters['password']);
35
36 // create accessToken for AbstractAuthedPage
37 $parameters['accessToken'] = StringUtil::getRandomID();
38
39 // handle registration date
40 if (!isset($parameters['registrationDate'])) $parameters['registrationDate'] = TIME_NOW;
41
42 $user = parent::create($parameters);
43
44 // create default values for user options
45 self::createUserOptions($user->userID);
46
47 return $user;
48 }
49
50 /**
51 * @see wcf\data\IEditableObject::deleteAll()
52 */
53 public static function deleteAll(array $objectIDs = array()) {
54 // unmark users
55 ClipboardHandler::getInstance()->unmark($objectIDs, ClipboardHandler::getInstance()->getObjectTypeID('com.woltlab.wcf.user'));
56
57 return parent::deleteAll($objectIDs);
58 }
59
60 /**
61 * @see wcf\data\DatabaseObjectEditor::update()
62 */
63 public function update(array $parameters = array()) {
64 // update salt and create new password hash
65 if (isset($parameters['password']) && $parameters['password'] !== '') {
66 $parameters['password'] = PasswordUtil::getDoubleSaltedHash($parameters['password']);
67 $parameters['accessToken'] = StringUtil::getRandomID();
68
69 // update accessToken
70 $this->accessToken = $parameters['accessToken'];
71 }
72 else {
73 unset($parameters['password'], $parameters['accessToken']);
74 }
75
76 parent::update($parameters);
77 }
78
79 /**
80 * Inserts default options.
81 *
82 * @param integer $userID
83 */
84 protected static function createUserOptions($userID) {
85 $userOptions = array();
86
87 // fetch default values
88 $sql = "SELECT optionID, defaultValue
89 FROM wcf".WCF_N."_user_option";
90 $statement = WCF::getDB()->prepareStatement($sql);
91 $statement->execute();
92 while ($row = $statement->fetchArray()) {
93 if (!empty($row['defaultValue'])) {
94 $userOptions[$row['optionID']] = $row['defaultValue'];
95 }
96 }
97
98 // insert default values
99 $keys = $values = '';
100 $statementParameters = array($userID);
101 foreach ($userOptions as $optionID => $optionValue) {
102 $keys .= ', userOption'.$optionID;
103 $values .= ', ?';
104 $statementParameters[] = $optionValue;
105 }
106
107 $sql = "INSERT INTO wcf".WCF_N."_user_option_value
108 (userID".$keys.")
109 VALUES (?".$values.")";
110 $statement = WCF::getDB()->prepareStatement($sql);
111 $statement->execute($statementParameters);
112 }
113
114 /**
115 * Updates user options.
116 *
117 * @param array $userOptions
118 */
119 public function updateUserOptions(array $userOptions = array()) {
120 $updateSQL = '';
121 $statementParameters = array();
122 foreach ($userOptions as $optionID => $optionValue) {
123 if (!empty($updateSQL)) $updateSQL .= ',';
124
125 $updateSQL .= 'userOption'.$optionID.' = ?';
126 $statementParameters[] = $optionValue;
127 }
128 $statementParameters[] = $this->userID;
129
130 if (!empty($updateSQL)) {
131 $sql = "UPDATE wcf".WCF_N."_user_option_value
132 SET ".$updateSQL."
133 WHERE userID = ?";
134 $statement = WCF::getDB()->prepareStatement($sql);
135 $statement->execute($statementParameters);
136 }
137 }
138
139 /**
140 * Adds a user to the groups he should be in.
141 *
142 * @param array $groups
143 * @param boolean $deleteOldGroups
144 * @param boolean $addDefaultGroups
145 */
146 public function addToGroups(array $groupIDs, $deleteOldGroups = true, $addDefaultGroups = true) {
147 // add default groups
148 if ($addDefaultGroups) {
149 $groupIDs = array_merge($groupIDs, UserGroup::getGroupIDsByType(array(UserGroup::EVERYONE, UserGroup::USERS)));
150 $groupIDs = array_unique($groupIDs);
151 }
152
153 // remove old groups
154 if ($deleteOldGroups) {
155 $sql = "DELETE FROM wcf".WCF_N."_user_to_group
156 WHERE userID = ?";
157 $statement = WCF::getDB()->prepareStatement($sql);
158 $statement->execute(array($this->userID));
159 }
160
161 // insert new groups
162 if (!empty($groupIDs)) {
163 $sql = "INSERT IGNORE INTO wcf".WCF_N."_user_to_group
164 (userID, groupID)
165 VALUES (?, ?)";
166 $statement = WCF::getDB()->prepareStatement($sql);
167 foreach ($groupIDs as $groupID) {
168 $statement->execute(array($this->userID, $groupID));
169 }
170 }
171 }
172
173 /**
174 * Adds a user to a user group.
175 *
176 * @param integer $groupID
177 */
178 public function addToGroup($groupID) {
179 $sql = "INSERT IGNORE INTO wcf".WCF_N."_user_to_group
180 (userID, groupID)
181 VALUES (?, ?)";
182 $statement = WCF::getDB()->prepareStatement($sql);
183 $statement->execute(array($this->userID, $groupID));
184 }
185
186 /**
187 * Removes a user from a user group.
188 *
189 * @param integer $groupID
190 */
191 public function removeFromGroup($groupID) {
192 $sql = "DELETE FROM wcf".WCF_N."_user_to_group
193 WHERE userID = ?
194 AND groupID = ?";
195 $statement = WCF::getDB()->prepareStatement($sql);
196 $statement->execute(array($this->userID, $groupID));
197 }
198
199 /**
200 * Removes a user from multiple user groups.
201 *
202 * @param array $groupIDs
203 */
204 public function removeFromGroups(array $groupIDs) {
205 $sql = "DELETE FROM wcf".WCF_N."_user_to_group
206 WHERE userID = ?
207 AND groupID = ?";
208 $statement = WCF::getDB()->prepareStatement($sql);
209 foreach ($groupIDs as $groupID) {
210 $statement->execute(array(
211 $this->userID,
212 $groupID
213 ));
214 }
215 }
216
217 /**
218 * Saves the visible languages of a user.
219 *
220 * @param array $languageIDs
221 */
222 public function addToLanguages(array $languageIDs) {
223 // remove previous languages
224 $sql = "DELETE FROM wcf".WCF_N."_user_to_language
225 WHERE userID = ?";
226 $statement = WCF::getDB()->prepareStatement($sql);
227 $statement->execute(array($this->userID));
228
229 // insert language ids
230 $sql = "INSERT INTO wcf".WCF_N."_user_to_language
231 (userID, languageID)
232 VALUES (?, ?)";
233 $statement = WCF::getDB()->prepareStatement($sql);
234
235 if (!empty($languageIDs)) {
236 WCF::getDB()->beginTransaction();
237 foreach ($languageIDs as $languageID) {
238 $statement->execute(array(
239 $this->userID,
240 $languageID
241 ));
242 }
243 WCF::getDB()->commitTransaction();
244 }
245 else {
246 // no language id given, use default language id instead
247 $statement->execute(array(
248 $this->userID,
249 LanguageFactory::getInstance()->getDefaultLanguageID()
250 ));
251 }
252 }
253
254 /**
255 * @see wcf\data\IEditableCachedObject::resetCache()
256 */
257 public static function resetCache() {
258 SessionHandler::resetSessions();
259 }
260 }