Joomla 2.5在没有电子邮件地址的PHP脚本中创建用户

Joomla 2.5在没有电子邮件地址的PHP脚本中创建用户

问题描述:

I have a a php function that works for creating a new user in Joomla 2.5. This function is used to synchronize an external customer database with Joomla.

The new requirement is that the email address needs to be an optional field. I can't seem to get the JFactory function to work without an email address. Is there another way to get the user created?

function add_joomla_user($username, $email, $name, $password, $group) {
    // Creates a new user in Joomla database with passed in information
    $return_message = '';

    $mainframe =& JFactory::getApplication("site");
    $mainframe->initialise();

    $user = JFactory::getUser(0); // it's important to set the "0" otherwise your admin user information will be loaded
    jimport('joomla.application.component.helper');     // include libraries/application/component/helper.php
    $usersParams = &JComponentHelper::getParams( 'com_users' );     // load the Params
    $userdata = array(); // place user data in an array for storing.
    $userdata['username'] = $username;
    $userdata['email'] = $email;
    $userdata['name'] = $name;
    $userdata['password'] = $password;
    $userdata['password2'] = $password;
    $defaultUserGroup = $usersParams->get('new_usertype', $group);
    $userdata['groups']=array($defaultUserGroup);   
    $userdata['block'] = 0; // set this to 0 so the user will be added immediately.

    if (!$user->bind($userdata)) { // bind the data and if it fails raise an error
        JError::raiseWarning('', JText::_( $user->getError())); // something went wrong!!
        $return_message = 'Error binding data: ' . $user->getError();
    }

    if (!$user->save()) { 
        JError::raiseWarning('', JText::_( $user->getError())); 
        $return_message = 'Error creating user: ' . $user->getError();
    } else {
        $return_message = 'Created user';
    }
    return $return_message;
}

我有一个php函数,可用于在Joomla 2.5中创建新用户。 此函数用于将外部客户数据库与Joomla同步。 p>

新要求是电子邮件地址必须是可选字段。 我似乎无法让JFactory功能在没有电子邮件地址的情况下工作。 是否有另一种方法来创建用户? p>

  function add_joomla_user($ username,$ email,$ name,$ password,$ group){
 //创建一个新的 Joomla数据库中的用户传入的信息
 $ return_message =''; 
 
 $ mainframe =&  JFactory :: getApplication(“site”); 
 $ mainframe-> initialise(); 
 
 $ user = JFactory :: getUser(0);  //设置“0”非常重要,否则将加载管理员用户信息
 jimport('joomla.application.component.helper');  // include libraries / application / component / helper.php 
 $ usersParams =& JComponentHelper :: getParams('com_users');  //加载Params 
 $ userdata = array();  //将用户数据放入数组中进行存储。
 $ userdata ['username'] = $ username; 
 $ userdata ['email'] = $ email; 
 $ userdata ['name'] = $ name;  
 $ userdata ['password'] = $ password; 
 $ userdata ['password2'] = $ password; 
 $ defaultUserGroup = $ usersParams-> get('new_usertype',$ group); 
 $ userdata  [ '基团'] =阵列($ defaultUserGroup);  
 $ userdata ['block'] = 0;  //将此值设置为0,以便立即添加用户。
 
 if(!$ user-> bind($ userdata)){//绑定数据,如果失败则引发错误
 JError ::  raiseWarning('',JText :: _($ user-> getError()));  //出错了!! 
 $ return_message ='错误绑定数据:'。  $ user-> getError(); 
} 
 
 if(!$ user-> save()){
 JError :: raiseWarning('',JText :: _($ user-> getError)  ()));  
 $ return_message ='创建用户时出错:'。  $ user-> getError(); 
} else {
 $ return_message ='创建用户'; 
} 
返回$ return_message; 
} 
  code>  pre> 
   DIV>

Joomla user handling definitely requires a unique email address for each user. It's tricky and I somewhat hesitate to suggest this, but what you could do if it is missing is substitute a random string or a string generated based on information in your database (like the $userdata['email'] = $username . '@noemail';. That way they will be easy to find later. Of course this means password reset and other functions will never work, but that would be true anyway if the user has no email.