Exemplo n.º 1
0
 /**
  * Invoke middleware
  *
  * @param  RequestInterface  $request  PSR7 request object
  * @param  ResponseInterface $response PSR7 response object
  * @param  callable          $next     Next middleware callable
  *
  * @return ResponseInterface PSR7 response object
  */
 public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
 {
     // Validate POST, PUT, DELETE, PATCH requests
     if (in_array($request->getMethod(), ['POST', 'PUT', 'DELETE', 'PATCH'])) {
         $body = $request->getParsedBody();
         $body = $body ? (array) $body : [];
         $name = isset($body[$this->prefix . '_name']) ? $body[$this->prefix . '_name'] : false;
         $value = isset($body[$this->prefix . '_value']) ? $body[$this->prefix . '_value'] : false;
         if (!$name || !$value || !$this->validateToken($name, $value)) {
             return $response->withStatus(400);
         }
     }
     // Generate new CSRF token
     $name = $this->prefix . mt_rand(0, mt_getrandmax());
     $value = $this->createToken();
     $this->saveToStorage($name, $value);
     $request = $request->withAttribute($this->prefix . '_name', $name)->withAttribute($this->prefix . '_value', $value);
     return $next($request, $response);
 }
 /**
  * Action logic
  *
  * @param RequestInterface $request Request
  * @param ResponseInterface $response Response
  * @return ResponseInterface
  */
 public function run(RequestInterface $request, ResponseInterface $response)
 {
     /** @var \Psr\Http\Message\ServerRequestInterface $request */
     /** @var \Doctrine\ORM\EntityManager $entityManager */
     // recovering the user data and validate
     $user = $this->getUser($request->getAttribute('id'));
     $validator = new UserValidator('modify', $request, $this->container);
     if (!$validator->validate()) {
         return new ValidationJsonResponse($validator->errors());
     }
     // update the user
     $entityManager = $this->container->get('EntityManager');
     $data = $request->getParsedBody();
     foreach ($data as $field => $value) {
         $user->{'set' . ucfirst($field)}($value);
     }
     $entityManager->flush();
     return new JsonResponse(['status' => 'updated'], 200);
 }
 /**
  * Action logic
  *
  * @param RequestInterface $request Petición
  * @param ResponseInterface $response Respuesta
  * @return ResponseInterface
  */
 public function run(RequestInterface $request, ResponseInterface $response)
 {
     /** @var \Psr\Http\Message\ServerRequestInterface $request */
     /** @var \Doctrine\ORM\EntityManager $entityManager */
     // data is validated
     $validator = new UserValidator('new', $request, $this->container);
     if (!$validator->validate()) {
         return new ValidationJsonResponse($validator->errors());
     }
     // insert the user
     $entityManager = $this->container->get('EntityManager');
     $data = $request->getParsedBody();
     $user = new User();
     foreach ($data as $field => $value) {
         $user->{'set' . ucfirst($field)}($value);
     }
     $entityManager->persist($user);
     $entityManager->flush();
     return new JsonResponse(['status' => 'created'], 201);
 }
Exemplo n.º 4
0
 /**
  * Invoke middleware
  *
  * @param  RequestInterface  $request  PSR7 request object
  * @param  ResponseInterface $response PSR7 response object
  * @param  callable          $next     Next middleware callable
  *
  * @return ResponseInterface PSR7 response object
  */
 public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
 {
     // Validate POST, PUT, DELETE, PATCH requests
     if (in_array($request->getMethod(), ['POST', 'PUT', 'DELETE', 'PATCH'])) {
         $body = $request->getParsedBody();
         $body = $body ? (array) $body : [];
         $name = isset($body[$this->prefix . '_name']) ? $body[$this->prefix . '_name'] : false;
         $value = isset($body[$this->prefix . '_value']) ? $body[$this->prefix . '_value'] : false;
         if (!$name || !$value || !$this->validateToken($name, $value)) {
             // Need to regenerate a new token, as the validateToken removed the current one.
             $request = $this->generateNewToken($request);
             $failureCallable = $this->getFailureCallable();
             return $failureCallable($request, $response, $next);
         }
     }
     // Generate new CSRF token
     $request = $this->generateNewToken($request);
     // Enforce the storage limit
     $this->enforceStorageLimit();
     return $next($request, $response);
 }
Exemplo n.º 5
0
 public function deleteQueue(Request $request, Response $response, array $args)
 {
     global $database;
     $err = new \Malmanger\Mpmq\Util\ErrorHandler();
     $data = $request->getParsedBody();
     $this->log->debug("deleteQueue args=" . print_r($args, true));
     $queue = new DbQueue($args['id']);
     if (!$queue->queueExists()) {
         $err->addNotFound($args['id']);
     }
     if (!$queue->delete()) {
         $err->addDbUpdate("deleteQueue");
     }
     if ($err->getLevel() > 0) {
         return $err->getErrorResponse($response);
     } else {
         $resp = new \Malmanger\Mpmq\Util\ResponseHandler();
         return $resp->setStatus(204, 'Queue deleted')->getResponse($response);
     }
 }
Exemplo n.º 6
0
 /**
  * Add new message to queue
  *
  * @param Psr\Http\Message\RequestInterface $request
  * @param Psr\Http\Message\ResponseInterface $response 
  * @param array $args
  * 
  * @return Psr\Http\Message\ResponseInterface
  */
 public function newMessage(Request $request, Response $response, array $args)
 {
     global $database;
     $err = new \Malmanger\Mpmq\Util\ErrorHandler();
     $data = $request->getParsedBody();
     $this->log->debug("newMessage data=" . print_r($data, true));
     // Check for mandatory parameters and set defaults
     $id = $args['id'];
     $queue = new DbQueue($id);
     if (!$queue->queueExists()) {
         $err->addNotFound("queue with id=" . $id);
     }
     $messageId = null;
     $title = null;
     $payload = null;
     $timeout = null;
     //        $key = "id";
     //        if (!array_key_exists($key, $data)) {
     //            $err->addMissing($key);
     //        } else {
     //            $id = $data["id"];
     //        }
     $key = "title";
     if (!array_key_exists($key, $data)) {
         $err->addMissing($key);
     } else {
         $title = $data[$key];
     }
     $key = "payload";
     if (array_key_exists($key, $data)) {
         $payload = $data[$key];
     }
     $key = "timeout";
     if (array_key_exists($key, $data)) {
         $timeout = $data[$key];
     } else {
         $timeout = $queue->getTimeout();
     }
     $message = new DbMessage($id, $messageId, $title, $payload, $timeout);
     //        if ($queue->queueExists()) {
     //            $err->addExists($id);
     //        }
     if ($err->getLevel() > 0) {
         return $err->getErrorResponse($response);
     }
     $messageId = $message->save();
     if (!$messageId) {
         $err->addDbUpdate("newMessage");
     }
     if ($err->getLevel() > 0) {
         return $err->getErrorResponse($response);
     } else {
         $data = array();
         $data['id'] = $message->getQueueId();
         $data['messageId'] = $message->getMessageId();
         $data['title'] = $message->getTitle();
         $data['payload'] = $message->getPayload();
         $data['timeout'] = $message->getTimeout();
         $data['inFlight'] = $message->getInFlight();
         $resp = new \Malmanger\Mpmq\Util\ResponseHandler($data);
         return $resp->getResponse($response);
     }
 }