/** * Create the user to LDAP (before onUserBeforeSave). * * @param array $user Populated LDAP attributes from the form. * * @return boolean Cancels the user creation to Joomla if False. * * @since 2.0 */ public function onUserCreation($user) { try { $dn = null; $attributes = array(); // Populate defaults for the mandatory $mandatory = array( 'username' => SHUtilArrayhelper::getValue($user, 'username'), 'password' => SHUtilArrayhelper::getValue($user, 'password_clear'), 'email' => SHUtilArrayhelper::getValue($user, 'email'), 'name' => SHUtilArrayhelper::getValue($user, 'name') ); // Include the helper file only if it exists if ($this->helper = $this->_getHelperFile()) { // Calculate the correct domain to insert user on if (method_exists($this->helper, 'getDomain')) { $this->domain = $this->helper->getDomain($user); } } $fields = $this->_getXMLFields(); // Loops around everything in the template XML foreach ($fields as $key => $value) { // Convert the value to a string $stringValue = (string) $value; // Convert the key to a string $stringKey = (string) $key; $name = (string) $value->attributes()->name; if ($stringKey == 'dn') { $name = 'mandatory' . $stringKey; // The dn which isn't an array $attribute =& $dn; } elseif ($stringKey == 'username' || $stringKey == 'password' || $stringKey == 'email' || $stringKey == 'name') { $name = 'mandatory' . $stringKey; // The mandatory fields use something a bit different $attribute =& $mandatory[$stringKey]; } else { // Standard multi-array attributes if (!isset($attributes[$name])) { $attributes[$name] = array(); } $attribute =& $attributes[$name][]; } // Get the value of the attributes using a variety of types switch ((string) $value->attributes()->type) { case 'form': $attribute = $user[$stringValue]; break; case 'string': $attribute = $stringValue; break; case 'eval': $attribute = $this->_execEval($stringValue, $user); break; case 'helper': $method = 'get' . (string) $name; $attribute = $this->helper->{$method}($user); break; } } $credentials = array( 'username' => $mandatory['username'], 'password' => $mandatory['password'], 'domain' => $this->domain, 'dn' => $dn ); // Kill any previous adapters for this user (though this plugin should be ordered first!!) SHFactory::$adapters[strtolower($user['username'])] = null; // Create an adapter and save core attributes $adapter = SHFactory::getUserAdapter($credentials, 'ldap', array('isNew' => true)); // Add core Joomla fields $adapter->setAttributes( array( 'username' => $mandatory['username'], 'password' => $mandatory['password'], 'fullname' => $mandatory['name'], 'email' => $mandatory['email'] ) ); // Add extra fields based from the template xml $adapter->setAttributes($attributes); // Create the LDAP user now SHLdapHelper::commitChanges($adapter, true, true); SHLog::add(JText::sprintf('PLG_LDAP_CREATION_INFO_12821', $mandatory['username']), 12821, JLog::INFO, 'ldap'); $this->username = $mandatory['username']; /* * Call onAfterCreation method in the helper which can be used to run * external scripts (such as creating home directories) and/or adding * groups to the new user. * * This method will be passed: * - $user Values directly from the user registration form. * - $attributes The attributes passed to the LDAP server for creation. * - $adapter The user adapter object. */ if ($this->helper && method_exists($this->helper, 'onAfterCreation')) { $this->helper->onAfterCreation($user, $attributes, $adapter); } return true; } catch (Exception $e) { SHLog::add($e, 12802, JLog::ERROR, 'ldap'); return false; } }
/** * Method is called before user data is stored in the database. * * @param array $user Holds the old user data. * @param boolean $isNew True if a new user is stored. * @param array $new Holds the new user data. * * @return boolean Cancels the save if False. * * @since 2.0 */ public function onUserBeforeSave($user, $isNew, $new) { $isAdapterExisting = true; $isLdapExisting = false; // Get the correct username where new username must be used when user isNew $username = $isNew ? $new['username'] : $user['username']; try { // We want to check if this user is an existing user in an Adapter $adapter = SHFactory::getUserAdapter($username); $adapter->getId(false); } catch (Exception $e) { // We will assume this user doesnt exist in an Adapter $isAdapterExisting = false; } if ($isAdapterExisting) { // We need to check the adapter is LDAP or not $isLdapExisting = $adapter->getType('LDAP'); } if ($isLdapExisting) { $this->isLdap = true; if (SHLdapHelper::triggerEvent('onUserBeforeSave', array($user, $isNew, $new)) !== false) { try { // Commit the changes to the Adapter if present SHLdapHelper::commitChanges($adapter, true, true); SHLog::add(JText::sprintf('LIB_SHLDAPEVENTBOUNCER_DEBUG_10986', $username), 10986, JLog::DEBUG, 'ldap'); } catch (Excpetion $e) { SHLog::add($e, 10981, JLog::ERROR, 'ldap'); } // For now lets NOT block the user from logging in even with a error return true; } return false; } elseif ($isNew) { // Ask all plugins if there is a plugin willing to deal with user creation for ldap if (count($results = SHFactory::getDispatcher('ldap')->trigger('askUserCreation'))) { // First, we must create and save the user as some plugins may talk to LDAP directly and cannot be delayed $result = SHLdapHelper::triggerEvent('onUserCreation', array($new)); // Allow Ldap events to be called if ($this->isLdap = $result) { JFactory::getSession()->set('created', $username, 'ldap'); if (SHLdapHelper::triggerEvent('onUserBeforeSave', array($user, $isNew, $new)) !== false) { try { // Commit the changes to the Adapter if present $adapter = SHFactory::getUserAdapter($username); SHLdapHelper::commitChanges($adapter, true, true); } catch (Exception $e) { SHLog::add($e, 10981, JLog::ERROR, 'ldap'); } // For now lets NOT block the user from logging in even with a error return true; } } // Something went wrong with the user creation return false; } } }