Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[electrophysiology_browser] Session page: Add Project Permissions check #6640

59 changes: 34 additions & 25 deletions modules/electrophysiology_browser/php/sessions.class.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php declare(strict_types=1);

/**
* This class features the code for the menu portion of the LORIS
* electrophysiology browser module.
Expand Down Expand Up @@ -37,6 +36,7 @@ class Sessions extends \NDB_Page
public $skipTemplate = true; // stops from looking for a smarty template
protected $timepoint;
protected $sessionID;
protected $candidate;

/**
* Determine whether the user has permission to view this page
Expand All @@ -47,51 +47,60 @@ class Sessions extends \NDB_Page
*/
function _hasAccess(\User $user) : bool
{
return ($user->hasPermission('electrophysiology_browser_view_allsites')
return (($user->hasPermission('electrophysiology_browser_view_allsites')
|| ($user->hasCenter($this->timepoint->getCenterID())
&& $user->hasPermission('electrophysiology_browser_view_site')
)
);
&& $user->hasPermission('electrophysiology_browser_view_site'))
) && $user->hasProject($this->timepoint->getProject()->getId()));
}

/**
* Handles a login request
* Load the required variables in order to check that the user
* has access to the session.
*
* @param ServerRequestInterface $request The incoming PSR7 request
* @param \User $user The user to load the resources for
* @param ServerRequestInterface $request The PSR15 Request being handled
*
* @return ResponseInterface The outgoing PSR7 response
* @throws \NotFound If the session id is non-numerical
* @throws \LorisException If the session is not found
*
* @return void
*/
public function handle(ServerRequestInterface $request) : ResponseInterface
{
$path = $request->getUri()->getPath();
$parameters = $request->getQueryParams();
$user = $request->getAttribute('user');
public function loadResources(
\User $user, ServerRequestInterface $request
) : void {

$path = $request->getUri()->getPath();
$matches = [];

// check that the session ID is of type integer
if (preg_match('#/sessions/(\d+)#', $path, $matches) !== 1) {
driusan marked this conversation as resolved.
Show resolved Hide resolved
return (new \LORIS\Middleware\PageDecorationMiddleware($user))
->process(
$request,
new \LORIS\Http\StringStream("Invalid session")
)->withStatus(404);
throw new \NotFound("Invalid session");
}

$session_id = intval($matches[1]);

try {
$this->timepoint = \NDB_Factory::singleton()->timepoint(
$session_id
);
$this->sessionID = $session_id;
} catch(\LorisException $e) {
return (new \LORIS\Middleware\PageDecorationMiddleware($user))
->process(
$request,
new \LORIS\Http\StringStream("Session not found")
)->withStatus(404);
throw new \NotFound("Session not found");
}

parent::loadResources($user, $request);
}

/**
* Handles a login request
*
* @param ServerRequestInterface $request The incoming PSR7 request
*
* @return ResponseInterface The outgoing PSR7 response
*/
public function handle(ServerRequestInterface $request) : ResponseInterface
{
$parameters = $request->getQueryParams();
$user = $request->getAttribute('user');

if (!$this->_hasAccess($user)) {
return (new \LORIS\Middleware\PageDecorationMiddleware($user))
->process(
Expand Down
2 changes: 1 addition & 1 deletion php/libraries/Module.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ abstract class Module extends \LORIS\Router\PrefixRouter
} else {
$_REQUEST['subtest'] = $pagename;
}

$page->loadResources($user, $request);
if ($page->_hasAccess($user) !== true) {
return (new \LORIS\Middleware\PageDecorationMiddleware(
$user
Expand Down
14 changes: 14 additions & 0 deletions php/libraries/NDB_Page.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,20 @@ class NDB_Page implements RequestHandlerInterface
->withBody(new \LORIS\Http\StringStream($this->display() ?? ""));
}

/**
* This function can be overridden in a module's page to load the necessary
* resources to check the permissions of a user.
*
* @param User $user The user to load the resources for
* @param ServerRequestInterface $request The PSR15 Request being handled
*
* @return void
*/
public function loadResources(
\User $user, ServerRequestInterface $request
) : void {
}

/**
* Displays the form
*
Expand Down