Skip to content

Commit a4cb5f4

Browse files
Merge pull request #58 from lepidus/main
Update thoth schema integration (OMP 3.5.0)
2 parents f7c2402 + 6afa031 commit a4cb5f4

62 files changed

Lines changed: 2124 additions & 239 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ This plugin is compatible with the following PKP applications:
3030

3131
After enabling the plugin, go to the plugin settings and fill in:
3232

33-
- **Email** and **Password**: Credentials for a Thoth account to connect with the API.
33+
- **Personal access token**: A valid Thoth personal access token used to authenticate API requests.
3434
- **Custom Thoth API**: Check this option to use a custom Thoth API instead of the official one.
3535
- **Thoth API URL**: The URL of the custom Thoth API (only required when the custom API option is enabled).
3636

37-
<img src="/docs/images/plugin_settings.png" alt="Plugin settings form with email, password, custom API and URL fields" width="700">
37+
<img src="/docs/images/plugin_settings.png" alt="Plugin settings form with personal access token, custom API and URL fields" width="700">
3838

3939
### Registering Monographs
4040

ThothSettingsForm.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
use APP\template\TemplateManager;
2222
use GuzzleHttp\Exception\GuzzleException;
23+
use Illuminate\Contracts\Encryption\DecryptException;
2324
use Illuminate\Support\Facades\Crypt;
2425
use PKP\form\Form;
2526
use PKP\form\validation\FormValidatorCSRF;
@@ -34,8 +35,7 @@
3435
class ThothSettingsForm extends Form
3536
{
3637
private const SETTINGS = [
37-
'email',
38-
'password',
38+
'token',
3939
'customThothApi',
4040
'customThothApiUrl',
4141
];
@@ -84,20 +84,27 @@ function ($customThothApiUrl) {
8484

8585
$this->addCheck(new FormValidatorCustom(
8686
$this,
87-
'password',
87+
'token',
8888
'required',
8989
'plugins.generic.thoth.settings.invalidCredentials',
90-
fn ($password) => $this->validateCredentials(trim($this->getData('email')), $password)
90+
fn ($token) => $this->validateCredentials(trim($token))
9191
));
9292
}
9393

9494
public function initData(): void
9595
{
9696
foreach (self::SETTINGS as $setting) {
9797
$value = $this->plugin->getSetting($this->contextId, $setting);
98+
if ($setting === 'token' && $value) {
99+
try {
100+
$value = Crypt::decrypt($value);
101+
} catch (DecryptException $exception) {
102+
$value = '';
103+
}
104+
}
98105
$this->setData(
99106
$setting,
100-
$setting === 'password' && $value ? Crypt::decrypt($value) : $value
107+
$value
101108
);
102109
}
103110
}
@@ -116,7 +123,7 @@ public function fetch($request, $template = null, $display = false): string
116123

117124
public function execute(...$functionArgs): void
118125
{
119-
$this->setData('password', Crypt::encrypt($this->getData('password')));
126+
$this->setData('token', Crypt::encrypt(trim($this->getData('token'))));
120127

121128
foreach (self::SETTINGS as $setting) {
122129
$this->plugin->updateSetting($this->contextId, $setting, trim($this->getData($setting)), 'string');
@@ -125,15 +132,15 @@ public function execute(...$functionArgs): void
125132
parent::execute(...$functionArgs);
126133
}
127134

128-
private function validateCredentials(string $email, string $password): bool
135+
private function validateCredentials(string $token): bool
129136
{
130137
$httpConfig = [];
131138
if ($this->getData('customThothApi') && $this->getData('customThothApiUrl')) {
132139
$httpConfig['base_uri'] = trim($this->getData('customThothApiUrl'));
133140
}
134141

135142
try {
136-
(new Client($httpConfig))->login($email, $password);
143+
(new Client($httpConfig))->setToken($token)->me();
137144
return true;
138145
} catch (QueryException) {
139146
return false;

classes/api/ThothEndpoint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function register(IlluminateRequest $illuminateRequest): JsonResponse
123123
$this->handleNotification($request, $submission, true, $disableNotification);
124124
} catch (QueryException $e) {
125125
$thothBookService->deleteRegisteredEntry();
126-
$this->handleNotification($request, $submission, false, $disableNotification, $e->getMessage());
126+
$this->handleNotification($request, $submission, false, $disableNotification, $e);
127127
$failure['errors'][] = __('plugins.generic.thoth.register.error.log', ['reason' => $e->getMessage()]);
128128
return response()->json($failure, Response::HTTP_BAD_REQUEST);
129129
}

classes/components/forms/config/PublishFormConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use APP\facades\Repo;
2020
use APP\plugins\generic\thoth\classes\facades\ThothRepository;
2121
use APP\plugins\generic\thoth\classes\facades\ThothService;
22+
use APP\plugins\generic\thoth\classes\notification\ThothNotification;
2223
use APP\submission\Submission;
2324
use ThothApi\GraphQL\Models\Work as ThothWork;
2425

classes/container/providers/ThothRepositoryProvider.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
use APP\core\Application;
2222
use APP\plugins\generic\thoth\classes\repositories\ThothAccountRepository;
23+
use APP\plugins\generic\thoth\classes\repositories\ThothAbstractRepository;
2324
use APP\plugins\generic\thoth\classes\repositories\ThothAffiliationRepository;
25+
use APP\plugins\generic\thoth\classes\repositories\ThothBiographyRepository;
2426
use APP\plugins\generic\thoth\classes\repositories\ThothBookRepository;
2527
use APP\plugins\generic\thoth\classes\repositories\ThothChapterRepository;
2628
use APP\plugins\generic\thoth\classes\repositories\ThothContributionRepository;
@@ -32,8 +34,10 @@
3234
use APP\plugins\generic\thoth\classes\repositories\ThothPublicationRepository;
3335
use APP\plugins\generic\thoth\classes\repositories\ThothReferenceRepository;
3436
use APP\plugins\generic\thoth\classes\repositories\ThothSubjectRepository;
37+
use APP\plugins\generic\thoth\classes\repositories\ThothTitleRepository;
3538
use APP\plugins\generic\thoth\classes\repositories\ThothWorkRelationRepository;
3639
use APP\plugins\generic\thoth\classes\repositories\ThothWorkRepository;
40+
use Illuminate\Contracts\Encryption\DecryptException;
3741
use Illuminate\Support\Facades\Crypt;
3842
use PKP\db\DAORegistry;
3943
use ThothApi\GraphQL\Client;
@@ -48,14 +52,21 @@ public function register($container)
4852

4953
$customThothApi = $pluginSettingsDao->getSetting($contextId, 'ThothPlugin', 'customThothApi');
5054
$customThothApiUrl = $pluginSettingsDao->getSetting($contextId, 'ThothPlugin', 'customThothApiUrl');
51-
$email = $pluginSettingsDao->getSetting($contextId, 'ThothPlugin', 'email');
52-
$password = $pluginSettingsDao->getSetting($contextId, 'ThothPlugin', 'password') ?? '';
55+
$token = $pluginSettingsDao->getSetting($contextId, 'ThothPlugin', 'token') ?? '';
56+
$decryptedToken = '';
57+
58+
if ($token) {
59+
try {
60+
$decryptedToken = Crypt::decrypt($token);
61+
} catch (DecryptException $exception) {
62+
$decryptedToken = '';
63+
}
64+
}
5365

5466
return [
5567
'customThothApi' => $customThothApi,
5668
'customThothApiUrl' => $customThothApiUrl,
57-
'email' => $email,
58-
'password' => Crypt::decrypt($password)
69+
'token' => $decryptedToken
5970
];
6071
});
6172

@@ -68,13 +79,17 @@ public function register($container)
6879
}
6980

7081
$client = new Client($httpConfig);
71-
return $client->login($config['email'], $config['password']);
82+
return $client->setToken($config['token']);
7283
});
7384

7485
$container->set('accountRepository', function ($container) {
7586
return new ThothAccountRepository($container->get('client'));
7687
});
7788

89+
$container->set('abstractRepository', function ($container) {
90+
return new ThothAbstractRepository($container->get('client'));
91+
});
92+
7893
$container->set('affiliationRepository', function ($container) {
7994
return new ThothAffiliationRepository($container->get('client'));
8095
});
@@ -83,6 +98,10 @@ public function register($container)
8398
return new ThothBookRepository($container->get('client'));
8499
});
85100

101+
$container->set('biographyRepository', function ($container) {
102+
return new ThothBiographyRepository($container->get('client'));
103+
});
104+
86105
$container->set('chapterRepository', function ($container) {
87106
return new ThothChapterRepository($container->get('client'));
88107
});
@@ -123,6 +142,10 @@ public function register($container)
123142
return new ThothSubjectRepository($container->get('client'));
124143
});
125144

145+
$container->set('titleRepository', function ($container) {
146+
return new ThothTitleRepository($container->get('client'));
147+
});
148+
126149
$container->set('workRelationRepository', function ($container) {
127150
return new ThothWorkRelationRepository($container->get('client'));
128151
});

classes/container/providers/ThothServiceProvider.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@
1919

2020
use APP\plugins\generic\thoth\classes\factories\ThothBookFactory;
2121
use APP\plugins\generic\thoth\classes\factories\ThothChapterFactory;
22+
use APP\plugins\generic\thoth\classes\factories\ThothAbstractFactory;
23+
use APP\plugins\generic\thoth\classes\factories\ThothBiographyFactory;
2224
use APP\plugins\generic\thoth\classes\factories\ThothContributionFactory;
2325
use APP\plugins\generic\thoth\classes\factories\ThothContributorFactory;
2426
use APP\plugins\generic\thoth\classes\factories\ThothLocationFactory;
2527
use APP\plugins\generic\thoth\classes\factories\ThothPublicationFactory;
28+
use APP\plugins\generic\thoth\classes\factories\ThothTitleFactory;
2629
use APP\plugins\generic\thoth\classes\services\ThothAffiliationService;
30+
use APP\plugins\generic\thoth\classes\services\ThothAbstractService;
31+
use APP\plugins\generic\thoth\classes\services\ThothBiographyService;
2732
use APP\plugins\generic\thoth\classes\services\ThothBookService;
2833
use APP\plugins\generic\thoth\classes\services\ThothChapterService;
2934
use APP\plugins\generic\thoth\classes\services\ThothContributionService;
@@ -33,6 +38,7 @@
3338
use APP\plugins\generic\thoth\classes\services\ThothPublicationService;
3439
use APP\plugins\generic\thoth\classes\services\ThothReferenceService;
3540
use APP\plugins\generic\thoth\classes\services\ThothSubjectService;
41+
use APP\plugins\generic\thoth\classes\services\ThothTitleService;
3642
use APP\plugins\generic\thoth\classes\services\ThothWorkRelationService;
3743

3844
class ThothServiceProvider implements ContainerProvider
@@ -43,6 +49,20 @@ public function register($container)
4349
return new ThothAffiliationService($container->get('affiliationRepository'));
4450
});
4551

52+
$container->set('abstractService', function ($container) {
53+
return new ThothAbstractService(
54+
new ThothAbstractFactory(),
55+
$container->get('abstractRepository')
56+
);
57+
});
58+
59+
$container->set('biographyService', function ($container) {
60+
return new ThothBiographyService(
61+
new ThothBiographyFactory(),
62+
$container->get('biographyRepository')
63+
);
64+
});
65+
4666
$container->set('bookService', function ($container) {
4767
return new ThothBookService(
4868
new ThothBookFactory(),
@@ -97,6 +117,13 @@ public function register($container)
97117
return new ThothSubjectService($container->get('subjectRepository'));
98118
});
99119

120+
$container->set('titleService', function ($container) {
121+
return new ThothTitleService(
122+
new ThothTitleFactory(),
123+
$container->get('titleRepository')
124+
);
125+
});
126+
100127
$container->set('workRelationService', function ($container) {
101128
return new ThothWorkRelationService($container->get('workRelationRepository'));
102129
});

classes/facades/ThothRepository.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ public static function account()
2525
return ThothContainer::getInstance()->get('accountRepository');
2626
}
2727

28+
public static function abstract()
29+
{
30+
return ThothContainer::getInstance()->get('abstractRepository');
31+
}
32+
33+
public static function biography()
34+
{
35+
return ThothContainer::getInstance()->get('biographyRepository');
36+
}
37+
2838
public static function affiliation()
2939
{
3040
return ThothContainer::getInstance()->get('affiliationRepository');
@@ -75,6 +85,11 @@ public static function subject()
7585
return ThothContainer::getInstance()->get('subjectRepository');
7686
}
7787

88+
public static function title()
89+
{
90+
return ThothContainer::getInstance()->get('titleRepository');
91+
}
92+
7893
public static function workRelation()
7994
{
8095
return ThothContainer::getInstance()->get('workRelationRepository');

classes/facades/ThothService.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,21 @@
2020

2121
class ThothService
2222
{
23+
public static function abstract()
24+
{
25+
return ThothContainer::getInstance()->get('abstractService');
26+
}
27+
2328
public static function affiliation()
2429
{
2530
return ThothContainer::getInstance()->get('affiliationService');
2631
}
2732

33+
public static function biography()
34+
{
35+
return ThothContainer::getInstance()->get('biographyService');
36+
}
37+
2838
public static function book()
2939
{
3040
return ThothContainer::getInstance()->get('bookService');
@@ -75,6 +85,11 @@ public static function subject()
7585
return ThothContainer::getInstance()->get('subjectService');
7686
}
7787

88+
public static function title()
89+
{
90+
return ThothContainer::getInstance()->get('titleService');
91+
}
92+
7893
public static function workRelation()
7994
{
8095
return ThothContainer::getInstance()->get('workRelationService');

0 commit comments

Comments
 (0)