Skip to content

Commit

Permalink
Merge pull request #3 from thoth-pub/stable-3_4_0
Browse files Browse the repository at this point in the history
Adapt to OMP 3.4
  • Loading branch information
thiagolepidus authored Oct 18, 2024
2 parents 8da6af9 + f4e9c6e commit 5df816b
Show file tree
Hide file tree
Showing 43 changed files with 526 additions and 321 deletions.
7 changes: 1 addition & 6 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ variables:

include:
- project: 'documentacao-e-tarefas/modelosparaintegracaocontinua'
ref: stable-3_3_0
ref: main
file:
- 'templates/groups/pkp_plugin.yml'
- 'templates/groups/omp/unit_tests.yml'

.unit_test_template:
before_script:
- rm -rf lib/APIKeyEncryption
- git submodule update --init --depth 1
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Work in Progress Integration of OMP and [Thoth](https://thoth.pub/) for communic
This plugin is compatible with the following PKP applications:

- OMP 3.3.0-x
- OMP 3.4.0-x

## Requirements

Expand Down
7 changes: 5 additions & 2 deletions ThothPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ThothPlugin
*
* @ingroup plugins_generic_thoth
*
* @brief Plugin for integration with Thoth for communication and synchronization of book data between the two platforms
*/

use APP\plugins\generic\thoth\classes\APIKeyEncryption;
use PKP\core\JSONMessage;

import('lib.pkp.classes.plugins.GenericPlugin');
import('plugins.generic.thoth.classes.ThothBadgeRender');
import('plugins.generic.thoth.classes.ThothNotification');
Expand Down Expand Up @@ -129,10 +133,9 @@ public function getThothClient($contextId = null)
$password = $this->getSetting($contextId, 'password');

if (!$email || !$password) {
throw new ThothException("Credentials not configured", 0);
throw new ThothException('Credentials not configured', 0);
}

import('plugins.generic.thoth.lib.APIKeyEncryption.APIKeyEncryption');
$password = APIKeyEncryption::decryptString($password);
$testEnvironment = $this->getSetting($contextId, 'testEnvironment');

Expand Down
9 changes: 7 additions & 2 deletions ThothSettingsForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ThothSettingsForm
*
* @ingroup plugins_generic_thoth
*
* @brief Form for managers to modify Thoth plugin settings
*/

import('lib.pkp.classes.form.Form');
import('plugins.generic.thoth.lib.APIKeyEncryption.APIKeyEncryption');
use APP\plugins\generic\thoth\classes\APIKeyEncryption;
use PKP\form\Form;
use PKP\form\validation\FormValidatorCSRF;
use PKP\form\validation\FormValidatorCustom;
use PKP\form\validation\FormValidatorPost;

import('plugins.generic.thoth.lib.thothAPI.ThothClient');

class ThothSettingsForm extends Form
Expand Down
48 changes: 48 additions & 0 deletions classes/APIKeyEncryption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace APP\plugins\generic\thoth\classes;

use Exception;
use Firebase\JWT\JWT;
use PKP\config\Config;

class APIKeyEncryption
{
public static function secretConfigExists(): bool
{
try {
self::getSecretFromConfig();
} catch (Exception $e) {
return false;
}
return true;
}

private static function getSecretFromConfig(): string
{
$secret = Config::getVar('security', 'api_key_secret');
if ($secret === '') {
throw new Exception("A secret must be set in the config file ('api_key_secret') so that keys can be encrypted and decrypted");
}
return $secret;
}

public static function encryptString(string $plainText): string
{
$secret = self::getSecretFromConfig();
return JWT::encode($plainText, $secret, 'HS256');
}

public static function decryptString(string $encryptedText): string
{
$secret = self::getSecretFromConfig();
try {
return JWT::decode($encryptedText, $secret, ['HS256']);
} catch (Firebase\JWT\SignatureInvalidException $e) {
throw new Exception(
'The `api_key_secret` configuration is not the same as the one used to encrypt the key.',
1
);
}
}
}
7 changes: 5 additions & 2 deletions classes/ThothBadgeRender.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ThothBadgeRender
*
* @ingroup plugins_generic_thoth
*
* @brief Manage callback functions render Thoth badge in Workflow page
*/

use APP\core\Application;

class ThothBadgeRender
{
private $plugin;
Expand Down Expand Up @@ -40,7 +43,7 @@ public function addThothBadge($hookName, $args)
]
);

$templateMgr->registerFilter("output", array($this, 'thothBadgeFilter'));
$templateMgr->registerFilter('output', [$this, 'thothBadgeFilter']);

return false;
}
Expand All @@ -55,7 +58,7 @@ public function thothBadgeFilter($output, $templateMgr)
$newOutput .= $templateMgr->fetch($this->plugin->getTemplateResource('thothBadge.tpl'));
$newOutput .= substr($output, $offset + strlen($match));
$output = $newOutput;
$templateMgr->unregisterFilter('output', array($this, 'thothBadgeFilter'));
$templateMgr->unregisterFilter('output', [$this, 'thothBadgeFilter']);
}
return $output;
}
Expand Down
5 changes: 5 additions & 0 deletions classes/ThothNotification.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ThothNotification
*
* @ingroup plugins_generic_thoth
*
* @brief Manage function to display plugin notifications
*/

use APP\core\Application;
use APP\notification\NotificationManager;
use PKP\core\JSONMessage;

class ThothNotification
{
private $plugin;
Expand Down
57 changes: 30 additions & 27 deletions classes/ThothRegister.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ThothRegister
*
* @ingroup plugins_generic_thoth
*
* @brief Manage callback functions to register works in Thoth
*/

use APP\facades\Repo;
use APP\i18n\AppLocale;
use APP\notification\Notification;
use PKP\security\Role;

import('plugins.generic.thoth.classes.facades.ThothService');

class ThothRegister
Expand Down Expand Up @@ -42,7 +48,7 @@ public function addImprintField($hookName, $form)
return;
}

$submission = Services::get('submission')->get($form->publication->getData('submissionId'));
$submission = Repo::submission()->get($form->publication->getData('submissionId'));

if ($submission->getData('thothWorkId')) {
return;
Expand All @@ -69,13 +75,13 @@ public function addImprintField($hookName, $form)
'value' => false,
'groupId' => 'default',
]))
->addField(new \PKP\components\forms\FieldSelect('imprint', [
'label' => __('plugins.generic.thoth.imprint'),
'options' => $imprintOptions,
'required' => true,
'showWhen' => 'registerConfirmation',
'groupId' => 'default'
]));
->addField(new \PKP\components\forms\FieldSelect('imprint', [
'label' => __('plugins.generic.thoth.imprint'),
'options' => $imprintOptions,
'required' => true,
'showWhen' => 'registerConfirmation',
'groupId' => 'default'
]));
} catch (ThothException $e) {
$warningIconHtml = '<span class="fa fa-exclamation-triangle pkpIcon--inline"></span>';
$noticeMsg = __('plugins.generic.thoth.connectionError');
Expand Down Expand Up @@ -173,22 +179,18 @@ public function registerWork($submission, $imprint)
try {
$thothClient = $this->plugin->getThothClient($submissionContext->getId());
$thothBook = ThothService::work()->registerBook($thothClient, $submission, $imprint);
$submission = Services::get('submission')->edit(
$submission,
['thothWorkId' => $thothBook->getId()],
$request
);
$submission = Repo::submission()->edit($submission, ['thothWorkId' => $thothBook->getId()]);

ThothNotification::notify(
$request,
NOTIFICATION_TYPE_SUCCESS,
Notification::NOTIFICATION_TYPE_SUCCESS,
__('plugins.generic.thoth.register.success')
);
} catch (ThothException $e) {
error_log($e->getMessage());
ThothNotification::notify(
$request,
NOTIFICATION_TYPE_ERROR,
Notification::NOTIFICATION_TYPE_ERROR,
__('plugins.generic.thoth.register.error')
);
}
Expand Down Expand Up @@ -230,16 +232,17 @@ public function addThothEndpoint($hookName, $args)
$endpoints = & $args[0];
$handler = $args[1];

if (!is_a($handler, 'PKPSubmissionHandler')) {
if (!is_a($handler, 'PKP\API\v1\submissions\PKPSubmissionHandler')) {
return false;
}


array_unshift(
$endpoints['PUT'],
[
'pattern' => $handler->getEndpointPattern() . '/{submissionId}/publications/{publicationId}/register',
'handler' => [$this, 'register'],
'roles' => [ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR],
'roles' => [Role::ROLE_ID_MANAGER, Role::ROLE_ID_SUB_EDITOR],
]
);

Expand All @@ -253,7 +256,7 @@ public function register($slimRequest, $response, $args)
$request = Application::get()->getRequest();
$handler = $request->getRouter()->getHandler();
$submission = $handler->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION);
$publication = Services::get('publication')->get((int) $args['publicationId']);
$publication = Repo::publication()->get($args['publicationId']);
$params = $slimRequest->getParsedBody();

if (empty($params['imprint'])) {
Expand Down Expand Up @@ -281,16 +284,16 @@ public function register($slimRequest, $response, $args)

$this->registerWork($submission, $params['imprint']);

$userGroupDao = DAORegistry::getDAO('UserGroupDAO');
$userGroups = Repo::userGroup()->getCollector()
->filterByContextIds([$submission->getData('contextId')])
->getMany();

$publicationProps = Services::get('publication')->getFullProperties(
$publication,
[
'request' => $request,
'userGroups' => $userGroupDao->getByContextId($submission->getData('contextId'))->toArray(),
]
);
$genreDao = DAORegistry::getDAO('GenreDAO');
$genres = $genreDao->getByContextId($submission->getData('contextId'))->toArray();

return $response->withJson($publicationProps, 200);
return $response->withJson(
Repo::publication()->getSchemaMap($submission, $userGroups, $genres)->map($publication),
200
);
}
}
10 changes: 7 additions & 3 deletions classes/ThothUpdater.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ThothUpdater
*
* @ingroup plugins_generic_thoth
*
* @brief Manage callback functions to update works in Thoth
*/

use APP\facades\Repo;
use APP\notification\Notification;

import('plugins.generic.thoth.classes.facades.ThothService');

class ThothUpdater
Expand All @@ -29,7 +33,7 @@ public function updateWork($hookName, $args)
$publication = $args[0];
$request = $args[3];

$submission = Services::get('submission')->get($publication->getData('submissionId'));
$submission = Repo::submission()->get($publication->getData('submissionId'));
$thothWorkId = $submission->getData('thothWorkId');

if (!$thothWorkId) {
Expand All @@ -42,14 +46,14 @@ public function updateWork($hookName, $args)

ThothNotification::notify(
$request,
NOTIFICATION_TYPE_SUCCESS,
Notification::NOTIFICATION_TYPE_SUCCESS,
__('plugins.generic.thoth.update.success')
);
} catch (ThothException $e) {
error_log($e->getMessage());
ThothNotification::notify(
$request,
NOTIFICATION_TYPE_ERROR,
Notification::NOTIFICATION_TYPE_ERROR,
__('plugins.generic.thoth.update.error')
);
}
Expand Down
9 changes: 5 additions & 4 deletions classes/components/forms/RegisterForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class RegisterForm
*
* @ingroup plugins_generic_thoth
*
* @brief A preset form for confirming a publication's issue before publishing.
* It may also be used for scheduling a publication in an issue for later
* publication.
*/

use PKP\components\forms\FormComponent;
use PKP\components\forms\FieldHTML;
use PKP\components\forms\FieldSelect;
use PKP\components\forms\FormComponent;

class RegisterForm extends FormComponent
{
Expand Down Expand Up @@ -68,9 +69,9 @@ public function __construct($action, $imprints, $errors)
]);

$this->addGroup([
'id' => 'default',
'pageId' => 'default',
])
'id' => 'default',
'pageId' => 'default',
])
->addField(new FieldHTML('validation', [
'description' => $msg,
'groupId' => 'default',
Expand Down
1 change: 1 addition & 0 deletions classes/services/ThothAffiliationService.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ThothAffiliationService
*
* @ingroup plugins_generic_thoth
*
* @brief Helper class that encapsulates business logic for Thoth affiliations
Expand Down
Loading

0 comments on commit 5df816b

Please sign in to comment.