Skip to content
New issue

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

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

Already on GitHub? # to your account

[Core] PSCID uniqueness #8411

Merged
merged 6 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions modules/api/php/endpoints/candidates.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,14 @@ class Candidates extends Endpoint implements \LORIS\Middleware\ETagCalculator
$pscid,
$project->getId()
);
} catch (\LorisException | \InvalidArgumentException $e) {
return new \LORIS\Http\Response\JSON\BadRequest($e->getMessage());
} catch (\ConflictException $e) {
driusan marked this conversation as resolved.
Show resolved Hide resolved
return new \LORIS\Http\Response\JSON\Conflict(
$e->getMessage()
);
} catch (\Exception | \LorisException | \InvalidArgumentException $e) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

When is a \LorisException or an \InvalidArgumentException not an \Exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When PSCID is not provided and generation is user

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean that by adding \Exception I should remove the other two because they are subclasses?

Copy link
Collaborator

Choose a reason for hiding this comment

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

That was my question, yes.

return new \LORIS\Http\Response\JSON\BadRequest(
$e->getMessage()
);
}

$candidate = \NDB_Factory::singleton()->candidate($candid);
Expand Down
11 changes: 11 additions & 0 deletions php/exceptions/ConflictException.class.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* This file contains the Conflict exception type.
*
* PHP Version 7
*
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
*/
class ConflictException extends LorisException
{
}
20 changes: 20 additions & 0 deletions php/libraries/Candidate.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ class Candidate implements \LORIS\StudyEntities\AccessibleResource,
ProjectID $registrationProjectID = null
): CandID {
$factory = NDB_Factory::singleton();
$db = $factory->database();

$site = \Site::singleton($centerID);

Expand Down Expand Up @@ -264,6 +265,25 @@ class Candidate implements \LORIS\StudyEntities\AccessibleResource,
);
}

// check pscid uniqueness
$existing = $db->pselectOne(
'SELECT
COUNT(*)
FROM candidate
WHERE PSCID = :v_pscid
GROUP BY
PSCID
',
['v_pscid' => $PSCID]
);

if ($existing > 0) {
throw new \ConflictException(
"PSCID must be unique",
PSCID_NOT_UNIQUE
);
}

// check pscid structure
if (!Candidate::validatePSCID(
$PSCID,
Expand Down