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

时间:2015-07-07 11:06:27

标签: php joomla joomla2.5

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

新要求是电子邮件地址必须是可选字段。我似乎无法在没有电子邮件地址的情况下使JFactory功能正常工作。是否有其他方法可以创建用户?

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;
}

1 个答案:

答案 0 :(得分:1)

Joomla用户处理肯定需要为每个用户提供唯一的电子邮件地址。这很棘手,我有点犹豫不决,但如果缺少它可以做的是替换随机字符串或基于数据库中的信息生成的字符串(如$userdata['email'] = $username . '@noemail';。这样他们将很容易当然这意味着密码重置和其他功能将无法正常工作,但如果用户没有电子邮件,则无论如何都是如此。