10 defined(
'JPATH_PLATFORM') or die;
27 protected $isRoot = null;
51 public $username = null;
67 public $password = null;
75 public $password_clear =
'';
91 public $sendEmail = null;
99 public $registerDate = null;
107 public $lastvisitDate = null;
115 public $activation = null;
123 public $params = null;
131 public $groups = array();
139 public $guest = null;
147 public $lastResetTime = null;
155 public $resetCount = null;
163 protected $_params = null;
171 protected $_authGroups = null;
179 protected $_authLevels = null;
187 protected $_authActions = null;
195 protected $_errorMsg = null;
201 protected static $instances = array();
210 public function __construct($identifier = 0)
216 if (!empty($identifier))
218 $this->load($identifier);
224 $this->sendEmail = 0;
240 public static function getInstance($identifier = 0)
243 if (!is_numeric($identifier))
265 if (empty(self::$instances[$id]))
267 $user =
new JUser($id);
268 self::$instances[$id] = $user;
271 return self::$instances[$id];
284 public function getParam($key, $default = null)
286 return $this->_params->get($key, $default);
299 public function setParam($key, $value)
301 return $this->_params->set($key, $value);
314 public function defParam($key, $value)
316 return $this->_params->def($key, $value);
330 public function authorise($action, $assetname = null)
333 if ($this->isRoot === null)
335 $this->isRoot =
false;
339 $rootUser = $config->get(
'root_user');
342 if (is_numeric($rootUser) && $this->
id > 0 && $this->
id == $rootUser)
344 $this->isRoot =
true;
346 elseif ($this->username && $this->username == $rootUser)
348 $this->isRoot =
true;
353 $identities = $this->getAuthorisedGroups();
354 array_unshift($identities, $this->
id * -1);
358 $this->isRoot =
true;
365 return $this->isRoot ?
true :
JAccess::check($this->
id, $action, $assetname);
378 public function getAuthorisedCategories($component, $action)
383 $query = $db->getQuery(
true)
384 ->select(
'c.id AS id, a.name AS asset_name')
385 ->from(
'#__categories AS c')
386 ->join(
'INNER',
'#__assets AS a ON c.asset_id = a.id')
387 ->where(
'c.extension = ' . $db->quote($component))
388 ->where(
'c.published = 1');
389 $db->setQuery($query);
390 $allCategories = $db->loadObjectList(
'id');
391 $allowedCategories = array();
393 foreach ($allCategories as $category)
395 if ($this->authorise($action, $category->asset_name))
397 $allowedCategories[] = (int) $category->id;
401 return $allowedCategories;
411 public function getAuthorisedViewLevels()
413 if ($this->_authLevels === null)
415 $this->_authLevels = array();
418 if (empty($this->_authLevels))
423 return $this->_authLevels;
433 public function getAuthorisedGroups()
435 if ($this->_authGroups === null)
437 $this->_authGroups = array();
440 if (empty($this->_authGroups))
445 return $this->_authGroups;
457 public function setLastVisit($timestamp = null)
460 $table = $this->getTable();
461 $table->load($this->
id);
463 return $table->setLastVisit($timestamp);
476 public function getParameters()
481 return $this->_params;
495 public function setParameters($params)
497 $this->_params = $params;
514 public static function getTable($type = null, $prefix =
'JTable')
519 if (!isset($tabletype))
521 $tabletype[
'name'] =
'user';
522 $tabletype[
'prefix'] =
'JTable';
528 $tabletype[
'name'] = $type;
529 $tabletype[
'prefix'] = $prefix;
545 public function bind(&$array)
548 $joomlaPluginEnabled = JPluginHelper::isEnabled(
'user',
'joomla');
550 if ($joomlaPluginEnabled)
552 $userPlugin = JPluginHelper::getPlugin(
'user',
'joomla');
553 $userPluginParams =
new JRegistry($userPlugin->params);
554 JPluginHelper::importPlugin(
'user',
'joomla');
555 $defaultEncryption = PlgUserJoomla::setDefaultEncryption($userPluginParams);
559 $defaultEncryption =
'bcrypt';
563 if (empty($this->
id))
566 if (empty($array[
'password']))
569 $array[
'password2'] = $array[
'password'];
574 if (isset($array[
'password2']) && $array[
'password'] != $array[
'password2'])
584 $array[
'password'] = $crypt;
590 $username = $this->
get(
'username');
592 if (strlen($username) > 150)
594 $username = substr($username, 0, 150);
595 $this->
set(
'username', $username);
600 $password = $this->
get(
'password');
602 if (strlen($password) > 55)
604 $password = substr($password, 0, 55);
605 $this->
set(
'password', $password);
612 if (!empty($array[
'password']))
614 if ($array[
'password'] != $array[
'password2'])
616 $this->setError(
JText::_(
'JLIB_USER_ERROR_PASSWORD_NOT_MATCH'));
625 $array[
'password'] = $crypt .
':' . $salt;
629 $array[
'password'] = $this->password;
633 if (array_key_exists(
'params', $array))
635 $this->_params->loadArray($array[
'params']);
637 if (is_array($array[
'params']))
639 $params = (string) $this->_params;
643 $params = $array[
'params'];
646 $this->params = $params;
650 if (!$this->setProperties($array))
652 $this->setError(
JText::_(
'JLIB_USER_ERROR_BIND_ARRAY'));
658 $this->
id = (int) $this->
id;
674 public function save($updateOnly =
false)
677 $table = $this->getTable();
678 $this->params = (string) $this->_params;
679 $table->bind($this->getProperties());
685 if (!$table->check())
687 $this->setError($table->getError());
699 $isNew = empty($this->
id);
702 if ($isNew && $updateOnly)
708 $oldUser =
new JUser($this->
id);
716 $iAmSuperAdmin = $my->authorise(
'core.admin');
718 $iAmRehashingSuperadmin =
false;
719 if (($my->id == 0 && !$isNew) && $this->id == $oldUser->id && $oldUser->authorise(
'core.admin') && substr($oldUser->password, 0, 4) !=
'$2y$')
721 $iAmRehashingSuperadmin =
true;
725 if ($iAmSuperAdmin !=
true && $iAmRehashingSuperadmin !=
true)
730 foreach ($this->groups as $groupId)
734 throw new RuntimeException(
'User not Super Administrator');
743 throw new RuntimeException(
'User not Super Administrator');
746 if ($this->groups != null)
749 foreach ($this->groups as $groupId)
753 throw new RuntimeException(
'User not Super Administrator');
761 JPluginHelper::importPlugin(
'user');
764 $result = $dispatcher->trigger(
'onUserBeforeSave', array($oldUser->getProperties(), $isNew, $this->getProperties()));
766 if (in_array(
false, $result,
true))
773 $result = $table->store();
776 if (empty($this->
id))
778 $this->
id = $table->get(
'id');
781 if ($my->id == $table->id)
785 $my->setParameters($registry);
789 $dispatcher->trigger(
'onUserAfterSave', array($this->getProperties(), $isNew, $result, $this->getError()));
793 $this->setError($e->getMessage());
808 public function delete()
810 JPluginHelper::importPlugin(
'user');
814 $dispatcher->trigger(
'onUserBeforeDelete', array($this->getProperties()));
817 $table = $this->getTable();
819 if (!$result = $table->delete($this->id))
821 $this->setError($table->getError());
825 $dispatcher->trigger(
'onUserAfterDelete', array($this->getProperties(), $result, $this->getError()));
839 public function load($id)
842 $table = $this->getTable();
845 if (!$table->load($id))
861 $this->_params->loadString($table->params);
864 $this->setProperties($table->getProperties());