Skip to content

Commit

Permalink
pkp#52 added *_permission_set for owner and submitter
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed May 6, 2024
1 parent ecf0456 commit d52111a
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 41 deletions.
52 changes: 47 additions & 5 deletions IThenticate.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ class IThenticate
'GROUP_ATTACHMENT_COMPLETE',
];

/**
* The list of valid permission for owner and submitter when creating a new submission
* @see https://developers.turnitin.com/docs/tca#create-a-submission
*
* @var array
*/
public const SUBMISSION_PERMISSION_SET = [
'ADMINISTRATOR',
'APPLICANT',
'EDITOR',
'INSTRUCTOR',
'LEARNER',
'UNDEFINED',
'USER',
];

/**
* The entity(e.g. submission owner, submitter etc) to id prefix mapping
* This helps to identify the type of entity associated with requesting system
Expand Down Expand Up @@ -239,15 +255,27 @@ public function confirmEula($user, $context) {
* Create a new submission at service's end
* @see https://developers.turnitin.com/docs/tca#create-a-submission
*
* @param Submission $submission The article submission to check for plagiarism
* @param User $user The user who is making submitting the submission
* @param Author $author The author/owher of the submission
* @param Site $site The core site of submission system
* @param Site $site The core site of submission system
* @param Submission $submission The article submission to check for plagiarism
* @param User $user The user who is making submitting the submission
* @param Author $author The author/owner of the submission
* @param string $authorPermission Submission author/owner permission set
* @param string $submitterPermission Submission submitter permission set
*
* @return string|null if succeed, it will return the created submission UUID from
* service's end and at failure, will return null
*
* @throws \Exception
*/
public function createSubmission($submission, $user, $author, $site) {
public function createSubmission($site, $submission, $user, $author, $authorPermission, $submitterPermission) {

if (!$this->validatePermission($authorPermission, static::SUBMISSION_PERMISSION_SET)) {
throw new \Exception("in valid owner permission {$authorPermission} given");
}

if (!$this->validatePermission($submitterPermission, static::SUBMISSION_PERMISSION_SET)) {
throw new \Exception("in valid submitter permission {$submitterPermission} given");
}

$publication = $submission->getCurrentPublication(); /** @var Publication $publication */
$author ??= $publication->getPrimaryAuthor();
Expand All @@ -263,6 +291,8 @@ public function createSubmission($submission, $user, $author, $site) {
'owner' => $this->getGeneratedId('owner', $author->getId()),
'title' => $publication->getLocalizedTitle($publication->getData('locale')),
'submitter' => $this->getGeneratedId('submitter', $user->getId()),
'owner_default_permission_set' => $authorPermission,
'submitter_default_permission_set' => $submitterPermission,
'metadata' => [
'owners' => [
[
Expand Down Expand Up @@ -627,4 +657,16 @@ protected function getGeneratedId($entity, $id, $silent = true) {

return static::ENTITY_ID_PREFIXES[$entity] . $id;
}

/**
* Validate the existence of a permission against a given permission set
*
* @param string $permission The specific permission to check for existence
* @param array $permissionSet The permission list to check against
*
* @return bool True/False if the permission exists in the given permission set
*/
protected function validatePermission($permission, $permissionSet) {
return in_array($permission, $permissionSet);
}
}
95 changes: 66 additions & 29 deletions PlagiarismPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class PlagiarismPlugin extends GenericPlugin {
*/
public const PLUGIN_INTEGRATION_NAME = 'Plagiarism plugin for OJS/OMP/OPS';

/**
* The default permission of submission primary author's to pass to the iThenticate service
*/
public const SUBMISSION_AUTOR_ITHENTICATE_DEFAULT_PERMISSION = 'USER';

/**
* Number of seconds EULA details for a context should be cached before refreshing it
*/
Expand Down Expand Up @@ -454,36 +459,43 @@ public function submitForPlagiarismCheck($hookName, $args) {
'submissionIds' => [$submission->getId()],
]);

foreach($submissionFiles as $submissionFile) { /** @var SubmissionFile $submissionFile */
// Create a new submission at iThenticate's end
$submissionUuid = $ithenticate->createSubmission(
$submission,
$user,
$author,
$request->getSite()
);

if (!$submissionUuid) {
$this->sendErrorMessage($submission->getId(), "Could not create the submission at iThenticate for file id {$submissionFile->getId()}");
return false;
}

$file = Services::get('file')->get($submissionFile->getData('fileId'));
$uploadStatus = $ithenticate->uploadFile(
$submissionUuid,
$submissionFile->getData("name", $publication->getData("locale")),
Services::get('file')->fs->read($file->path),
);

// Upload submission files for successfully created submission at iThenticate's end
if (!$uploadStatus) {
$this->sendErrorMessage($submission->getId(), 'Could not complete the file upload at iThenticate for file id ' . $submissionFile->getData("name", $publication->getData("locale")));
return false;
try {
foreach($submissionFiles as $submissionFile) { /** @var SubmissionFile $submissionFile */
// Create a new submission at iThenticate's end
$submissionUuid = $ithenticate->createSubmission(
$request->getSite(),
$submission,
$user,
$author,
static::SUBMISSION_AUTOR_ITHENTICATE_DEFAULT_PERMISSION,
$this->getSubmitterPermission($context, $user)
);

if (!$submissionUuid) {
$this->sendErrorMessage($submission->getId(), "Could not create the submission at iThenticate for file id {$submissionFile->getId()}");
return false;
}

$file = Services::get('file')->get($submissionFile->getData('fileId'));
$uploadStatus = $ithenticate->uploadFile(
$submissionUuid,
$submissionFile->getData("name", $publication->getData("locale")),
Services::get('file')->fs->read($file->path),
);

// Upload submission files for successfully created submission at iThenticate's end
if (!$uploadStatus) {
$this->sendErrorMessage($submission->getId(), 'Could not complete the file upload at iThenticate for file id ' . $submissionFile->getData("name", $publication->getData("locale")));
return false;
}

$submissionFile->setData('ithenticate_id', $submissionUuid);
$submissionFile->setData('ithenticate_similarity_scheduled', 0);
$submissionFileDao->updateObject($submissionFile);
}

$submissionFile->setData('ithenticate_id', $submissionUuid);
$submissionFile->setData('ithenticate_similarity_scheduled', 0);
$submissionFileDao->updateObject($submissionFile);
} catch (\Throwable $exception) {
$this->sendErrorMessage($submission->getId(), $exception->getMessage());
return false;
}

return true;
Expand Down Expand Up @@ -743,4 +755,29 @@ protected function stampEulaVersionToSubmittingUser($user, $version) {

$userDao->updateObject($user);
}

/**
* Get the submission submitter's appropriate permission based on role in the submission context
*
* @param Context $context
* @param User $user
*
* @return string
*/
protected function getSubmitterPermission($context, $user) {

if ($user->hasRole([ROLE_ID_SITE_ADMIN, ROLE_ID_MANAGER], $context->getId())) {
return 'ADMINISTRATOR';
}

if ($user->hasRole([ROLE_ID_SUB_EDITOR], $context->getId())) {
return 'EDITOR';
}

if ($user->hasRole([ROLE_ID_AUTHOR], $context->getId())) {
return 'USER';
}

return 'UNDEFINED';
}
}
56 changes: 49 additions & 7 deletions TestIThenticate.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ class TestIThenticate {
'GROUP_ATTACHMENT_COMPLETE',
];

/**
* The list of valid permission for owner and submitter when creating a new submission
* @see https://developers.turnitin.com/docs/tca#create-a-submission
*
* @var array
*/
public const SUBMISSION_PERMISSION_SET = [
'ADMINISTRATOR',
'APPLICANT',
'EDITOR',
'INSTRUCTOR',
'LEARNER',
'UNDEFINED',
'USER',
];

/**
* Create a new instance
*
Expand Down Expand Up @@ -163,7 +179,7 @@ public function getEnabledFeature($feature = null) {
}
},
"tenant": {
"require_eula": false
"require_eula": true
},
"product_name": "Turnitin Originality",
"access_options": [
Expand Down Expand Up @@ -224,16 +240,30 @@ public function confirmEula($user, $context) {
* Create a new submission at service's end
* @see https://developers.turnitin.com/docs/tca#create-a-submission
*
* @param Submission $submission The article submission to check for plagiarism
* @param User $user The user who is making submitting the submission
* @param Author $author The author/owher of the submission
* @param Site $site The core site of submission system
* @param Site $site The core site of submission system
* @param Submission $submission The article submission to check for plagiarism
* @param User $user The user who is making submitting the submission
* @param Author $author The author/owner of the submission
* @param string $authorPermission Submission author/owner permission set
* @param string $submitterPermission Submission submitter permission set
*
* @return string|null if succeed, it will return the created submission UUID from
* service's end and at failure, will return null
*
* @throws \Exception
*/
public function createSubmission($submission, $user, $author, $site) {
error_log("Creating a new submission with id {$submission->getId()} by submitter {$user->getId()} for owner {$author->getId()}");
public function createSubmission($site, $submission, $user, $author, $authorPermission, $submitterPermission) {

if (!$this->validatePermission($authorPermission, static::SUBMISSION_PERMISSION_SET)) {
throw new \Exception("in valid owner permission {$authorPermission} given");
}

if (!$this->validatePermission($submitterPermission, static::SUBMISSION_PERMISSION_SET)) {
throw new \Exception("in valid submitter permission {$submitterPermission} given");
}

error_log("Creating a new submission with id {$submission->getId()} by submitter {$user->getId()} for owner {$author->getId()} with owner permission as {$authorPermission} and submitter permission as {$submitterPermission}");

return \Illuminate\Support\Str::uuid()->__toString();
}

Expand Down Expand Up @@ -380,4 +410,16 @@ protected function getEulaConfirmationLocale($locale) {

return in_array($locale, $eulaLangs) ? $locale : static::DEFAULT_EULA_LANGUAGE;
}

/**
* Validate the existence of a permission against a given permission set
*
* @param string $permission The specific permission to check for existence
* @param array $permissionSet The permission list to check against
*
* @return bool True/False if the permission exists in the given permission set
*/
protected function validatePermission($permission, $permissionSet) {
return in_array($permission, $permissionSet);
}
}

0 comments on commit d52111a

Please sign in to comment.