/** * 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(); }
/** * 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]; }
/** * Returns if the current or specified user was authenticated * via LDAP. * * @param JUser|integer|array $user Optional user id (if null then uses current user). * * @return boolean True if user is Ldap authenticated or False otherwise. * * @since 2.0 */ public static function isUserLdap($user = null) { $type = SHUserHelper::getTypeParam($user); // Create a new adapter if ($type = ucfirst(strtolower($type))) { $class = "SHUserAdapters${type}"; $adapter = new $class(array('username' => '', 'password' => '')); return $adapter->getType('LDAP') ? true : false; } return false; }