Ejemplo n.º 1
0
 /**
  * @param array $path
  * @param \Rocker\Server $server
  * @return OperationResponse
  */
 public function dispatchRequest(array $path, Server $server)
 {
     $op = $this->loadOperation($path);
     $method = $this->server->request()->getMethod();
     // Could not resolve any operation, return 404
     if ($op === null) {
         $response = new OperationResponse(404);
         $response->setBody(array('error' => 'Operation not found'));
         return $response;
     }
     $op->setRequest($server->request());
     $isAuthenticated = $this->authenticate($op, $server);
     // Handle OPTIONS request
     if ($method == 'OPTIONS') {
         $response = new OperationResponse();
         // Add allowed request data
         $requestHeaders = $server->request()->headers('Access-Control-Request-Headers', false);
         $allowedHeaders = 'Authorization, Content-Type, Content-Length' . ($requestHeaders ? ', ' . $requestHeaders : '');
         $response->addHeader('Access-Control-Allow-Headers', ucwords($allowedHeaders));
         $response->addHeader('Access-Control-Allow-Methods', implode(',', $op->allowedMethods()));
         return $response;
     }
     // Wrong method!
     if (!in_array($method, $op->allowedMethods())) {
         $response = new OperationResponse(405);
         $response->addHeader('Access-Control-Allow-Methods', implode(',', $op->allowedMethods()));
         $response->setBody(array('error' => 'Wrong request method, only ' . implode(', ', $op->allowedMethods()) . ' is allowed'));
     } elseif ($op->requiresAuth() && !$isAuthenticated) {
         $response = new OperationResponse(401);
         $with = $server->request()->headers('HTTP_X_REQUESTED_WITH');
         if (!$with) {
             $with = $server->request()->headers('X_REQUESTED_WITH');
         }
         if (!$with || strtolower($with) != 'xmlhttprequest') {
             $authConfig = $this->server->config('application.auth');
             $response->setHeaders(array('WWW-Authenticate' => $authConfig['mechanism']));
         }
         $response->addHeader('Access-Control-Allow-Methods', implode(',', $op->allowedMethods()));
     } elseif ($missingArgs = $this->findMissingArgs($method, $op)) {
         $response = new OperationResponse(400);
         $response->setBody(array('error' => 'One or more required arguments is missing (' . implode(', ', $missingArgs) . ')'));
     } else {
         $response = $op->exec($this->server, $this->db, $this->cache);
     }
     return $response;
 }
Ejemplo n.º 2
0
 /**
  * @param Server $server
  * @param ConnectionInterface $db
  * @param \Rocker\Cache\CacheInterface $cache
  * @return array
  */
 public static function deleteUserEvent($server, $db, $cache)
 {
     $userFactory = new UserFactory($db, $cache);
     $user = $userFactory->load(basename($server->request()->getPath()));
     if ($user !== null && ($files = $user->meta()->get('files', array()))) {
         $storage = self::loadStorageClass($server);
         self::deleteAllFiles($user, $userFactory, $files, $storage);
     }
 }