Exemplo n.º 1
0
 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;
 }
Exemplo n.º 2
0
 /**
  * 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;
 }