Skip to content

Commit 783e387

Browse files
committed
[Instrument] Remove exception from NDB_BVL_Instrument::factory
This removes the exception from NDB_BVL_Instrument::factory(), so that it's possible to call hasAccess on an instrument. Currently the hasAccess or Factory call mix concerns, making it difficult for a module to check if a user has access to an instrument.
1 parent a286050 commit 783e387

File tree

8 files changed

+58
-19
lines changed

8 files changed

+58
-19
lines changed

htdocs/survey.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,16 @@ function initialize()
103103
throw new Exception("Data has already been submitted.", 403);
104104
}
105105

106-
$instrumentObj = \NDB_BVL_Instrument::factory(
106+
$instrumentObj = \NDB_BVL_Instrument::factory(
107107
$this->loris,
108108
$this->TestName,
109109
);
110+
111+
$user = \User::singleton();
112+
if ($instrumentObj->_hasAccess($user) !== true) {
113+
throw new \Exception("Permission denied", 403);
114+
}
115+
110116
$subtests = $instrumentObj->getSubtestList();
111117
$this->NumPages = count($subtests) + 1;
112118

modules/api/php/endpoints/candidate/visit/instruments.class.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ class Instruments extends Endpoint implements \LORIS\Middleware\ETagCalculator
131131
'',
132132
true
133133
);
134+
135+
$user = $request->getAttribute('user');
136+
if ($instrument->_hasAccess($user) == false) {
137+
return new \LORIS\Http\Response\JSON\Forbidden();
138+
};
134139
} catch (\Exception $e) {
135140
return new \LORIS\Http\Response\JSON\NotFound();
136141
}

modules/api/php/endpoints/project/instruments.class.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ class Instruments extends Endpoint implements \LORIS\Middleware\ETagCalculator
108108
'',
109109
true
110110
);
111+
112+
$user = $request->getAttribute("user");
113+
if ($instrument->_hasAccess($user) == false) {
114+
return new \LORIS\Http\Response\JSON\Forbidden();
115+
}
111116
} catch (\Exception $e) {
112117
return new \LORIS\Http\Response\JSON\NotFound();
113118
}

modules/conflict_resolver/php/endpoints/unresolved.class.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ class Unresolved extends Endpoint implements ETagCalculator
252252
'',
253253
true
254254
);
255+
if ($instrument->_hasAccess($user) == false) {
256+
return new \LORIS\Http\Response\JSON\NotFound(
257+
'Permission denied for ' . $conflict['TableName']
258+
);
259+
}
255260
$instrument->_saveValues(
256261
[$conflict['FieldName'] => $conflict['CorrectAnswer']]
257262
);

modules/datadict/php/datadict.class.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class Datadict extends \DataFrameworkMenu
6161
$instruments = \Utility::getAllInstruments();
6262
$dictInstruments = [];
6363
$lorisinstance = $this->lorisinstance;
64+
65+
$user = \User::singleton();
6466
foreach ($instruments as $instrument => $name) {
6567
// Since the testname does not always match the table name in the
6668
// database we need to instantiate the object to get the table name
@@ -71,6 +73,13 @@ class Datadict extends \DataFrameworkMenu
7173
'',
7274
''
7375
);
76+
if ($iObj->_hasAccess($user)) {
77+
$this->logger->debug(
78+
"Skipping $instrument in field options"
79+
. " because user does not have permission"
80+
);
81+
continue;
82+
}
7483
} catch (\Exception $e) {
7584
error_log(
7685
"There was a problem instantiating the instrument ".

modules/instruments/php/module.class.inc

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ class Module extends \Module
8484
$page
8585
);
8686

87+
$user = $request->getAttribute("user");
88+
if ($instrument->_hasAccess($user) == false) {
89+
return (new \Laminas\Diactoros\Response())
90+
->withStatus(403)
91+
->withBody(new \LORIS\Http\StringStream("Permission denied"));
92+
}
93+
8794
$request = $request->withAttribute('pageclass', $instrument);
8895

8996
return $instrument->process($request, $instrument);
@@ -165,13 +172,23 @@ class Module extends \Module
165172
$tests = array_keys(\Utility::getAllInstruments());
166173

167174
$dict = [];
175+
$user = \User::singleton();
168176
foreach ($tests as $testname) {
169177
try {
170-
$inst = \NDB_BVL_Instrument::factory($loris, $testname, "", "");
171-
$cat = new \LORIS\Data\Dictionary\Category(
178+
$inst = \NDB_BVL_Instrument::factory($loris, $testname, "", "");
179+
if ($inst->_hasAccess($user) == false) {
180+
$this->logger->debug(
181+
"Skipping dictionary for $testname"
182+
. " due to permissions."
183+
);
184+
continue;
185+
}
186+
187+
$cat = new \LORIS\Data\Dictionary\Category(
172188
$testname,
173189
$inst->getFullName()
174190
);
191+
175192
$fields = $inst->getDataDictionary();
176193
$dict[] = $cat->withItems($fields);
177194
} catch (\LorisException $e) {

modules/instruments/php/visitsummary.class.inc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class VisitSummary extends \NDB_Page
5252
return $this->_handleGET(
5353
new CandID($params['CandID']),
5454
$params['VisitLabel'],
55+
$user,
5556
);
5657
default:
5758
return new \LORIS\Http\Response\JSON\MethodNotAllowed(
@@ -61,15 +62,16 @@ class VisitSummary extends \NDB_Page
6162
}
6263

6364
/**
64-
* Helper to specifically handle PATCH HTTP methods to the endpoint.
65+
* Helper to specifically handle GET HTTP methods to the endpoint.
6566
*
6667
* @param CandID $candid The CandID for the visit.
6768
* @param string $visitLabel The visit label string.
69+
* @param \User $user The user accessing the endpoint
6870
*
6971
* @return ResponseInterface
7072
*/
7173
private function _handleGET(
72-
CandID $candid, string $visitLabel
74+
CandID $candid, string $visitLabel, \User $user
7375
) : ResponseInterface {
7476
$DB = \NDB_Factory::singleton()->database();
7577

@@ -111,6 +113,9 @@ class VisitSummary extends \NDB_Page
111113
'',
112114
false
113115
);
116+
if ($instrument->_hasAccess($user) == false) {
117+
continue;
118+
}
114119
if ($instrument === null) {
115120
$bvl_result[$key]['Completion'] = 0;
116121
} else {

php/libraries/NDB_BVL_Instrument.class.inc

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -308,17 +308,6 @@ abstract class NDB_BVL_Instrument extends NDB_Page
308308
$base . "project/instruments/$instrument.rules"
309309
);
310310

311-
$user = \User::singleton();
312-
$access = $obj->_hasAccess($user);
313-
314-
// check that user has access
315-
if ($access == false) {
316-
throw new Exception(
317-
"You do not have access to this page.",
318-
403
319-
);
320-
}
321-
322311
return $obj;
323312
}
324313

@@ -380,9 +369,7 @@ abstract class NDB_BVL_Instrument extends NDB_Page
380369
return true;
381370
}
382371
}
383-
384-
//no user permissions match required instrument permissions
385-
throw new Exception("You do not have access to this page.", 403);
372+
return false;
386373
}
387374
}
388375

0 commit comments

Comments
 (0)