public function onUserBeforeSaveGroup($form, $table, $isNew) { $groupname = $table->title; try { // We want to check if this group is an existing group in an Adapter $adapter = SHFactory::getGroupAdapter($groupname); $adapter->getId(); // We need to gather the adapter name to call the correct dispatcher $adapterName = $adapter::getName(); } catch (Exception $e) { // We will assume this group doesnt exist in an Adapter $adapterName = false; } if ($adapterName) { $event = SHAdapterEventHelper::triggerEvent($adapterName, 'onGroupBeforeSave', array($groupname, $isNew)); if ($event !== false) { try { // Commit the changes to the Adapter if present SHAdapterHelper::commitChanges($adapter, true, true); //TODO: newId SHLog::add(JText::sprintf('LIB_SHADAPTEREVENTBOUNCER_DEBUG_10986', $groupname), 10986, JLog::DEBUG, $adapterName); return true; } catch (Excpetion $e) { //TODO: newId SHLog::add($e, 10981, JLog::ERROR, $adapterName); } } return $event; } elseif ($isNew) { // Use a default group adapter $name = SHFactory::getConfig()->get('user.type'); // We must create and save the group as plugins may talk to adapter driver and expect a group object if (SHAdapterEventHelper::triggerEvent($name, 'onGroupCreation', array($groupname)) === true) { JFactory::getSession()->set('created', $groupname, SHGroupHelper::SESSION_KEY); $event = SHAdapterEventHelper::triggerEvent($adapterName, 'onGroupBeforeSave', array($groupname, $isNew)); if ($event !== false) { try { // Commit the changes to the Adapter if present $adapter = SHFactory::getGroupAdapter($groupname); SHAdapterHelper::commitChanges($adapter, true, true); return true; } catch (Exception $e) { //TODO: newId SHLog::add($e, 10981, JLog::ERROR, $name); } } return $event; } // Something went wrong with the group creation return false; } }
/** * Commits the changes to the LDAP user adapter and parses the result. * If any errors occurred then optionally log them and throw an exception. * * @param SHUserAdaptersLdap $adapter LDAP user adapter. * @param boolean $log Log any errors directly to SHLog. * @param boolean $throw Throws an exception on error OR return array on error. * * @return true|array * * @deprecated Use SHAdapterHelper::commitChanges() instead */ public static function commitChanges($adapter, $log = false, $throw = true) { return SHAdapterHelper::commitChanges($adapter, $log, $throw); }
/** * 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' => JArrayHelper::getValue($user, 'username'), 'password' => JArrayHelper::getValue($user, 'password_clear'), 'email' => JArrayHelper::getValue($user, 'email'), 'name' => JArrayHelper::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 SHAdapterHelper::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; } }