FOSSology  4.4.0
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 (strcasecmp($request->getHeaderLine('Content-Type'),
72  "application/json") === 0) {
73  $content = $request->getBody()->getContents();
74  return json_decode($content, true);
75  } else {
76  // application/x-www-form-urlencoded or multipart/form-data
77  return $request->getParsedBody();
78  }
79  }
80 
86  protected function throwNotAdminException(): void
87  {
88  if (!Auth::isAdmin()) {
89  throw new HttpForbiddenException("Only admin can access this endpoint.");
90  }
91  }
92 
100  protected function uploadAccessible($id): void
101  {
102  if (! $this->dbHelper->doesIdExist("upload", "upload_pk", $id)) {
103  throw new HttpNotFoundException("Upload does not exist");
104  }
105  if (! $this->restHelper->getUploadDao()->isAccessible($id,
106  $this->restHelper->getGroupId())) {
107  throw new HttpForbiddenException("Upload is not accessible");
108  }
109  }
110 
119  protected function isItemExists(int $uploadId, int $itemId): void
120  {
121  if (!$this->dbHelper->doesIdExist(
122  $this->restHelper->getUploadDao()->getUploadtreeTableName($uploadId),
123  "uploadtree_pk", $itemId)) {
124  throw new HttpNotFoundException("Item does not exist");
125  }
126  }
127 }
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