10 defined(
'JPATH_PLATFORM') or die;
38 parent::__construct(
'#__users',
'id', $db);
57 public function load($userId = null, $reset =
true)
79 $query = $this->_db->getQuery(
true)
81 ->from($this->_db->quoteName(
'#__users'))
82 ->where($this->_db->quoteName(
'id') .
' = ' . (int) $userId);
83 $this->_db->setQuery($query);
84 $data = (array) $this->_db->loadAssoc();
95 $return = $this->bind($data);
97 if ($return !==
false)
101 ->select($this->_db->quoteName(
'g.id'))
102 ->select($this->_db->quoteName(
'g.title'))
103 ->from($this->_db->quoteName(
'#__usergroups') .
' AS g')
104 ->join(
'INNER', $this->_db->quoteName(
'#__user_usergroup_map') .
' AS m ON m.group_id = g.id')
105 ->where($this->_db->quoteName(
'm.user_id') .
' = ' . (int) $userId);
106 $this->_db->setQuery($query);
109 $this->groups = $this->_db->loadAssocList(
'id',
'id');
125 public function bind($array, $ignore =
'')
127 if (array_key_exists(
'params', $array) && is_array($array[
'params']))
131 $array[
'params'] = (string) $registry;
135 $return = parent::bind($array, $ignore);
138 if ($return && !empty($this->groups))
144 $query = $this->_db->getQuery(
true)
145 ->select($this->_db->quoteName(
'id'))
146 ->select($this->_db->quoteName(
'title'))
147 ->from($this->_db->quoteName(
'#__usergroups'))
148 ->where($this->_db->quoteName(
'id') .
' = ' . implode(
' OR ' . $this->_db->quoteName(
'id') .
' = ', $this->groups));
149 $this->_db->setQuery($query);
152 $this->groups = $this->_db->loadAssocList(
'id',
'id');
166 public function check()
175 if (trim($this->name) ==
'')
177 $this->setError(
JText::_(
'JLIB_DATABASE_ERROR_PLEASE_ENTER_YOUR_NAME'));
182 if (trim($this->username) ==
'')
184 $this->setError(
JText::_(
'JLIB_DATABASE_ERROR_PLEASE_ENTER_A_USER_NAME'));
189 if (preg_match(
'#[<>"\'%;()&\\s\\\\]|\\.\\./#', $this->username) || strlen(utf8_decode($this->username)) < 2)
191 $this->setError(
JText::sprintf(
'JLIB_DATABASE_ERROR_VALID_AZ09', 2));
198 $this->setError(
JText::_(
'JLIB_DATABASE_ERROR_VALID_MAIL'));
207 if (empty($this->registerDate) || $this->registerDate == $this->_db->getNullDate())
213 if (empty($this->lastvisitDate))
215 $this->lastvisitDate = $this->_db->getNullDate();
219 $query = $this->_db->getQuery(
true)
220 ->select($this->_db->quoteName(
'id'))
221 ->from($this->_db->quoteName(
'#__users'))
222 ->where($this->_db->quoteName(
'username') .
' = ' . $this->_db->quote($this->username))
223 ->where($this->_db->quoteName(
'id') .
' != ' . (int) $this->
id);
224 $this->_db->setQuery($query);
226 $xid = (int) $this->_db->loadResult();
228 if ($xid && $xid != (
int) $this->
id)
230 $this->setError(
JText::_(
'JLIB_DATABASE_ERROR_USERNAME_INUSE'));
237 ->select($this->_db->quoteName(
'id'))
238 ->from($this->_db->quoteName(
'#__users'))
239 ->where($this->_db->quoteName(
'email') .
' = ' . $this->_db->quote($this->email))
240 ->where($this->_db->quoteName(
'id') .
' != ' . (int) $this->
id);
241 $this->_db->setQuery($query);
242 $xid = (int) $this->_db->loadResult();
244 if ($xid && $xid != (
int) $this->
id)
246 $this->setError(
JText::_(
'JLIB_DATABASE_ERROR_EMAIL_INUSE'));
253 $rootUser = $config->get(
'root_user');
255 if (!is_numeric($rootUser))
258 ->select($this->_db->quoteName(
'id'))
259 ->from($this->_db->quoteName(
'#__users'))
260 ->where($this->_db->quoteName(
'username') .
' = ' . $this->_db->quote($rootUser));
261 $this->_db->setQuery($query);
262 $xid = (int) $this->_db->loadResult();
264 if ($rootUser == $this->username && (!$xid || $xid && $xid != (
int) $this->
id)
265 || $xid && $xid == (
int) $this->
id && $rootUser != $this->username)
267 $this->setError(
JText::_(
'JLIB_DATABASE_ERROR_USERNAME_CANNOT_CHANGE'));
290 public function store($updateNulls =
false)
293 $k = $this->_tbl_key;
298 $groups = $this->groups;
299 unset($this->groups);
305 $this->_db->updateObject($this->_tbl, $this, $this->_tbl_key, $updateNulls);
310 $this->_db->insertObject($this->_tbl, $this, $this->_tbl_key);
314 $this->groups = $groups;
318 if (is_array($this->groups) && count($this->groups))
321 $query = $this->_db->getQuery(
true)
322 ->delete($this->_db->quoteName(
'#__user_usergroup_map'))
323 ->where($this->_db->quoteName(
'user_id') .
' = ' . (int) $this->
id);
324 $this->_db->setQuery($query);
325 $this->_db->execute();
329 ->insert($this->_db->quoteName(
'#__user_usergroup_map'))
330 ->columns(array($this->_db->quoteName(
'user_id'), $this->_db->quoteName(
'group_id')));
333 foreach ($this->groups as $group)
335 $query->clear(
'values')
336 ->values($this->
id .
', ' . $group);
337 $this->_db->setQuery($query);
338 $this->_db->execute();
343 if ($this->block == (
int) 1)
346 ->delete($this->_db->quoteName(
'#__user_keys'))
347 ->where($this->_db->quoteName(
'user_id') .
' = ' . $this->_db->quote($this->username));
348 $this->_db->setQuery($query);
349 $this->_db->execute();
364 public function delete($userId = null)
367 $k = $this->_tbl_key;
371 $this->$k = (int) $userId;
375 $query = $this->_db->getQuery(
true)
376 ->delete($this->_db->quoteName($this->_tbl))
377 ->where($this->_db->quoteName($this->_tbl_key) .
' = ' . (int) $this->$k);
378 $this->_db->setQuery($query);
379 $this->_db->execute();
383 ->delete($this->_db->quoteName(
'#__user_usergroup_map'))
384 ->where($this->_db->quoteName(
'user_id') .
' = ' . (int) $this->$k);
385 $this->_db->setQuery($query);
386 $this->_db->execute();
393 ->delete($this->_db->quoteName(
'#__messages_cfg'))
394 ->where($this->_db->quoteName(
'user_id') .
' = ' . (int) $this->$k);
395 $this->_db->setQuery($query);
396 $this->_db->execute();
399 ->delete($this->_db->quoteName(
'#__messages'))
400 ->where($this->_db->quoteName(
'user_id_to') .
' = ' . (int) $this->$k);
401 $this->_db->setQuery($query);
402 $this->_db->execute();
405 ->delete($this->_db->quoteName(
'#__user_keys'))
406 ->where($this->_db->quoteName(
'user_id') .
' = ' . $this->_db->quote($this->username));
407 $this->_db->setQuery($query);
408 $this->_db->execute();
423 public function setLastVisit($timeStamp = null, $userId = null)
426 if (is_null($userId))
434 jexit(
'No userid in setLastVisit');
443 $query = $db->getQuery(
true)
444 ->update($db->quoteName($this->_tbl))
445 ->
set($db->quoteName(
'lastvisitDate') .
'=' . $db->quote($date->toSql()))
446 ->where($db->quoteName(
'id') .
'=' . (int) $userId);
447 $db->setQuery($query);