コード例 #1
0
 /**
  * Returns a shared instance of the eZUser class pr $id value.
  * If user can not be fetched, then anonymous user is returned and
  * a warning trown, if anonymous user can not be fetched, then NoUser
  * is returned and another warning is thrown.
  *
  * @param int|false $id On false: Gets current user id from session
  *        or from {@link eZUser::anonymousId()} if not set.
  * @return eZUser
  */
 static function instance($id = false)
 {
     if (!empty($GLOBALS["eZUserGlobalInstance_{$id}"])) {
         return $GLOBALS["eZUserGlobalInstance_{$id}"];
     }
     $userId = $id;
     $currentUser = null;
     $http = eZHTTPTool::instance();
     $anonymousUserID = self::anonymousId();
     $sessionHasStarted = eZSession::hasStarted();
     // If not specified get the current user
     if ($userId === false) {
         if ($sessionHasStarted) {
             $userId = $http->sessionVariable('eZUserLoggedInID');
             if (!is_numeric($userId)) {
                 $userId = $anonymousUserID;
                 eZSession::setUserID($userId);
                 $http->setSessionVariable('eZUserLoggedInID', $userId);
             }
         } else {
             $userId = $anonymousUserID;
             eZSession::setUserID($userId);
         }
     }
     // Check user cache (this effectivly fetches user from cache)
     // user not found if !isset( isset( $userCache['info'][$userId] ) )
     $userCache = self::getUserCacheByUserId($userId);
     if (isset($userCache['info'][$userId])) {
         $userArray = $userCache['info'][$userId];
         if (is_numeric($userArray['contentobject_id'])) {
             $currentUser = new eZUser($userArray);
             $currentUser->setUserCache($userCache);
         }
     }
     $ini = eZINI::instance();
     // Check if:
     // - the user has not logged out,
     // - the user is not logged in,
     // - and if a automatic single sign on plugin is enabled.
     if (!self::$userHasLoggedOut && is_object($currentUser) && !$currentUser->isRegistered()) {
         $ssoHandlerArray = $ini->variable('UserSettings', 'SingleSignOnHandlerArray');
         if (!empty($ssoHandlerArray)) {
             $ssoUser = false;
             foreach ($ssoHandlerArray as $ssoHandler) {
                 $className = 'eZ' . $ssoHandler . 'SSOHandler';
                 if (class_exists($className)) {
                     $impl = new $className();
                     $ssoUser = $impl->handleSSOLogin();
                     // If a user was found via SSO, then use it
                     if ($ssoUser !== false) {
                         $currentUser = $ssoUser;
                         $userId = $currentUser->attribute('contentobject_id');
                         $userInfo = array();
                         $userInfo[$userId] = array('contentobject_id' => $userId, 'login' => $currentUser->attribute('login'), 'email' => $currentUser->attribute('email'), 'password_hash' => $currentUser->attribute('password_hash'), 'password_hash_type' => $currentUser->attribute('password_hash_type'));
                         eZSession::setUserID($userId);
                         $http->setSessionVariable('eZUserLoggedInID', $userId);
                         eZUser::updateLastVisit($userId);
                         eZUser::setCurrentlyLoggedInUser($currentUser, $userId);
                         eZHTTPTool::redirect(eZSys::wwwDir() . eZSys::indexFile(false) . eZSys::requestURI() . eZSys::queryString(), array(), 302);
                         eZExecution::cleanExit();
                     }
                 } else {
                     eZDebug::writeError("Undefined ssoHandler class: {$className}", __METHOD__);
                 }
             }
         }
     }
     if ($userId != $anonymousUserID) {
         $sessionInactivityTimeout = $ini->variable('Session', 'ActivityTimeout');
         if (!isset($GLOBALS['eZSessionIdleTime'])) {
             eZUser::updateLastVisit($userId);
         } else {
             $sessionIdle = $GLOBALS['eZSessionIdleTime'];
             if ($sessionIdle > $sessionInactivityTimeout) {
                 eZUser::updateLastVisit($userId);
             }
         }
     }
     if (!$currentUser) {
         $currentUser = eZUser::fetch(self::anonymousId());
         eZDebug::writeWarning('User not found, returning anonymous');
     }
     if (!$currentUser) {
         $currentUser = new eZUser(array('id' => -1, 'login' => 'NoUser'));
         eZDebug::writeWarning('Anonymous user not found, returning NoUser');
     }
     $GLOBALS["eZUserGlobalInstance_{$id}"] = $currentUser;
     return $currentUser;
 }