/** * Create a new Joomla user before we create a new Solidres's customer * * TODO: save new user using joomla user model to be above to run some plugin event for User * * @param $data * @param $table * @param $isNew * @param $response * * @return bool */ public function onCustomerBeforeSave($data, $table, $isNew, &$response) { jimport('solidres.user.user'); // Rebuild user data $userData = array('id' => $data['user_id'], 'name' => $data['firstname'] . ' ' . $data['middlename'] . ' ' . $data['lastname'], 'username' => $data['username'], 'password' => $data['password'], 'password2' => $data['password2'], 'email' => $data['email'], 'groups' => array('2')); $pk = !empty($userData['id']) ? $userData['id'] : 0; $srUser = SRUser::getInstance($pk); if (!$srUser->bind($userData)) { $table->setError($srUser->getError()); return false; } $result = $srUser->save(); if (!$result) { $table->setError($srUser->getError()); return false; } // Assign the recent insert joomla user id $response = $srUser->id; return true; }
/** * Method to save the JUser object to the database * * @param boolean $updateOnly Save the object only if not a new user * Currently only used in the user reset password method. * * @return boolean True on success * * @since 11.1 * @throws exception */ public function save() { // Create the user table object $table = $this->getTable(); $this->params = (string) $this->_params; $table->bind($this->getProperties()); // Allow an exception to be thrown. try { // Check and store the object. if (!$table->check()) { $this->setError($table->getError()); return false; } // If user is made a Super Admin group and user is NOT a Super Admin // // @todo ACL - this needs to be acl checked // $my = JFactory::getUser(); //are we creating a new user $isNew = empty($this->id); // Get the old user $oldUser = new SRUser($this->id); // // Access Checks // // The only mandatory check is that only Super Admins can operate on other Super Admin accounts. // To add additional business rules, use a user plugin and throw an Exception with onUserBeforeSave. // Check if I am a Super Admin $iAmSuperAdmin = $my->authorise('core.admin'); // We are only worried about edits to this account if I am not a Super Admin. if ($iAmSuperAdmin != true) { if ($isNew) { // Check if the new user is being put into a Super Admin group. foreach ($this->groups as $key => $groupId) { if (JAccess::checkGroup($groupId, 'core.admin')) { throw new Exception(JText::_('JLIB_USER_ERROR_NOT_SUPERADMIN')); } } } else { // I am not a Super Admin, and this one is, so fail. if (JAccess::check($this->id, 'core.admin')) { throw new Exception(JText::_('JLIB_USER_ERROR_NOT_SUPERADMIN')); } if ($this->groups != null) { // I am not a Super Admin and I'm trying to make one. foreach ($this->groups as $groupId) { if (JAccess::checkGroup($groupId, 'core.admin')) { throw new Exception(JText::_('JLIB_USER_ERROR_NOT_SUPERADMIN')); } } } } } // Fire the onUserBeforeSave event. JPluginHelper::importPlugin('user'); $dispatcher = JDispatcher::getInstance(); $result = $dispatcher->trigger('onUserBeforeSave', array($oldUser->getProperties(), $isNew, $this->getProperties())); if (in_array(false, $result, true)) { // Plugin will have to raise its own error or throw an exception. return false; } // Store the user data in the database if (!($result = $table->store())) { throw new Exception($table->getError()); } // Set the id for the JUser object in case we created a new user. if (empty($this->id)) { $this->id = $table->get('id'); } if ($my->id == $table->id) { $registry = new JRegistry(); $registry->loadString($table->params); $my->setParameters($registry); } } catch (Exception $e) { $this->setError($e->getMessage()); return false; } return $result; }