Ejemplo n.º 1
0
 /**
  * If table key (id) is NULL : inserts new rows
  * otherwise updates existing row in the database tables
  *
  * Can be overridden or overloaded by the child classes
  *
  * @param  boolean  $updateNulls  TRUE: null object variables are also updated, FALSE: not.
  * @return boolean                TRUE if successful otherwise FALSE
  *
  * @throws \RuntimeException
  */
 public function store($updateNulls = false)
 {
     global $_CB_framework, $ueConfig;
     $this->id = (int) $this->id;
     $isNew = $this->id == 0;
     $oldUsername = null;
     $oldGids = array();
     $oldBlock = null;
     if (!$isNew) {
         // get actual username to update sessions in case:
         $sql = 'SELECT ' . $this->_db->NameQuote($this->_cmsUserTableUsername) . ', ' . $this->_db->NameQuote('block') . ' FROM ' . $this->_db->NameQuote($this->_cmsUserTable) . ' WHERE ' . $this->_db->NameQuote($this->_cmsUserTableKey) . ' = ' . (int) $this->user_id;
         $this->_db->setQuery($sql);
         $oldEntry = null;
         if ($this->_db->loadObject($oldEntry)) {
             /** @var \JUser $oldEntry */
             $oldUsername = $oldEntry->username;
             $gids = array_values((array) \JFactory::getAcl()->getGroupsByUser($this->id, false));
             foreach ($gids as $k => $v) {
                 $gids[$k] = (string) $v;
             }
             $oldGids = $gids;
             $oldBlock = $oldEntry->block;
         }
     }
     if (!$isNew && $this->confirmed == 0 && $this->cbactivation == '' && $ueConfig['reg_confirmation'] != 0) {
         $this->_setActivationCode();
     }
     // creates CMS and CB objects:
     $this->_mapUsers();
     // remove the previous email set in bindSafely() and needed for checkSafely():
     unset($this->_original_email);
     // stores first into CMS to get id of user if new:
     $this->_cmsUser->groups = $this->gids;
     $result = $this->_cmsUser->save();
     if (!$result) {
         $this->_error = $this->_cmsUser->getError();
         if (class_exists('JText')) {
             $this->_error = \JText::_($this->_error);
         }
     }
     if ($result) {
         // synchronize id and user_id:
         if ($isNew) {
             $this->id = $this->_cmsUser->id;
             $this->_comprofilerUser->id = $this->_cmsUser->id;
             if ($this->confirmed == 0 && $this->cbactivation == '' && $ueConfig['reg_confirmation'] != 0) {
                 $this->_setActivationCode();
             }
         }
         // stores CB user into comprofiler: if new, inserts, otherwise updates:
         if ($this->user_id == 0) {
             $this->user_id = $this->_cmsUser->id;
             $this->_comprofilerUser->user_id = $this->user_id;
             $result = $this->_comprofilerUser->storeNew($updateNulls);
         } else {
             $result = $this->_comprofilerUser->store($updateNulls);
         }
         if (!$result) {
             $this->_error = $this->_comprofilerUser->getError();
         }
     }
     if ($result) {
         // update the ACL:
         $query = 'SELECT m.id AS aro_id, a.group_id FROM #__user_usergroup_map AS a' . "\n INNER JOIN #__usergroups AS m ON m.id= a.group_id" . "\n WHERE a.user_id = " . (int) $this->id;
         $this->_db->setQuery($query);
         $aro_group = null;
         $result = $this->_db->loadObject($aro_group);
         /** @var \StdClass $aro_group */
         if ($result && !$isNew && ($oldUsername != $this->username || !self::_ArraysEquivalent($oldGids, $this->gids) || $oldBlock == 0 && $this->block == 1)) {
             // Update current sessions state if there is a change in gid or in username:
             if ($this->block == 0) {
                 $query = 'UPDATE #__session ' . "\n SET username = "******"\n WHERE userid = " . (int) $this->id;
                 $this->_db->setQuery($query);
                 $result = $this->_db->query();
                 // Clear usergroup and access caches on gid change (fixing bug #5461):
                 if (method_exists('clearAccessRights', $this->_cmsUser)) {
                     // This already calls clearStatics
                     $this->_cmsUser->clearAccessRights();
                 } else {
                     \JAccess::clearStatics();
                 }
                 // This is needed for instant adding of groups to logged-in user (fixing bug #3581):
                 $session = \JFactory::getSession();
                 $jUser = $session->get('user');
                 if ($jUser->id == $this->id) {
                     $session->set('user', new \JUser((int) $this->id));
                 }
             } else {
                 // logout user now that user login has been blocked:
                 if ($_CB_framework->myId() == $this->id) {
                     $_CB_framework->logout();
                 }
                 $this->_db->setQuery("DELETE FROM #__session WHERE userid = " . (int) $this->id);
                 //TBD: check if this is enough for J 1.5
                 $result = $this->_db->query();
             }
         }
         if (!$result) {
             $this->_error = $this->_db->stderr();
             return false;
         }
     }
     return $result;
 }