public static function authenticate($username, $password) { if (!empty($username) && ($authSource = AuthUserTable::getAuthSource($username))) { Console::debug("Found authentication source {$authSource} for {$username}"); $driverList = array(AuthDriverManager::getDriver($authSource)); } else { // avoid issues with session collision when many users connect from // the same computer at the same time with the same browser session ! if (AuthUserTable::userExists($username)) { self::setFailureMessage(get_lang("There is already an account with this username.")); return false; } $authSource = null; $driverList = AuthDriverManager::getRegisteredDrivers(); } foreach ($driverList as $driver) { $driver->setAuthenticationParams($username, $password); if ($driver->authenticate()) { $uid = AuthUserTable::registered($username, $driver->getAuthSource()); if ($uid) { if ($driver->userUpdateAllowed()) { $userAttrList = $driver->getFilteredUserData(); if (isset($userAttrList['loginName'])) { $newUserName = $userAttrList['loginName']; if (!get_conf('claro_authUsernameCaseSensitive', true)) { $newUsername = strtolower($newUserName); $username = strtolower($username); } // avoid session collisions ! if ($username != $newUserName) { Console::error("EXTAUTH ERROR : try to overwrite an existing user {$username} with another one" . var_export($userAttrList, true)); } else { AuthUserTable::updateUser($uid, $userAttrList); Console::info("EXTAUTH INFO : update user {$uid} {$username} with " . var_export($userAttrList, true)); } } else { Console::error("EXTAUTH ERROR : no loginName given for user {$username} by authSource " . $driver->getAuthSource()); } } return Claro_CurrentUser::getInstance($uid, true); } elseif ($driver->userRegistrationAllowed()) { // duplicate code here to avoid issue with multiple requests on a busy server ! if (AuthUserTable::userExists($username)) { self::setFailureMessage(get_lang("There is already an account with this username.")); return false; } $uid = AuthUserTable::createUser($driver->getUserData()); return Claro_CurrentUser::getInstance($uid, true); } } elseif ($authSource) { self::setFailureMessage($driver->getFailureMessage()); } } // authentication failed return false; }
// first login for a not self registred (e.g. registered by a teacher) // do nothing (code may be added later) $currentUser->updateCreatorId(); $_SESSION['firstLogin'] = true; } else { $_SESSION['firstLogin'] = false; } // RECORD SSO COOKIE // $ssoEnabled set in conf/auth.sso.conf.php if (get_conf('ssoEnabled', false)) { FromKernel::uses('sso/cookie.lib'); $boolCookie = SingleSignOnCookie::setForUser($currentUser->userId); } // end if ssoEnabled } else { $currentUser = Claro_CurrentUser::getInstance($_uid); try { $currentUser->loadFromSession(); $_user = $currentUser->getRawData(); } catch (Exception $e) { $_user = null; } } } catch (Exception $e) { exit('WARNING !! Undefined user id: the requested user doesn\'t exist ' . 'at line ' . __LINE__); } } else { // Anonymous, logout or login failed $_user = null; $_uid = null; $is_platformAdmin = false;
/** * Get the authentication profile for the given user id * @param int $userId * @return AuthProfile */ public static function getUserAuthProfile($userId) { if ($userId != claro_get_current_user_id()) { $user = new Claro_User($userId); $user->loadFromDatabase(); } else { $user = Claro_CurrentUser::getInstance(); } $authSource = $user->authSource; if (!$authSource) { throw new Exception("Cannot find user authentication source for user {$userId}"); } try { $profileOptions = AuthDriverManager::getDriver($authSource)->getAuthProfileOptions(); } catch (Exception $e) { if (claro_is_platform_admin() || claro_is_in_a_course() && claro_is_course_manager() && $userId != claro_get_current_user_id()) { Console::warning("Cannot find user authentication source for user {$userId}, use claroline default options instead"); $profileOptions = AuthDriverManager::getDriver('claroline')->getAuthProfileOptions(); } else { throw $e; } } $authProfile = new AuthProfile($userId, $authSource); $authProfile->setAuthDriverOptions($profileOptions); if (claro_debug_mode()) { pushClaroMessage(var_export($profileOptions, true), 'debug'); } return $authProfile; }
/** * Singleton constructor * @todo avoid using the singleton pattern and use a factory instead ? * @param int $uid user id * @param boolean $forceReload force reloading the data * @return Claro_CurrentUser current user */ public static function getInstance($uid = null, $forceReload = false) { if ($forceReload || !self::$instance) { self::$instance = new self($uid); if (!$forceReload && claro_is_user_authenticated()) { self::$instance->loadFromSession(); } else { self::$instance->load($forceReload); } } return self::$instance; }