Пример #1
0
 /**
  * Method to create a new Joomla! user if it does not yet exist
  *
  * @param array $user
  * @param bool $empty_password
  * @return JUser|null
  */
 public function create($user, $empty_password = false)
 {
     // Check on the users email
     if (empty($user['email']) || $this->isValidEmail($user['email']) == false) {
         return false;
     }
     // Import needed libraries
     jimport('joomla.utilities.date');
     jimport('joomla.user.helper');
     jimport('joomla.application.component.helper');
     // Import user plugins
     JPluginHelper::importPlugin('user');
     // Get system variables
     $db = JFactory::getDBO();
     // Determine the email address
     $email = $user['email'];
     if (!empty($user['original_data']['email'])) {
         $email = $user['original_data']['email'];
     }
     // Try to fetch the user-record from the database
     $query = 'SELECT `id` FROM #__users WHERE email=' . $db->quote(email);
     $db->setQuery($query);
     $result = $db->loadResult();
     // If $result is empty, this user (with $user['email']) does not exist yet
     if (empty($result)) {
         // Construct a data-array for this user
         $data = array('name' => $user['name'], 'username' => $user['username'], 'email' => $user['email'], 'guest' => 0);
         // Current date
         $now = new JDate();
         $data['registerDate'] = $now->toSql();
         // Do not use empty passwords in the Joomla! user-record
         if ($empty_password == false) {
             // Generate a new password if a password is not set
             if (!empty($user['password']) && is_string($user['password'])) {
                 $password = $user['password'];
             } else {
                 $password = JUserHelper::genRandomPassword();
             }
             // Generate the encrypted password
             $salt = JUserHelper::genRandomPassword(32);
             $crypt = JUserHelper::getCryptedPassword($password, $salt);
             $data['password'] = $crypt . ':' . $salt;
             $data['password2'] = $crypt . ':' . $salt;
             // Use empty password in the Joomla! user-record
         } else {
             $data['password'] = '';
             $data['password2'] = '';
         }
         // Make sure MageBridge events stop
         $data['disable_events'] = 1;
         // Trigger the before-save event
         MageBridgeModelDebug::getInstance()->notice('Firing event onUserBeforeSave');
         JFactory::getApplication()->triggerEvent('onUserBeforeSave', array($data, true, $data));
         // Get the com_user table-class and use it to store the data to the database
         $table = JTable::getInstance('user', 'JTable');
         $table->bind($data);
         $result = $table->store();
         // Load the user
         $newuser = $this->loadByEmail($user['email']);
         $data['id'] = $newuser->id;
         // Trigger the after-save event
         MageBridgeModelDebug::getInstance()->notice('Firing event onUserAfterSave');
         JFactory::getApplication()->triggerEvent('onUserAfterSave', array($data, true, true, null));
         // Add additional data
         if (isset($table->id) && $table->id > 0) {
             // Check whether the current user is part of any groups
             $db->setQuery('SELECT * FROM `#__user_usergroup_map` WHERE `user_id`=' . $table->id);
             $rows = $db->loadObjectList();
             if (empty($rows)) {
                 $group_id = MageBridgeUserHelper::getDefaultJoomlaGroupid();
                 if (!empty($group_id)) {
                     $db->setQuery('INSERT INTO `#__user_usergroup_map` SET `user_id`=' . $table->id . ', `group_id`=' . $group_id);
                     $db->execute();
                 }
             }
         }
         // Get the resulting user
         return self::loadByEmail($user['email']);
     }
     return null;
 }
Пример #2
0
 public function create($user, $empty_password = false)
 {
     // Import needed libraries
     jimport('joomla.utilities.date');
     jimport('joomla.user.helper');
     jimport('joomla.application.component.helper');
     // Get system variables
     $db = JFactory::getDBO();
     // Try to fetch the user-record from the database
     $query = 'SELECT `id` FROM #__users WHERE email=' . $db->quote($user['email']);
     $db->setQuery($query);
     $result = $db->loadResult();
     // If $result is empty, this user (with $user['email']) does not exist yet
     if (empty($result)) {
         // Construct a data-array for this user
         $data = array('name' => $user['name'], 'username' => $user['username'], 'email' => $user['email'], 'guest' => 0);
         // Current date
         $now = new JDate();
         $data['registerDate'] = method_exists('JDate', 'toSql') ? $now->toSql() : $now->toMySQL();
         // Add Joomla! 1.5 specific data
         if (MageBridgeHelper::isJoomla15()) {
             $data['usertype'] = MageBridgeUserHelper::getDefaultJoomlaGroup();
             $data['gid'] = MageBridgeUserHelper::getDefaultJoomlaGroupid();
         }
         // Do not use empty passwords in the Joomla! user-record
         if ($empty_password == false) {
             // Generate a new password if a password is not set
             $pasword = empty($user['password']) ? JUserHelper::genRandomPassword() : $user['password'];
             // Generate the encrypted password
             $salt = JUserHelper::genRandomPassword(32);
             $crypt = JUserHelper::getCryptedPassword($password, $salt);
             $data['password'] = $crypt . ':' . $salt;
             $data['password2'] = $crypt . ':' . $salt;
             // Use empty password in the Joomla! user-record
         } else {
             $data['password'] = '';
             $data['password2'] = '';
         }
         // Get the com_user table-class and use it to store the data to the database
         $table = JTable::getInstance('user', 'JTable');
         $table->bind($data);
         $table->store();
         // Add Joomla! 1.6 or higher specific data
         if (MageBridgeHelper::isJoomla15() == false) {
             if (isset($table->id) && $table->id > 0) {
                 // Check whether the current user is part of any groups
                 $db->setQuery('SELECT * FROM #__user_usergroup_map` WHERE `user_id`=' . $table->id);
                 $rows = $db->loadObjectList();
                 if (empty($rows)) {
                     $group_id = MageBridgeUserHelper::getDefaultJoomlaGroupid();
                     if (!empty($group_id)) {
                         $db->setQuery('INSERT INTO `#__user_usergroup_map` SET `user_id`=' . $table->id . ', `group_id`=' . $group_id);
                         $db->query();
                     }
                 }
             }
         }
         // Get the resulting user
         return self::loadByEmail($user['email']);
     }
     return null;
 }