Newer
Older
<?php
/*
* This file is part of Anis Server.
*
* (c) Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Action;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Http\Message\ServerRequestInterface;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use Slim\Exception\HttpUnauthorizedException;
use Slim\Exception\HttpForbiddenException;
/**
* @author François Agneray <francois.agneray@lam.fr>
* @package App\Action
*/
abstract class AbstractAction
{
/**
* The EntityManager is the central access point to Doctrine ORM functionality
*
* @var EntityManagerInterface
*/
protected $em;
/**
* Create the classe before call __invoke to execute the action
*
* @param EntityManagerInterface $em Doctrine Entity Manager Interface
*/
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
/**
* @param string $field
* @param array $parsedBody
*
* @return string true if field is empty or false else
*/
protected function isEmptyField(string $field, array $parsedBody): bool
{
return !isset($parsedBody[$field]);
}
/**
* @param ServerRequestInterface $request PSR-7 This object represents the HTTP request
* @param string $datasetName
* @param array $adminRoles
protected function verifyDatasetAuthorization(
ServerRequestInterface $request,
string $datasetName,
array $adminRoles
) {
$token = $request->getAttribute('token');
if (!$token) {
// The user is not connected (401)
throw new HttpUnauthorizedException($request);
}
$roles = $token->realm_access->roles;
$qb = $this->em->createQueryBuilder();
$qb->select('d.name')
->from('App\Entity\Group', 'g')
->join('g.datasets', 'd')
->where($qb->expr()->in('g.role', $roles))
->andWhere($qb->expr()->eq('d.name', ':dname'));
$qb->setParameter('dname', $datasetName);
$r = $qb->getQuery()->getResult();
if (count($r) < 1) {
throw new HttpForbiddenException(
$request,
'You do not have the permission to access the dataset : ' . $datasetName
);
}
}
}
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* @param ServerRequestInterface $request PSR-7 This object represents the HTTP request
* @param string $instanceName
* @param array $adminRoles
*/
protected function verifyInstanceAuthorization(
ServerRequestInterface $request,
string $instanceName,
array $adminRoles
) {
$token = $request->getAttribute('token');
if (!$token) {
// The user is not connected (401)
throw new HttpUnauthorizedException($request);
}
$roles = $token->realm_access->roles;
if (!$this->isAdmin($adminRoles, $roles)) {
$qb = $this->em->createQueryBuilder();
$qb->select('i.name')
->from('App\Entity\InstanceGroup', 'ig')
->join('ig.instances', 'i')
->where($qb->expr()->in('ig.role', $roles))
->andWhere($qb->expr()->eq('i.name', ':iname'));
$qb->setParameter('iname', $instanceName);
$r = $qb->getQuery()->getResult();
if (count($r) < 1) {
throw new HttpForbiddenException(
$request,
'You do not have the permission to access the instance : ' . $instanceName
);
}
}
}
protected function isAdmin(array $adminRoles, $roles)
{
$admin = false;
for ($i = 0; $i < count($adminRoles); $i++) {
$admin = in_array($adminRoles[$i], $roles);
if ($admin) {
break;
}
}
return $admin;
}