Beispiel #1
0
 /**
  * Gets a user link from the adapter map table.
  *
  * @param   string|integer  $user  Either a username or Joomla user ID.
  * @param   boolean         $isId  If the user input is a user ID then set to true.
  *
  * @return  array           Array of links for user or empty array if none found.
  *
  * @since   2.1
  */
 public static function getUser($user = null, $isId = false)
 {
     // Use I_ for joomla_id and U_ for usernames
     $cacheKey = ($isId ? 'I_' : 'U_') . $user;
     if (isset(self::$userCache[$cacheKey])) {
         return self::$userCache[$cacheKey];
     }
     if ($isId) {
         $links = self::lookupFromJoomlaId(self::TYPE_USER, $user);
     } else {
         $links = self::lookupFromUsername($user);
     }
     if ($links) {
         self::$userCache[$cacheKey] = $links;
         if (!$isId) {
             // There should be only ever one Joomla_Id for a user therefore we can cache that now
             foreach ($links as $link) {
                 self::$userCache['I_' . $link['joomla_id']] = array($link);
             }
         }
         return self::$userCache[$cacheKey];
     }
     // Backwards compatibility for 2.0 (*inefficient methods*)
     if ($type = SHUserHelper::getTypeParam($user)) {
         $domain = SHUserHelper::getDomainParam($user);
         return array(array('type' => self::TYPE_USER, 'adapter' => $type, 'domain' => $domain, 'adapter_id' => null, 'joomla_id' => null));
     }
     return array();
 }
Beispiel #2
0
	/**
	 * Method is called before user data is stored in the database.
	 *
	 * Saves profile data to LDAP if a profile form is detected.
	 *
	 * @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)
	{
		if (!$this->params->get('allow_ldap_save', 1))
		{
			// Not allowed to save back to LDAP
			return;
		}

		// Default the return result to true
		$result = true;

		try
		{
			// Get username for adapter
			$username = SHUtilArrayhelper::getValue($user, 'username', false, 'string');

			if (empty($username))
			{
				// The old username isn't present so use new username
				$username = SHUtilArrayhelper::getValue($new, 'username', false, 'string');
			}

			// Include the mandatory Joomla fields (fullname and email)
			$this->saveMandatoryToLdap($username, $new['name'], $new['email']);

			// Check there is a profile to save (i.e. this event may not have been called from the profile form)
			if ($this->use_profile && (isset($new[self::FORM_FIELDS_NAME]) && (count($new[self::FORM_FIELDS_NAME]))))
			{
				$xml = $this->getXMLFields(SHUserHelper::getDomainParam($new));

				// Only get profile data and enabled elements from the input
				$profileData = $this->cleanInput($xml, $new[self::FORM_FIELDS_NAME]);

				// Save the profile back to LDAP
				$result = $this->saveProfileToLdap($xml, $username, $profileData);
			}
		}
		catch (Exception $e)
		{
			SHLog::add($e, 12232, JLog::ERROR, 'ldap');

			return false;
		}

		return $result;
	}
Beispiel #3
0
	/**
	 * Gets the user adapter for the user specified. Creates a new user
	 * adapter if one doesnt already exist for the user.
	 *
	 * @param   array|string  $user     Either a username string or array of credentials including JUser ID and domain.
	 * @param   string        $type     Type of adapter (e.g. ldap, xml, federated).
	 * @param   array         $options  An array of optional options including isNew.
	 *
	 * @return  SHUserAdapter  Object to user adapter.
	 *
	 * @since   2.0
	 * @throws  Exception
	 */
	public static function getUserAdapter($user, $type = null, $options = array())
	{
		if (is_array($user))
		{
			$username = strtolower(SHUtilArrayhelper::getValue($user, 'username', null, 'string'));
			$credentials = $user;
		}
		else
		{
			$username = strtolower((string) $user);
			$credentials = array('username' => $username);
		}

		if (empty($username))
		{
			throw new RuntimeException(JText::_('LIB_SHFACTORY_ERR_2121'), 2121);
		}

		if (!isset(self::$adapters[$username]))
		{
			$config = self::getConfig();

			// Check if this user is in the blacklist
			if ($blacklist = (array) json_decode($config->get('user.blacklist')))
			{
				if (in_array($username, $blacklist))
				{
					throw new RuntimeException(JText::sprintf('LIB_SHFACTORY_ERR_2125', $username), 2125);
				}
			}

			// If the JUser ID has been specified then use it (more efficient)
			if ($id = isset($credentials['id']) ? (int) $credentials['id'] : JUserHelper::getUserId($username))
			{
				$jUser = JFactory::getUser($id);

				if ((boolean) $config->get('user.usedomain', true))
				{
					if (!isset($credentials['domain']))
					{
						// Attempt to get the domain for this user
						$credentials['domain'] = SHUserHelper::getDomainParam($jUser);
					}
				}
				else
				{
					unset($credentials['domain']);
				}

				if (!isset($credentials['type']))
				{
					// Attempt to get the User Adapter type
					$type = SHUserHelper::getTypeParam($jUser);
				}
			}

			if (is_null($type))
			{
				// Get the default/primary user adpater type from the database
				$type = $config->get('user.type');
			}

			// Camel case friendly for class name
			$type = ucfirst(strtolower($type));
			$class = "SHUserAdapters${type}";

			if (class_exists($class))
			{
				// Create the adapter (note: remember to unset if using multiple adapters!)
				self::$adapters[$username] = new $class($credentials, null, $options);
			}
			else
			{
				throw new RuntimeException(JText::sprintf('LIB_SHFACTORY_ERR_2123', $class), 2123);
			}
		}
		else
		{
			// Update credentials if required
			if ($password = SHUtilArrayhelper::getValue($user, 'password', false))
			{
				self::$adapters[$username]->updateCredential($password, $options);
			}
		}

		return self::$adapters[$username];
	}