1

I am building a SaaS platform for a client that has a Joomla site equipped with JomSocial. We have disabled the PayPlans plugin because the SaaS platform is handling it's own payments and they want the JomSocial community component to be free.

I have build a sign-up process that creates the Joomla and JomSocial user successfully and even enables and activates the Joomla user, but the JomSocial part is not working. I can log in created user accounts, but I get redirected to the noaccess page if I try to access the profile, the feed or any other Joomla page.

The Joomla User Activation is set to SELF, but when I create the user in code the activation email does not get sent. I either need to activate manually or manually send the activation email.

My question is two-fold:1) Is there a way to activate a JomSocial User in PHP? or 2) How can I tell JomSocial to send the activation email to the new user?

1

I forgot to post my solution. Sorry everyone. I found this information in multiple posts on StackOverflow and also after digging into the Joomla! documentation.

// Requirments needed to build this function.
defined('_JEXEC') or die('Restricted access');
require_once ( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once ( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once ( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
include_once JPATH_ROOT . '/components/com_community/libraries/core.php';
include_once JPATH_ROOT . '/components/com_community/libraries/user.php';

/**
 * Creates a new Joomla! user. This method throws errors so you have to
 * wrap the call to this method in a try/catch. It will also set a default
 * $name, $username and $password if none is provided.
 * 
 * @param type $email REQUIRED
 * @param string $name
 * @param type $username
 * @param type $password
 * @return boolean
 * @throws Exception
 */ 
function createUser($email = null, $name = null, $username = null, $password = null) {

  if (is_null($email)) {
    throw new Exception("Email is required to create new Joomla! user.");
  }
  $uniqid = uniqid();
  if (is_null($name)) {
    $name = 'member-' . $uniqid;
  }
  if (is_null($username)) {
    $username = $name;
  }
  if (is_null($password)) {
    $password = $uniqid;
  }

  try {
    // Create new user.
    jimport('joomla.application.component.helper');
    $params = 'com_users';
    $usersParams = JComponentHelper::getParams($params);
    $new_user = JFactory::getUser(0);
    $config = JComponentHelper::getParams('com_users');
    $defaultUserGroup = $config->get('new_usertype', 2);
    $jdata = array(
      "name" => $name,
      "username" => $username,
      "password" => $password,
      "password2" => $password,
      "email" => $email,
      "sendEmail" => 0,
      "groups" => array($defaultUserGroup)
    );

    // Automatically activate user. Comment this out if you use double
    // opt-in method.
    $useractivation = $usersParams->get('useractivation');
    if ($useractivation === 1) {
      jimport('joomla.user.helper');
      $jdata['activation'] = JUtility::getHash(JUserHelper::genRandomPassword());
      $jdata['block'] = 1; // block the user
    } else {
      $jdata['block'] = 0; // don't block the user
    }

    // Write new user to the Joomla! database.
    if (!$new_user->bind($jdata)) {
      throw new Exception("Could not bind data. Error: " . $new_user->getError());
      return false;
    }

    if (!$new_user->save()) {
      throw new Exception("Could not save user. Error: " . $new_user->getError());
      return false;
    }

    // Finally, you can return the new user Id.
    $cuser = CFactory::getUser($new_user->id);
    return $cuser;

  } catch (Exception $ex) {
    consoleLog("could not create user: " . $ex->getMessage());
    throw new Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
    return false;
  }
}

Hope this help someone!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.