Ejemplo n.º 1
0
 /**
  * Log-in client if successful or terminate api if not authorized
  *
  * @param   string  $scope                     Name of the scope to test against
  * @param   bool    $terminateIfNotAuthorized  Terminate api if client is not authorized
  *
  * @throws Exception
  * @return  bool
  *
  * @since   1.2
  */
 public function isAuthorized($scope, $terminateIfNotAuthorized)
 {
     $authorized = false;
     JFactory::getApplication()->triggerEvent('RApiHalBeforeIsAuthorizedCheck', array($scope, $terminateIfNotAuthorized, $this->options, $this->authorizationCheck, &$authorized));
     if ($authorized) {
         return $authorized;
     }
     // OAuth2 check
     if ($this->authorizationCheck == 'oauth2') {
         /** @var $response OAuth2\Response */
         $response = RApiOauth2Helper::verifyResourceRequest($scope);
         if ($response instanceof OAuth2\Response) {
             if (!$response->isSuccessful() && $terminateIfNotAuthorized) {
                 // OAuth2 Server response is in fact correct output for errors
                 $response->send($this->options->get('format', 'json'));
                 JFactory::getApplication()->close();
             }
         } elseif ($response === false && $terminateIfNotAuthorized) {
             throw new Exception(JText::_('LIB_REDCORE_API_OAUTH2_SERVER_IS_NOT_ACTIVE'));
         } else {
             $response = json_decode($response);
             if (!empty($response->user_id)) {
                 $user = JFactory::getUser($response->user_id);
                 // Load the JUser class on application for this client
                 JFactory::getApplication()->loadIdentity($user);
                 JFactory::getSession()->set('user', $user);
                 return true;
             }
             $authorized = false || !$terminateIfNotAuthorized;
         }
     } elseif ($this->authorizationCheck == 'joomla') {
         // Get username and password from globals
         $credentials = RApiHalHelper::getCredentialsFromGlobals();
         $authorized = RUser::userLogin($credentials) || !$terminateIfNotAuthorized;
     }
     if (!$authorized && $terminateIfNotAuthorized) {
         $customError = $this->triggerFunction('createCustomHttpError', 401, $this->apiErrors);
         $this->setStatusCode(401, $customError);
     }
     return $authorized || !$terminateIfNotAuthorized;
 }