FOSSology  4.5.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 
118  protected function isItemExists(int $uploadId, int $itemId): void
119  {
120  if (!$this->dbHelper->doesIdExist(
121  $this->restHelper->getUploadDao()->getUploadtreeTableName($uploadId),
122  "uploadtree_pk", $itemId)) {
123  throw new HttpNotFoundException("Item does not exist");
124  }
125  }
126 
132  public function isJsonRequest($request)
133  {
134  return strcasecmp($request->getHeaderLine('Content-Type'),
135  "application/json") === 0;
136  }
137 }
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