forked from kyfr59/omeka-s-module-GuestUser
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModule.php
973 lines (875 loc) · 35 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
<?php declare(strict_types=1);
/*
* Copyright BibLibre, 2016
* Copyright Daniel Berthereau, 2017-2024
*
* This software is governed by the CeCILL license under French law and abiding
* by the rules of distribution of free software. You can use, modify and/ or
* redistribute the software under the terms of the CeCILL license as circulated
* by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy, modify
* and redistribute granted by the license, users are provided only with a
* limited warranty and the software's author, the holder of the economic
* rights, and the successive licensors have only limited liability.
*
* In this respect, the user's attention is drawn to the risks associated with
* loading, using, modifying and/or developing or reproducing the software by
* the user in light of its specific status of free software, that may mean that
* it is complicated to manipulate, and that also therefore means that it is
* reserved for developers and experienced professionals having in-depth
* computer knowledge. Users are therefore encouraged to load and test the
* software's suitability as regards their requirements in conditions enabling
* the security of their systems and/or data to be ensured and, more generally,
* to use and operate it in the same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*/
namespace Guest;
if (!class_exists(\Common\TraitModule::class)) {
require_once dirname(__DIR__) . '/Common/TraitModule.php';
}
use Common\Stdlib\PsrMessage;
use Common\TraitModule;
use Guest\Entity\GuestToken;
use Guest\Permissions\Acl;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\Form\Element;
use Laminas\Mvc\MvcEvent;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Laminas\View\Renderer\PhpRenderer;
use Omeka\Api\Representation\UserRepresentation;
use Omeka\Module\AbstractModule;
use Omeka\Module\Exception\ModuleCannotInstallException;
use Omeka\Permissions\Assertion\IsSelfAssertion;
/**
* Guest
*
* @copyright BibLibre, 2016
* @copyright Daniel Berthereau, 2017-2025
* @license http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*/
class Module extends AbstractModule
{
use TraitModule;
const NAMESPACE = __NAMESPACE__;
protected $dependencies = [
'Common',
];
/**
* {@inheritDoc}
* @see \Omeka\Module\AbstractModule::onBootstrap()
* @todo Find the right way to load Guest before other modules in order to add role.
*/
public function onBootstrap(MvcEvent $event): void
{
parent::onBootstrap($event);
$this->addAclRoleAndRules();
}
public function install(ServiceLocatorInterface $services): void
{
// Required during install because the role is set in config.
require_once __DIR__ . '/src/Permissions/Acl.php';
$this->installAuto($services);
}
/**
* @depracated See Common 3.4.66.
*/
protected function installAuto(ServiceLocatorInterface $services): void
{
$this->setServiceLocator($services);
$this->initTranslations();
/**@var \Laminas\Mvc\I18n\Translator $translator */
$translator = $services->get('MvcTranslator');
$this->preInstall();
if (!$this->checkDependencies()) {
if (count($this->dependencies) === 1) {
$message = new PsrMessage(
'This module requires the module "{module}".', // @translate
['module' => reset($this->dependencies)]
);
} else {
$message = new PsrMessage(
'This module requires modules "{modules}".', // @translate
['modules' => implode('", "', $this->dependencies)]
);
}
throw new ModuleCannotInstallException((string) $message->setTranslator($translator));
}
if (!$this->checkAllResourcesToInstall()) {
$message = new PsrMessage(
'This module has resources that cannot be installed.' // @translate
);
throw new ModuleCannotInstallException((string) $message->setTranslator($translator));
}
$sqlFile = $this->modulePath() . '/data/install/schema.sql';
if (!$this->checkNewTablesFromFile($sqlFile)) {
$message = new PsrMessage(
'This module cannot install its tables, because they exist already. Try to remove them first.' // @translate
);
throw new ModuleCannotInstallException((string) $message->setTranslator($translator));
}
$this->execSqlFromFile($sqlFile);
$this
->installAllResources()
->manageConfig('install')
->manageMainSettings('install')
->manageSiteSettings('install')
->manageUserSettings('install')
->postInstall();
}
protected function preInstall(): void
{
$services = $this->getServiceLocator();
$translate = $services->get('ControllerPluginManager')->get('translate');
if (!method_exists($this, 'checkModuleActiveVersion') || !$this->checkModuleActiveVersion('Common', '3.4.65')) {
$message = new \Omeka\Stdlib\Message(
$translate('The module %1$s should be upgraded to version %2$s or later.'), // @translate
'Common', '3.4.65'
);
throw new \Omeka\Module\Exception\ModuleCannotInstallException((string) $message);
}
}
protected function preUninstall(): void
{
$this->deactivateGuests();
}
protected function preUpgrade(): void
{
// Required during upgrade because the role is set in config.
require_once __DIR__ . '/src/Permissions/Acl.php';
}
/**
* Add ACL role and rules for this module.
*/
protected function addAclRoleAndRules(): void
{
/** @var \Omeka\Permissions\Acl $acl */
$services = $this->getServiceLocator();
$acl = $services->get('Omeka\Acl');
// TODO To be removed when roles will be integrated in core.
/** @see https://github.com/omeka/omeka-s/pull/2241 */
// This check allows to add the role "guest" by dependencies without
// complex process. It avoids issues when the module is disabled too.
// TODO Find a way to set the role "guest" during init or via Omeka\Service\AclFactory (allowing multiple delegators).
if (!$acl->hasRole(Acl::ROLE_GUEST)) {
$acl->addRole(Acl::ROLE_GUEST);
}
if (!$acl->hasRole('guest_private')) {
$acl->addRole('guest_private');
}
$acl->addRoleLabel(Acl::ROLE_GUEST, 'Guest'); // @translate
$settings = $services->get('Omeka\Settings');
$isOpenRegister = $settings->get('guest_open', 'moderate');
// Rules for anonymous.
$acl
->allow(
null,
[\Guest\Controller\Site\AnonymousController::class]
)
->allow(
null,
[\Guest\Controller\Site\GuestController::class],
// Redirected to login in controller.
['me']
)
;
if ($isOpenRegister !== 'closed') {
$acl
->allow(
null,
[\Omeka\Entity\User::class],
// Change role and Activate user should be set to allow external
// logging (ldap, saml, etc.), not only guest registration here.
// Internal checks are added in the controller.
['create', 'change-role', 'activate-user']
)
->allow(
null,
[\Omeka\Api\Adapter\UserAdapter::class],
['create']
);
} else {
$acl
->deny(
null,
[\Guest\Controller\Site\AnonymousController::class],
['register']
);
}
// Rules for guest.
$roles = $acl->getRoles();
$acl
->allow(
$roles,
[\Guest\Controller\Site\GuestController::class]
)
->allow(
[Acl::ROLE_GUEST, 'guest_private'],
[\Omeka\Entity\User::class],
['read', 'update', 'change-password'],
new IsSelfAssertion
)
->allow(
[Acl::ROLE_GUEST, 'guest_private'],
[\Omeka\Api\Adapter\UserAdapter::class],
['read', 'update']
)
->deny(
[Acl::ROLE_GUEST, 'guest_private'],
[
'Omeka\Controller\Admin\Asset',
'Omeka\Controller\Admin\Index',
'Omeka\Controller\Admin\Item',
'Omeka\Controller\Admin\ItemSet',
'Omeka\Controller\Admin\Job',
'Omeka\Controller\Admin\Media',
'Omeka\Controller\Admin\Module',
'Omeka\Controller\Admin\Property',
'Omeka\Controller\Admin\ResourceClass',
'Omeka\Controller\Admin\ResourceTemplate',
'Omeka\Controller\Admin\Setting',
'Omeka\Controller\Admin\SystemInfo',
'Omeka\Controller\Admin\User',
'Omeka\Controller\Admin\Vocabulary',
'Omeka\Controller\SiteAdmin\Index',
'Omeka\Controller\SiteAdmin\Page',
]
)
;
// Rules for api.
if ($isOpenRegister !== 'closed') {
$acl
->allow(
null,
[\Guest\Controller\ApiController::class],
['login', 'session-token', 'logout', 'register']
)
->allow(
null,
[\Omeka\Entity\User::class],
// Change role and Activate user should be set to allow external
// logging (ldap, saml, etc.), not only guest registration here.
['create', 'change-role', 'activate-user']
)
->allow(
null,
[\Omeka\Api\Adapter\UserAdapter::class],
'create'
);
}
// This is an api, so all rest api actions are allowed when available.
// Rights are managed via credentials.
$acl
->allow(
null,
[
\Guest\Controller\ApiController::class,
\Guest\Controller\GuestApiController::class,
]
);
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager): void
{
// TODO How to attach all public events only?
$sharedEventManager->attach(
'*',
'view.layout',
[$this, 'appendLoginNav']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\UserAdapter::class,
'api.delete.post',
[$this, 'deleteGuestToken']
);
// Add the guest main infos to the user show admin pages.
$sharedEventManager->attach(
'Omeka\Controller\Admin\User',
'view.details',
[$this, 'viewUserDetails']
);
$sharedEventManager->attach(
'Omeka\Controller\Admin\User',
'view.show.after',
[$this, 'viewUserShowAfter']
);
// Add links to login form.
$sharedEventManager->attach(
'*',
'view.login.after',
[$this, 'addLoginLinks']
);
// Manage redirect to admin or site after login.
$sharedEventManager->attach(
'*',
'user.login',
[$this, 'handleUserLogin']
);
// Add the guest element form to the user form.
$sharedEventManager->attach(
\Omeka\Form\UserForm::class,
'form.add_elements',
[$this, 'addUserFormElement']
);
// Add the guest element filters to the user form.
$sharedEventManager->attach(
\Omeka\Form\UserForm::class,
'form.add_input_filters',
[$this, 'addUserFormElementFilter']
);
// FIXME Use the autoset of the values (in a fieldset) and remove this.
$sharedEventManager->attach(
'Omeka\Controller\Admin\User',
'view.edit.form.before',
[$this, 'addUserFormValue']
);
$sharedEventManager->attach(
\Omeka\Form\SettingForm::class,
'form.add_elements',
[$this, 'handleMainSettings']
);
$sharedEventManager->attach(
\Omeka\Form\SiteSettingsForm::class,
'form.add_elements',
[$this, 'handleSiteSettings']
);
// Add a job for module Easy Admin.
$sharedEventManager->attach(
\EasyAdmin\Form\CheckAndFixForm::class,
'form.add_elements',
[$this, 'handleEasyAdminJobsForm']
);
$sharedEventManager->attach(
\EasyAdmin\Controller\Admin\CheckAndFixController::class,
'easyadmin.job',
[$this, 'handleEasyAdminJobs']
);
}
protected function isSettingTranslatable(string $settingsType, string $name): bool
{
$translatables = [
'guest_login_text',
'guest_register_text',
'guest_dashboard_label',
'guest_capabilities',
'guest_short_capabilities',
'guest_message_confirm_email_subject',
'guest_message_confirm_email',
'guest_message_confirm_registration_email_subject',
'guest_message_confirm_registration_email',
'guest_message_update_email_subject',
'guest_message_update_email',
'guest_message_confirm_email_site',
'guest_message_confirm_register_site',
'guest_message_confirm_register_moderate_site',
'guest_terms_text',
];
if ($settingsType !== 'settings'
&& $settingsType !== 'site_settings'
) {
return false;
}
return in_array($name, $translatables);
}
public function appendLoginNav(Event $event): void
{
$view = $event->getTarget();
if ($view->params()->fromRoute('__ADMIN__')) {
return;
}
$auth = $this->getServiceLocator()->get('Omeka\AuthenticationService');
if ($auth->hasIdentity()) {
$view->headStyle()->appendStyle('li a.registerlink, li a.loginlink { display:none; }');
} else {
$view->headStyle()->appendStyle('li a.logoutlink { display:none; }');
}
}
public function viewUserDetails(Event $event): void
{
$view = $event->getTarget();
$user = $view->resource;
$this->viewUserData($view, $user, 'common/admin/guest');
}
public function viewUserShowAfter(Event $event): void
{
$view = $event->getTarget();
$user = $view->vars()->user;
$this->viewUserData($view, $user, 'common/admin/guest-list');
}
protected function viewUserData(PhpRenderer $view, UserRepresentation $user, $template): void
{
$services = $this->getServiceLocator();
$userSettings = $services->get('Omeka\Settings\User');
$userSettings->setTargetId($user->id());
$guestSite = $this->guestSite($user);
echo $view->partial(
$template,
[
'user' => $user,
'userSettings' => $userSettings,
'guestSite' => $guestSite,
]
);
}
public function addLoginLinks(Event $event): void
{
$settings = $this->getServiceLocator()->get('Omeka\Settings');
$loginView = $settings->get('guest_append_links_to_login_view');
if (!$loginView) {
return;
}
$view = $event->getTarget();
$plugins = $view->getHelperPluginManager();
$links = [];
if ($plugins->has('casLoginUrl')) {
$translate = $plugins->get('translate');
$casLoginUrl = $plugins->get('casLoginUrl');
$links[] = [
'url' => $casLoginUrl(),
'label' => $translate('CAS Login'), // @translate
'class' => 'login-cas',
];
}
if ($plugins->has('ssoLoginLinks')) {
$url = $plugins->get('url');
$idps = $settings->get('singlesignon_idps') ?: [];
// Manage old an new version of module Single Sign-On.
foreach ($idps as $idpSlug => $idp) {
$idpName = $idp['entity_short_id'] ?? $idp['idp_entity_short_id'] ?? $idpSlug;
$links[] = [
'url' => $url('sso', ['action' => 'login', 'idp' => $idpName], true),
'label' => !empty($idp['entity_name'])
? $idp['entity_name']
: (!empty($idp['idp_entity_name'])
? $idp['idp_entity_name']
: ($idp['entity_id'] ?? $idp['idp_entity_id'] ?? $view->translate('[Unknown idp]'))), // @translate
'class' => str_replace(['.', ':'], '-', $idpName),
];
}
}
// TODO Ldap is integrated inside default form.
if ($links) {
echo $view->partial('common/guest-login-links', [
'links' => $links,
'selector' => $loginView,
]);
}
}
/**
* @see https://github.com/omeka/omeka-s/pull/1961
* @uses \Guest\Mvc\Controller\Plugin\UserRedirectUrl
*
* Copy :
* @see \Guest\Module::handleUserLogin()
* @see \GuestPrivateRole\Module::handleUserLogin()
*/
public function handleUserLogin(Event $event): void
{
$userRedirectUrl = $this->getServiceLocator()->get('ControllerPluginManager')->get('userRedirectUrl');
$userRedirectUrl();
}
public function addUserFormElement(Event $event): void
{
/** @var \Omeka\Form\UserForm $form */
$form = $event->getTarget();
$services = $this->getServiceLocator();
$auth = $services->get('Omeka\AuthenticationService');
$settings = $services->get('Omeka\Settings');
$skip = $settings->get('guest_terms_skip');
$isV4 = version_compare(\Omeka\Module::VERSION, '4', '>=');
if ($isV4) {
$elementGroups = [
'guest' => 'Guest', // @translate
];
$userSettingsFieldset = $form->get('user-settings');
$userSettingsFieldset->setOption('element_groups', array_merge($userSettingsFieldset->getOption('element_groups') ?: [], $elementGroups));
}
// Public form.
if ($form->getOption('is_public') && !$skip) {
// Don't add the agreement checkbox in public when registered.
if ($auth->hasIdentity()) {
return;
}
$fieldset = $form->get('user-settings');
$fieldset
->add([
'name' => 'guest_agreed_terms',
'type' => Element\Checkbox::class,
'options' => [
'element_group' => 'guest',
'label' => 'Agreed terms', // @translate
],
'attributes' => [
'id' => 'guest_agreed_terms',
'value' => false,
'required' => true,
],
]);
return;
}
// The user is not the current user, but the user in the form.
$userId = $services->get('Application')->getMvcEvent()->getRouteMatch()->getParam('id');
if (!$userId) {
return;
}
$entityManager = $services->get('Omeka\EntityManager');
/** @var \Omeka\Entity\User $user */
$user = $entityManager->find(\Omeka\Entity\User::class, $userId);
// Manage a direct creation (no id).
if ($user) {
/** @var \Omeka\Settings\UserSettings $userSettings */
$userSettings = $services->get('Omeka\Settings\User');
$userSettings->setTargetId($userId);
$agreedTerms = $userSettings->get('guest_agreed_terms');
$siteRegistration = $userSettings->get('guest_site', $settings->get('default_site', 1));
} else {
$agreedTerms = false;
$siteRegistration = $settings->get('default_site', 1);
}
// Admin board.
$fieldset = $form->get('user-settings');
$fieldset
->add([
'name' => 'guest_site',
'type' => \Common\Form\Element\OptionalSiteSelect::class,
'options' => [
'element_group' => 'guest',
'label' => 'Guest site', // @translate
'info' => 'This parameter is used to manage some site related features, in particular messages.', // @translate
'empty_option' => '',
],
'attributes' => [
'id' => 'guest_site',
'class' => 'chosen-select',
'value' => $siteRegistration,
'required' => false,
'multiple' => false,
'data-placeholder' => 'Select site…', // @translate
],
])
->add([
'name' => 'guest_agreed_terms',
'type' => Element\Checkbox::class,
'options' => [
'element_group' => 'guest',
'label' => 'Agreed terms', // @translate
],
'attributes' => [
'id' => 'guest_agreed_terms',
'value' => $agreedTerms,
],
])
->add([
'name' => 'guest_send_email_moderated_registration',
'type' => Element\Checkbox::class,
'options' => [
'element_group' => 'guest',
'label' => 'Send an email to confirm registration after moderation (user should be activated first)', // @translate
],
'attributes' => [
'id' => 'guest_send_email_moderated_registration',
],
]);
if (!$user) {
return;
}
/** @var \Guest\Entity\GuestToken $guestToken */
$guestToken = $entityManager->getRepository(GuestToken::class)
->findOneBy(['email' => $user->getEmail()], ['id' => 'DESC']);
if (!$guestToken || $guestToken->isConfirmed()) {
return;
}
$fieldset
->add([
'name' => 'guest_clear_token',
'type' => Element\Checkbox::class,
'options' => [
'element_group' => 'guest',
'label' => 'Clear registration token', // @translate
],
'attributes' => [
'id' => 'guest_clear_token',
'value' => false,
],
]);
}
public function addUserFormElementFilter(Event $event): void
{
/** @var \Omeka\Form\UserForm $form */
$form = $event->getTarget();
if ($form->getOption('is_public')) {
return;
}
$services = $this->getServiceLocator();
// The user is not the current user, but the user in the form.
$userId = $services->get('Application')->getMvcEvent()->getRouteMatch()->getParam('id');
if (!$userId) {
return;
}
$inputFilter = $event->getParam('inputFilter');
$inputFilter->get('user-settings')
->add([
'name' => 'guest_send_email_moderated_registration',
'required' => false,
'filters' => [
[
'name' => \Laminas\Filter\Callback::class,
'options' => [
'callback' => [$this, 'sendEmailModeration'],
],
],
],
]);
$entityManager = $services->get('Omeka\EntityManager');
/** @var \Omeka\Entity\User $user */
$user = $entityManager->find(\Omeka\Entity\User::class, $userId);
if (!$user) {
return;
}
/** @var \Guest\Entity\GuestToken $guestToken */
$guestToken = $entityManager->getRepository(GuestToken::class)
->findOneBy(['email' => $user->getEmail()], ['id' => 'DESC']);
if (!$guestToken || $guestToken->isConfirmed()) {
return;
}
$inputFilter->get('user-settings')
->add([
'name' => 'guest_clear_token',
'required' => false,
'filters' => [
[
'name' => \Laminas\Filter\Callback::class,
'options' => [
'callback' => [$this, 'clearToken'],
],
],
],
]);
}
public function sendEmailModeration($value): void
{
static $isSent = false;
if ($isSent || !$value) {
return;
}
$services = $this->getServiceLocator();
// The user is not the current user, but the user in the form.
$userId = $services->get('Application')->getMvcEvent()->getRouteMatch()->getParam('id');
if (!$userId) {
return;
}
$messenger = $services->get('ControllerPluginManager')->get('messenger');
$entityManager = $services->get('Omeka\EntityManager');
/** @var \Omeka\Entity\User $user */
$user = $entityManager->find(\Omeka\Entity\User::class, $userId);
if (!$user) {
return;
}
if (!$user->isActive()) {
$message = new \Omeka\Stdlib\Message(
'You cannot send a message to confirm registration: user is not active.' // @translate
);
$messenger->addError($message);
return;
}
$settings = $services->get('Omeka\Settings');
$api = $services->get('Omeka\ApiManager');
$userRepresentation = $api->read('users', ['id' => $user->getId()])->getContent();
$guestSite = $this->guestSite($userRepresentation);
if (!$guestSite) {
try {
$guestSite = $api->read('sites', ['id' => $settings->get('default_site', 1)])->getContent();
} catch (\Omeka\Api\Exception\NotFoundException $e) {
$message = new \Omeka\Stdlib\Message(
'A default site should be set or the user should have a site in order to confirm registration.' // @translate
);
$messenger->addError($message);
return;
}
}
$siteSettings = $services->get('Omeka\Settings\Site');
$siteSettings->setTargetId($guestSite->id());
$config = $this->getConfig()['settings']['guest'];
$subject = $siteSettings->get('guest_message_confirm_registration_email_subject')
?: $settings->get('guest_message_confirm_registration_email_subject');
$body = $siteSettings->get('guest_message_confirm_registration_email')
?: $settings->get('guest_message_confirm_registration_email');
$subject = $subject ?: $config['guest_message_confirm_registration_email_subject'];
$body = $body ?: $config['guest_message_confirm_registration_email'];
// TODO Factorize creation of email.
$data = [
'main_title' => $settings->get('installation_title', 'Omeka S'),
'site_title' => $guestSite->title(),
'site_url' => $guestSite->siteUrl(null, true),
'user_email' => $user->getEmail(),
'user_name' => $user->getName(),
];
$subject = new PsrMessage($subject, $data);
$body = new PsrMessage($body, $data);
$sendEmail = $services->get('ControllerPluginManager')->get('sendEmail');
$result = $sendEmail($user->getEmail(), $subject, $body, $user->getName());
if ($result) {
$isSent = true;
$message = new PsrMessage('The message of confirmation of the registration has been sent.'); // @translate
$messenger->addSuccess($message);
} else {
$message = new PsrMessage('An error occurred when the email was sent.'); // @translate
$messenger->addError($message);
$logger = $services->get('Omeka\Logger');
$logger->err('[Guest] ' . $message);
}
}
public function clearToken($value): void
{
if (!$value) {
return;
}
$services = $this->getServiceLocator();
// The user is not the current user, but the user in the form.
$userId = $services->get('Application')->getMvcEvent()->getRouteMatch()->getParam('id');
if (!$userId) {
return;
}
/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager = $services->get('Omeka\EntityManager');
/** @var \Omeka\Entity\User $user */
$user = $entityManager->find(\Omeka\Entity\User::class, $userId);
if (!$user) {
return;
}
/** @var \Guest\Entity\GuestToken $guestToken */
$token = $entityManager->getRepository(GuestToken::class)
->findOneBy(['email' => $user->getEmail()], ['id' => 'DESC']);
if (!$token || $token->isConfirmed()) {
return;
}
$entityManager->remove($token);
$entityManager->flush();
}
public function addUserFormValue(Event $event): void
{
// Set the default value for a user setting.
$user = $event->getTarget()->vars()->user;
$form = $event->getParam('form');
$services = $this->getServiceLocator();
$userSettings = $services->get('Omeka\Settings\User');
$userSettings->setTargetId($user->id());
$settings = $services->get('Omeka\Settings');
$skip = $settings->get('guest_terms_skip');
$guestSettings = [
'guest_agreed_terms',
];
$config = $services->get('Config')['guest']['user_settings'];
$fieldset = $form->get('user-settings');
foreach ($guestSettings as $name) {
if ($name === 'guest_agreed_terms' && $skip) {
$fieldset->get($name)->setAttribute('value', 1);
continue;
}
$fieldset->get($name)->setAttribute(
'value',
$userSettings->get($name, $config[$name])
);
}
}
public function deleteGuestToken(Event $event): void
{
$request = $event->getParam('request');
/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager = $this->getServiceLocator()->get('Omeka\EntityManager');
$id = $request->getId();
$token = $entityManager->getRepository(GuestToken::class)->findOneBy(['user' => $id]);
if (empty($token)) {
return;
}
$entityManager->remove($token);
$entityManager->flush();
}
public function handleEasyAdminJobsForm(Event $event): void
{
/**
* @var \EasyAdmin\Form\CheckAndFixForm $form
* @var \Laminas\Form\Element\Radio $process
*/
$form = $event->getTarget();
$fieldset = $form->get('module_tasks');
$process = $fieldset->get('process');
$valueOptions = $process->getValueOptions();
$valueOptions['guest_reset_agreement_terms'] = 'Guest: Reset terms agreement for all guests'; // @translate
$process->setValueOptions($valueOptions);
$fieldset
->add([
'type' => \Laminas\Form\Fieldset::class,
'name' => 'guest_reset_agreement_terms',
'options' => [
'label' => 'Options to reset agreement terms', // @translate
],
'attributes' => [
'class' => 'guest_reset_agreement_terms',
],
]);
$fieldset->get('guest_reset_agreement_terms')
->add([
'name' => 'agreement',
'type' => \Common\Form\Element\OptionalRadio::class,
'options' => [
'label' => 'Reset terms agreement for all guests', // @translate
'info' => 'When terms and conditions are updated, you may want guests agree them one more time. Warning: to set false will impact all guests. So warn them some time before.', // @translate
'value_options' => [
'keep' => 'No change', // @translate
'unset' => 'Set false', // @translate
'set' => 'Set true', // @translate
],
],
'attributes' => [
'id' => 'guest_reset_agreement_terms-agreement',
'value' => 'keep',
'required' => false,
],
]);
}
public function handleEasyAdminJobs(Event $event): void
{
$process = $event->getParam('process');
if ($process === 'guest_reset_agreement_terms') {
$params = $event->getParam('params');
$event->setParam('job', \Guest\Job\GuestAgreement::class);
$event->setParam('args', $params['module_tasks']['guest_reset_agreement_terms'] ?? []);
}
}
/**
* Get the site of a user (option "guest_site").
*
* @param UserRepresentation $user
* @return \Omeka\Api\Representation\SiteRepresentation|null
*/
protected function guestSite(UserRepresentation $user): ?\Omeka\Api\Representation\SiteRepresentation
{
$services = $this->getServiceLocator();
$api = $services->get('Omeka\ApiManager');
$userSettings = $services->get('Omeka\Settings\User');
$userSettings->setTargetId($user->id());
$guestSite = $userSettings->get('guest_site') ?: null;
if ($guestSite) {
try {
$guestSite = $api->read('sites', ['id' => $guestSite], [], ['initialize' => false])->getContent();
} catch (\Omeka\Api\Exception\NotFoundException $e) {
$guestSite = null;
}
}
return $guestSite;
}
protected function deactivateGuests(): void
{
$services = $this->getServiceLocator();
$entityManager = $services->get('Omeka\EntityManager');
$guests = $entityManager->getRepository(\Omeka\Entity\User::class)->findBy(['role' => 'guest']);
foreach ($guests as $user) {
$user->setIsActive(false);
$entityManager->persist($user);
}
$entityManager->flush();
}
}