Ejemplo n.º 1
0
 /**
  * Init
  *
  * @return void
  */
 public function init()
 {
     $this->autoload_files();
     if (craft()->config->get('paginationParameter', 'restfulApi') === craft()->config->get('pageTrigger')) {
         $exception = new RestfulApiException();
         $exception->setMessage('The `paginationParameter` cannot be the same as `pageTrigger`.');
         throw $exception;
     }
 }
 /**
  * Save Element
  *
  * @param array $params Parameters
  *
  * @return BaseElementModel $model
  */
 public function saveElement(BaseElementModel $element, Request $request)
 {
     $element_type = craft()->elements->getElementType($element->getElementType());
     $result = $element_type->saveElement($element, null);
     if (!$result) {
         $exception = new RestfulApiException();
         $exception->setStatus(400)->setMessage('Element could not be stored.');
         throw $exception;
     }
     craft()->content->saveContent($element);
     return $element;
 }
Ejemplo n.º 3
0
 /**
  * Set Error
  *
  * @param RestfulApiException $exception Exception
  *
  * @return Response Response
  */
 public function setError(RestfulApiException $exception)
 {
     $body = ['error' => ['message' => $exception->getMessage()]];
     if ($exception->hasErrors()) {
         $body['error']['errors'] = $exception->getErrors();
     }
     if (\Craft\craft()->config->get('devMode', 'restfulApi')) {
         if ($exception->hasInput()) {
             $body['error']['input'] = $exception->getInput();
         }
         $body['error']['debug'] = $exception->getTrace();
     }
     $this->transformer = \Craft\craft()->config->get('exceptionTransformer', 'restfulApi');
     $this->item = $body;
     return $this;
 }
Ejemplo n.º 4
0
 /**
  * Validate Element Permission
  *
  * @param Request   $request          Request
  * @param UserModel $user             User
  * @param bool      $is_authenticated Is Authenticated
  *
  * @return null|RestfulApiException
  */
 protected function validateElementPermission(Request $request, UserModel $user = null, $is_authenticated = false)
 {
     $element_permissions = \Craft\craft()->restfulApi_config->getElementPermissions($request->getAttribute('elementType'));
     if ($is_authenticated && in_array($request->getMethod(), $element_permissions['authenticated'])) {
         return;
     }
     if (in_array($request->getMethod(), $element_permissions['public'])) {
         return;
     }
     $exception = new RestfulApiException();
     $exception->setStatus(401)->setMessage(sprintf('User is not authorized to perform method `%s` on `%s` element type.', $request->getMethod(), $request->getAttribute('elementType')));
     throw $exception;
 }
 /**
  * Get Element Permissions
  *
  * @param string $element_type Element Type
  *
  * @return array Element Permissions
  */
 public function getElementPermissions($element_type)
 {
     $element_permissions = $this->getElementTypeConfig($element_type, 'permissions');
     if (!$element_permissions) {
         $element_permissions = $this->getElementPermissions('*');
     }
     if (!$element_permissions) {
         $exception = new RestfulApiException();
         $exception->setStatus(415)->setMessage(sprintf('Permissions for the `%s` element type is not defined.', $element_type));
         throw $exception;
     }
     return $element_permissions;
 }
 /**
  * Resource Router
  *
  * @param array $variables Variables
  *
  * @return void
  */
 public function actionResourceRouter(array $variables = [])
 {
     try {
         $this->dispatcher->handle($this, $variables);
         return $this->response->send();
     } catch (RestfulApiException $exception) {
         $exception->setInput($this->request->getParsedBody());
         $response = new Response();
         return $response->setStatus($exception->getStatusCode(), $exception->getStatusPhrase())->setError($exception)->send();
     } catch (CDbException $CDbException) {
         $exception = new RestfulApiException();
         $exception->setMessage($CDbException->getMessage());
         $response = new Response();
         return $response->setError($exception)->send();
     } catch (\Craft\Exception $craftException) {
         $exception = new RestfulApiException();
         $exception->setMessage($craftException->getMessage());
         $response = new Response();
         return $response->setError($exception)->send();
     }
 }