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

[Core] Add LorisInstance class #6118

Merged
merged 2 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/dashboard/php/dashboard.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class Dashboard extends \NDB_Form
$DB = \NDB_Factory::singleton()->database();
$user = \User::singleton();

// FIXME: This should use \LORIS\LorisInstance->getActiveModules(), but there
// is no access to a LORISInstance object at this point in the code.
$this->modules = \Module::getActiveModules($DB);

$this->smallwidgets = [];
Expand Down
107 changes: 107 additions & 0 deletions src/LorisInstance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php declare(strict_types=1);

namespace LORIS;

/**
* A LorisInstance represents an installed instance of a LORIS
* project.
*
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
*/
class LorisInstance
{
protected $modulesDirs;
private $DB;

/**
* Construct a LORISInstance for the install connected to $db
* which uses modules from $moduleDirs.
*
* @param \Database $db A database connection to this
* instance.
* @param string[] $moduleDirs A list of directories that may
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice to use SPLFileInfo instead especially if the main purpose of this property is to do validation based on the filepaths.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not the main purpose. The main purpose is to have a list of directories that modules may be found in.

* contain modules for this instance.
*/
public function __construct(\Database $db, array $modulesDirs)
{
$this->DB = $db;
$this->modulesDirs = $modulesDirs;
}

/**
* Return an active database connection to this instance.
*
* @return \Database
*/
public function getDatabaseConnection() : \Database
{
return $this->DB;
}

/**
* Return a list of directories on the filesystem which
* may contain modules.
*
* @return string[]
*/
private function getModulesDirs() : array
{
return $this->modulesDirs;
}

/**
* Retrieve all active module descriptors from the given database.
*
* @param \Database $db an open connection to a database containing a 'modules'
* table
*
* @return \Module[]
*/
public function getActiveModules(): array
{
$mnames = $this->getDatabaseConnection()->pselectCol(
"SELECT Name FROM modules WHERE Active='Y'",
[]
);

$modules = [];
foreach ($mnames as $name) {
try {
$modules[] = \Module::factory($name);
} catch (\LorisModuleMissingException $e) {
error_log($e->getMessage() . " " . $e->getTraceAsString());
}
}
return $modules;
}

/**
* Return true if the LORISInstance has a module named
* $name. To be installed it must be both available on
* the filesystem, and active in the modules table.
driusan marked this conversation as resolved.
Show resolved Hide resolved
*
* @return bool
*/
public function hasModule(string $name) : bool
{
$dirs = $this->getModulesDirs();
$found = false;
foreach ($dirs as $subdir) {
if (is_dir($subdir . "/" . $name)) {
$found = true;
break;
}
}

if ($found === false) {
return false;
}

foreach ($this->getActiveModules() as $module) {
if ($module->getName() == $name) {
return true;
}
}
return false;
}
}
11 changes: 5 additions & 6 deletions src/Middleware/UserPageDecorationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public function __construct(
string $pagename,
\NDB_Config $config,
array $JS,
array $CSS,
\Database $DB
array $CSS
) {

$this->JSFiles = $JS;
Expand All @@ -32,7 +31,6 @@ public function __construct(
$this->BaseURL = $baseurl;
$this->PageName = $pagename;
$this->user = $user;
$this->DB = $DB;
}

/**
Expand All @@ -48,14 +46,15 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
{
ob_start();
// Set the page template variables
// $user is set by the page base router
// $user and $loris is set by the page base router
$user = $request->getAttribute("user");
$loris = $request->getAttribute("loris");
$tpl_data = array(
'test_name' => $this->PageName,
);
$menu = [];

$menu = [];
$modules = \Module::getActiveModules($this->DB);
$modules = $loris->getActiveModules();
foreach ($modules as $module) {
if (!$module->hasAccess($user)) {
continue;
Expand Down
27 changes: 16 additions & 11 deletions src/Router/BaseRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
*/
class BaseRouter extends PrefixRouter implements RequestHandlerInterface
{
protected $projectdir;
protected $moduledir;
protected $lorisinstance;
protected $user;

/**
Expand All @@ -44,9 +43,14 @@ class BaseRouter extends PrefixRouter implements RequestHandlerInterface
*/
public function __construct(\User $user, string $projectdir, string $moduledir)
{
$this->user = $user;
$this->projectdir = $projectdir;
$this->moduledir = $moduledir;
$this->user = $user;
$this->lorisinstance = new \LORIS\LorisInstance(
\NDB_Factory::singleton()->database(),
[
$projectdir . "/modules",
$moduledir,
]
);
}

/**
Expand All @@ -66,7 +70,9 @@ public function handle(ServerRequestInterface $request) : ResponseInterface
// Remove any trailing slash remaining, so that foo/ and foo are the same
// route
$path = preg_replace("/\/$/", "", $path);
$request = $request->withAttribute("user", $this->user);
$request = $request->withAttribute("user", $this->user)
->withAttribute("loris", $this->lorisinstance);

if ($path == "") {
if ($this->user instanceof \LORIS\AnonymousUser) {
$modulename = "login";
Expand All @@ -84,9 +90,9 @@ public function handle(ServerRequestInterface $request) : ResponseInterface
$components = preg_split("/\/+?/", $path);
$modulename = $components[0];
}
if (is_dir($this->moduledir . "/" . $modulename)
|| is_dir($this->projectdir . "/modules/" . $modulename)
) {

$factory = \NDB_Factory::singleton();
if ($this->lorisinstance->hasModule($modulename)) {
$uri = $request->getURI();
$suburi = $this->stripPrefix($modulename, $uri);

Expand All @@ -96,7 +102,6 @@ public function handle(ServerRequestInterface $request) : ResponseInterface
$baseurl = $uri->withPath($baseurl)->withQuery("");
$request = $request->withAttribute("baseurl", $baseurl->__toString());

$factory = \NDB_Factory::singleton();
$factory->setBaseURL($baseurl);

$module = \Module::factory($modulename);
Expand All @@ -112,8 +117,8 @@ public function handle(ServerRequestInterface $request) : ResponseInterface
$path = $uri->getPath();
$baseurl = $uri->withPath("/")->withQuery("");

$factory = \NDB_Factory::singleton();
$factory->setBaseURL($baseurl);

switch (count($components)) {
case 1:
$request = $request
Expand Down