Added GET /users/{user}/permissions

This commit is contained in:
Lukas Metzger 2018-04-01 19:52:00 +02:00
parent c00263e072
commit 706011edd6
4 changed files with 161 additions and 0 deletions

View file

@ -0,0 +1,44 @@
<?php
namespace Controllers;
require '../vendor/autoload.php';
use \Slim\Http\Request as Request;
use \Slim\Http\Response as Response;
class Permissions
{
/** @var \Monolog\Logger */
private $logger;
/** @var \Slim\Container */
private $c;
public function __construct(\Slim\Container $c)
{
$this->logger = $c->logger;
$this->c = $c;
}
public function getList(Request $req, Response $res, array $args)
{
$ac = new \Operations\AccessControl($this->c);
if (!$ac->isAdmin($req->getAttribute('userId'))) {
$this->logger->info('Non admin user tries to get permissions');
return $res->withJson(['error' => 'You must be admin to use this feature'], 403);
}
$paging = new \Utils\PagingInfo($req->getQueryParam('page'), $req->getQueryParam('pagesize'));
$user = intval($args['user']);
$permissions = new \Operations\Permissions($this->c);
$results = $permissions->getPermissions($paging, $user);
return $res->withJson([
'paging' => $paging->toArray(),
'results' => $results
], 200);
}
}

View file

@ -0,0 +1,68 @@
<?php
namespace Operations;
require '../vendor/autoload.php';
/**
* This class provides functions for retrieving and modifying permissions.
*/
class Permissions
{
/** @var \Monolog\Logger */
private $logger;
/** @var \PDO */
private $db;
/** @var \Slim\Container */
private $c;
public function __construct(\Slim\Container $c)
{
$this->logger = $c->logger;
$this->db = $c->db;
$this->c = $c;
}
/**
* Get a list of permissions
*
* @param $pi PageInfo object, which is also updated with total page number
* @param $userId Id of the user for which the permissions should be retrieved
*
* @return array Array with matching permissions
*/
public function getPermissions(\Utils\PagingInfo &$pi, int $userId) : array
{
//Count elements
if ($pi->pageSize === null) {
$pi->totalPages = 1;
} else {
$query = $this->db->prepare('SELECT COUNT(*) AS total FROM permissions WHERE user_id=:userId');
$query->bindValue(':userId', $userId, \PDO::PARAM_INT);
$query->execute();
$record = $query->fetch();
$pi->totalPages = ceil($record['total'] / $pi->pageSize);
}
$pageStr = \Services\Database::makePagingString($pi);
$query = $this->db->prepare('
SELECT P.domain_id as domainId,D.name as domainName FROM permissions P
LEFT OUTER JOIN domains D ON D.id=P.domain_id
WHERE P.user_id=:userId'
. $pageStr);
$query->bindValue(':userId', $userId, \PDO::PARAM_INT);
$query->execute();
$data = $query->fetchAll();
return $data;
}
}

View file

@ -47,6 +47,8 @@ $app->group('/v1', function () {
$this->delete('/records/{recordId}/credentials/{credentialId}', '\Controllers\Credentials:delete');
$this->get('/records/{recordId}/credentials/{credentialId}', '\Controllers\Credentials:getSingle');
$this->put('/records/{recordId}/credentials/{credentialId}', '\Controllers\Credentials:put');
$this->get('/users/{user}/permissions', '\Controllers\Permissions:getList');
})->add('\Middlewares\Authentication');
});

View file

@ -0,0 +1,47 @@
const test = require('../testlib');
test.run(async function () {
await test('admin', async function (assert, req) {
//Test paging
var res = await req({
url: '/users/2/permissions?pagesize=1&page=2',
method: 'get'
});
assert.equal(res.status, 200, 'Status should be OK');
assert.equal(res.data.paging, {
page: 2,
total: 2,
pagesize: 1
}, 'Paging data fail for ' + res.config.url);
assert.equal(res.data.results.length, 1, "Should be 1 results.");
var res = await req({
url: '/users/2/permissions',
method: 'get'
});
assert.equal(res.status, 200, 'Get of permissions should be OK');
assert.equal(res.data.results, [
{
domainId: '1',
domainName: 'example.com'
},
{
domainId: '2',
domainName: 'slave.example.net'
}
], 'Get permissions result fail');
});
await test('user', async function (assert, req) {
var res = await req({
url: '/users/2/permissions',
method: 'get'
});
assert.equal(res.status, 403, 'Get of permissions should fail for user.');
});
});