FOSSology  4.7.1
Open Source License Compliance by Open Source Software
RestController.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2018 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
15 namespace Fossology\UI\Api\Controllers;
16 
22 use Psr\Container\ContainerInterface;
23 use Psr\Http\Message\ServerRequestInterface;
24 
30 {
35  protected $container;
36 
41  protected $restHelper;
42 
47  protected $dbHelper;
48 
53  public function __construct($container)
54  {
55  $this->container = $container;
56  $this->restHelper = $this->container->get('helper.restHelper');
57  $this->dbHelper = $this->restHelper->getDbHelper();
58  }
59 
69  protected function getParsedBody(ServerRequestInterface $request)
70  {
71  if ($this->isJsonRequest($request)) {
72  $content = $request->getBody()->getContents();
73  return json_decode($content, true);
74  } else {
75  // application/x-www-form-urlencoded or multipart/form-data
76  return $request->getParsedBody();
77  }
78  }
79 
85  protected function throwNotAdminException(): void
86  {
87  if (!Auth::isAdmin()) {
88  throw new HttpForbiddenException("Only admin can access this endpoint.");
89  }
90  }
91 
99  protected function uploadAccessible($id): void
100  {
101  if (! $this->dbHelper->doesIdExist("upload", "upload_pk", $id)) {
102  throw new HttpNotFoundException("Upload does not exist");
103  }
104  if (! $this->restHelper->getUploadDao()->isAccessible($id,
105  $this->restHelper->getGroupId())) {
106  throw new HttpForbiddenException("Upload is not accessible");
107  }
108  }
109 
121  protected function uploadEditable($id): void
122  {
123  $this->uploadAccessible($id);
124  if (! $this->restHelper->getUploadDao()->isEditable($id,
125  $this->restHelper->getGroupId())) {
126  throw new HttpForbiddenException(
127  "You do not have write permission on this upload.");
128  }
129  }
130 
139  protected function isItemExists(int $uploadId, int $itemId): void
140  {
141  $uploadDao = $this->restHelper->getUploadDao();
142  $item = $uploadDao->getUploadEntry($itemId,
143  $uploadDao->getUploadtreeTableName($uploadId));
144  if (empty($item) || intval($item['upload_fk']) !== $uploadId) {
145  throw new HttpNotFoundException("Item does not exist");
146  }
147  }
148 
154  public function isJsonRequest($request)
155  {
156  return strcasecmp($request->getHeaderLine('Content-Type'),
157  "application/json") === 0;
158  }
159 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
static isAdmin()
Check if user is admin.
Definition: Auth.php:92
Base controller for REST calls.
isItemExists(int $uploadId, int $itemId)
getParsedBody(ServerRequestInterface $request)
Parse request body as JSON and return associative PHP array.
Provides helper methods to access database for REST api.
Definition: DbHelper.php:38
Provides various DAO helper functions for REST api.
Definition: RestHelper.php:32