-
Notifications
You must be signed in to change notification settings - Fork 4
/
Module.php
680 lines (654 loc) · 33 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
<?php
namespace Datascribe;
use Composer\Semver\Comparator;
use Datascribe\Api\Adapter\DatascribeDatasetAdapter;
use Datascribe\Api\Adapter\DatascribeProjectAdapter;
use Datascribe\Entity\DatascribeUser;
use Datascribe\PermissionsAssertion\IsDatascribeUserAssertion;
use Datascribe\PermissionsAssertion\ReviewerCanAssertion;
use Datascribe\PermissionsAssertion\TranscriberCanAssertion;
use Omeka\Api\Exception\PermissionDeniedException;
use Omeka\Module\AbstractModule;
use Omeka\Permissions\Acl;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\Mvc\MvcEvent;
use Laminas\Permissions\Acl\Assertion\AssertionAggregate;
use Laminas\ServiceManager\ServiceLocatorInterface;
class Module extends AbstractModule
{
public function getConfig()
{
return include sprintf('%s/config/module.config.php', __DIR__);
}
public function onBootstrap(MvcEvent $event)
{
parent::onBootstrap($event);
$this->addAclRules();
// Set the corresponding visibility rules on DataScribe items.
$em = $this->getServiceLocator()->get('Omeka\EntityManager');
$filter = $em->getFilters()->getFilter('resource_visibility');
$filter->addRelatedEntity('Datascribe\Entity\DatascribeItem', 'item_id');
}
public function install(ServiceLocatorInterface $services)
{
$sql = <<<'SQL'
CREATE TABLE datascribe_record (id INT UNSIGNED AUTO_INCREMENT NOT NULL, item_id INT UNSIGNED NOT NULL, owner_id INT DEFAULT NULL, created_by_id INT DEFAULT NULL, modified_by_id INT DEFAULT NULL, needs_review TINYINT(1) DEFAULT '0' NOT NULL, needs_work TINYINT(1) DEFAULT '0' NOT NULL, position INT DEFAULT NULL, created DATETIME NOT NULL, modified DATETIME DEFAULT NULL, transcriber_notes LONGTEXT DEFAULT NULL, reviewer_notes LONGTEXT DEFAULT NULL, INDEX IDX_5628651126F525E (item_id), INDEX IDX_56286517E3C61F9 (owner_id), INDEX IDX_5628651B03A8386 (created_by_id), INDEX IDX_562865199049ECE (modified_by_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;
CREATE TABLE datascribe_value (id INT UNSIGNED AUTO_INCREMENT NOT NULL, field_id INT UNSIGNED NOT NULL, record_id INT UNSIGNED NOT NULL, is_invalid TINYINT(1) DEFAULT '0' NOT NULL, is_missing TINYINT(1) DEFAULT '0' NOT NULL, is_illegible TINYINT(1) DEFAULT '0' NOT NULL, text LONGTEXT DEFAULT NULL, INDEX IDX_2FFB3B33443707B0 (field_id), INDEX IDX_2FFB3B334DFD750C (record_id), UNIQUE INDEX UNIQ_2FFB3B33443707B04DFD750C (field_id, record_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;
CREATE TABLE datascribe_project (id INT UNSIGNED AUTO_INCREMENT NOT NULL, owner_id INT DEFAULT NULL, created_by_id INT DEFAULT NULL, modified_by_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, description LONGTEXT DEFAULT NULL, created DATETIME NOT NULL, modified DATETIME DEFAULT NULL, is_public TINYINT(1) DEFAULT '0' NOT NULL, INDEX IDX_B44944477E3C61F9 (owner_id), INDEX IDX_B4494447B03A8386 (created_by_id), INDEX IDX_B449444799049ECE (modified_by_id), UNIQUE INDEX UNIQ_B44944475E237E06 (name), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;
CREATE TABLE datascribe_dataset (id INT UNSIGNED AUTO_INCREMENT NOT NULL, project_id INT UNSIGNED NOT NULL, item_set_id INT DEFAULT NULL, validated_by_id INT DEFAULT NULL, exported_by_id INT DEFAULT NULL, synced_by_id INT DEFAULT NULL, owner_id INT DEFAULT NULL, created_by_id INT DEFAULT NULL, modified_by_id INT DEFAULT NULL, guidelines LONGTEXT DEFAULT NULL, revert_review_status TINYINT(1) DEFAULT '0' NOT NULL, export_missing_illegible TINYINT(1) DEFAULT '0' NOT NULL, export_storage_id VARCHAR(255) DEFAULT NULL, validated DATETIME DEFAULT NULL, exported DATETIME DEFAULT NULL, name VARCHAR(255) NOT NULL, description LONGTEXT DEFAULT NULL, synced DATETIME DEFAULT NULL, created DATETIME NOT NULL, modified DATETIME DEFAULT NULL, is_public TINYINT(1) DEFAULT '0' NOT NULL, INDEX IDX_2C5AD579166D1F9C (project_id), INDEX IDX_2C5AD579960278D7 (item_set_id), INDEX IDX_2C5AD579C69DE5E5 (validated_by_id), INDEX IDX_2C5AD579F748B80E (exported_by_id), INDEX IDX_2C5AD5797141DBAD (synced_by_id), INDEX IDX_2C5AD5797E3C61F9 (owner_id), INDEX IDX_2C5AD579B03A8386 (created_by_id), INDEX IDX_2C5AD57999049ECE (modified_by_id), UNIQUE INDEX UNIQ_2C5AD579166D1F9C5E237E06 (project_id, name), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;
CREATE TABLE datascribe_user (id INT UNSIGNED AUTO_INCREMENT NOT NULL, project_id INT UNSIGNED NOT NULL, user_id INT DEFAULT NULL, role VARCHAR(255) NOT NULL, INDEX IDX_D99C3265166D1F9C (project_id), INDEX IDX_D99C3265A76ED395 (user_id), UNIQUE INDEX UNIQ_D99C3265166D1F9CA76ED395 (project_id, user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;
CREATE TABLE datascribe_field (id INT UNSIGNED AUTO_INCREMENT NOT NULL, dataset_id INT UNSIGNED NOT NULL, name VARCHAR(255) NOT NULL, description LONGTEXT DEFAULT NULL, position INT NOT NULL, is_primary TINYINT(1) DEFAULT '0' NOT NULL, is_required TINYINT(1) DEFAULT '0' NOT NULL, data_type VARCHAR(255) NOT NULL, data LONGTEXT NOT NULL COMMENT '(DC2Type:json)', INDEX IDX_6979265FD47C2D1B (dataset_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;
CREATE TABLE datascribe_item (id INT UNSIGNED AUTO_INCREMENT NOT NULL, dataset_id INT UNSIGNED NOT NULL, item_id INT NOT NULL, prioritized_by_id INT DEFAULT NULL, locked_by_id INT DEFAULT NULL, submitted_by_id INT DEFAULT NULL, reviewed_by_id INT DEFAULT NULL, synced_by_id INT DEFAULT NULL, prioritized DATETIME DEFAULT NULL, locked DATETIME DEFAULT NULL, submitted DATETIME DEFAULT NULL, reviewed DATETIME DEFAULT NULL, is_approved TINYINT(1) DEFAULT NULL, synced DATETIME DEFAULT NULL, transcriber_notes LONGTEXT DEFAULT NULL, reviewer_notes LONGTEXT DEFAULT NULL, INDEX IDX_4B14C132D47C2D1B (dataset_id), INDEX IDX_4B14C132126F525E (item_id), INDEX IDX_4B14C1324A0B323 (prioritized_by_id), INDEX IDX_4B14C1327A88E00 (locked_by_id), INDEX IDX_4B14C13279F7D87D (submitted_by_id), INDEX IDX_4B14C132FC6B21F1 (reviewed_by_id), INDEX IDX_4B14C1327141DBAD (synced_by_id), UNIQUE INDEX UNIQ_4B14C132D47C2D1B126F525E (dataset_id, item_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;
ALTER TABLE datascribe_record ADD CONSTRAINT FK_5628651126F525E FOREIGN KEY (item_id) REFERENCES datascribe_item (id) ON DELETE CASCADE;
ALTER TABLE datascribe_record ADD CONSTRAINT FK_56286517E3C61F9 FOREIGN KEY (owner_id) REFERENCES user (id) ON DELETE SET NULL;
ALTER TABLE datascribe_record ADD CONSTRAINT FK_5628651B03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id) ON DELETE SET NULL;
ALTER TABLE datascribe_record ADD CONSTRAINT FK_562865199049ECE FOREIGN KEY (modified_by_id) REFERENCES user (id) ON DELETE SET NULL;
ALTER TABLE datascribe_value ADD CONSTRAINT FK_2FFB3B33443707B0 FOREIGN KEY (field_id) REFERENCES datascribe_field (id) ON DELETE CASCADE;
ALTER TABLE datascribe_value ADD CONSTRAINT FK_2FFB3B334DFD750C FOREIGN KEY (record_id) REFERENCES datascribe_record (id) ON DELETE CASCADE;
ALTER TABLE datascribe_project ADD CONSTRAINT FK_B44944477E3C61F9 FOREIGN KEY (owner_id) REFERENCES user (id) ON DELETE SET NULL;
ALTER TABLE datascribe_project ADD CONSTRAINT FK_B4494447B03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id) ON DELETE SET NULL;
ALTER TABLE datascribe_project ADD CONSTRAINT FK_B449444799049ECE FOREIGN KEY (modified_by_id) REFERENCES user (id) ON DELETE SET NULL;
ALTER TABLE datascribe_dataset ADD CONSTRAINT FK_2C5AD579166D1F9C FOREIGN KEY (project_id) REFERENCES datascribe_project (id) ON DELETE CASCADE;
ALTER TABLE datascribe_dataset ADD CONSTRAINT FK_2C5AD579960278D7 FOREIGN KEY (item_set_id) REFERENCES item_set (id) ON DELETE SET NULL;
ALTER TABLE datascribe_dataset ADD CONSTRAINT FK_2C5AD579C69DE5E5 FOREIGN KEY (validated_by_id) REFERENCES user (id) ON DELETE SET NULL;
ALTER TABLE datascribe_dataset ADD CONSTRAINT FK_2C5AD579F748B80E FOREIGN KEY (exported_by_id) REFERENCES user (id) ON DELETE SET NULL;
ALTER TABLE datascribe_dataset ADD CONSTRAINT FK_2C5AD5797141DBAD FOREIGN KEY (synced_by_id) REFERENCES user (id) ON DELETE SET NULL;
ALTER TABLE datascribe_dataset ADD CONSTRAINT FK_2C5AD5797E3C61F9 FOREIGN KEY (owner_id) REFERENCES user (id) ON DELETE SET NULL;
ALTER TABLE datascribe_dataset ADD CONSTRAINT FK_2C5AD579B03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id) ON DELETE SET NULL;
ALTER TABLE datascribe_dataset ADD CONSTRAINT FK_2C5AD57999049ECE FOREIGN KEY (modified_by_id) REFERENCES user (id) ON DELETE SET NULL;
ALTER TABLE datascribe_user ADD CONSTRAINT FK_D99C3265166D1F9C FOREIGN KEY (project_id) REFERENCES datascribe_project (id) ON DELETE CASCADE;
ALTER TABLE datascribe_user ADD CONSTRAINT FK_D99C3265A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE SET NULL;
ALTER TABLE datascribe_field ADD CONSTRAINT FK_6979265FD47C2D1B FOREIGN KEY (dataset_id) REFERENCES datascribe_dataset (id) ON DELETE CASCADE;
ALTER TABLE datascribe_item ADD CONSTRAINT FK_4B14C132D47C2D1B FOREIGN KEY (dataset_id) REFERENCES datascribe_dataset (id) ON DELETE CASCADE;
ALTER TABLE datascribe_item ADD CONSTRAINT FK_4B14C132126F525E FOREIGN KEY (item_id) REFERENCES item (id) ON DELETE CASCADE;
ALTER TABLE datascribe_item ADD CONSTRAINT FK_4B14C1324A0B323 FOREIGN KEY (prioritized_by_id) REFERENCES user (id) ON DELETE SET NULL;
ALTER TABLE datascribe_item ADD CONSTRAINT FK_4B14C1327A88E00 FOREIGN KEY (locked_by_id) REFERENCES user (id) ON DELETE SET NULL;
ALTER TABLE datascribe_item ADD CONSTRAINT FK_4B14C13279F7D87D FOREIGN KEY (submitted_by_id) REFERENCES user (id) ON DELETE SET NULL;
ALTER TABLE datascribe_item ADD CONSTRAINT FK_4B14C132FC6B21F1 FOREIGN KEY (reviewed_by_id) REFERENCES user (id) ON DELETE SET NULL;
ALTER TABLE datascribe_item ADD CONSTRAINT FK_4B14C1327141DBAD FOREIGN KEY (synced_by_id) REFERENCES user (id) ON DELETE SET NULL;
SQL;
$conn = $services->get('Omeka\Connection');
$conn->exec('SET FOREIGN_KEY_CHECKS=0;');
$conn->exec($sql);
$conn->exec('SET FOREIGN_KEY_CHECKS=1;');
}
public function uninstall(ServiceLocatorInterface $services)
{
$conn = $services->get('Omeka\Connection');
$conn->exec('SET FOREIGN_KEY_CHECKS=0;');
$conn->exec('DROP TABLE IF EXISTS datascribe_user;');
$conn->exec('DROP TABLE IF EXISTS datascribe_value;');
$conn->exec('DROP TABLE IF EXISTS datascribe_record;');
$conn->exec('DROP TABLE IF EXISTS datascribe_item;');
$conn->exec('DROP TABLE IF EXISTS datascribe_field;');
$conn->exec('DROP TABLE IF EXISTS datascribe_dataset;');
$conn->exec('DROP TABLE IF EXISTS datascribe_project;');
$conn->exec('SET FOREIGN_KEY_CHECKS=1;');
}
public function upgrade($oldVersion, $newVersion, ServiceLocatorInterface $services)
{
$conn = $services->get('Omeka\Connection');
if (Comparator::lessThan($oldVersion, '0.2.0')) {
// Add the record position column and populate the records with
// consecutive positive integers per item.
$conn->exec('
ALTER TABLE datascribe_record
ADD COLUMN position INT DEFAULT NULL AFTER needs_work');
$itemStmt = $conn->query('SELECT id FROM datascribe_item');
$recordStmt = $conn->prepare('
UPDATE datascribe_record
SET position = (@position := @position + 1)
WHERE item_id = ?
ORDER BY id');
while ($row = $itemStmt->fetch()) {
$conn->exec('SET @position := 0');
$recordStmt->bindValue(1, (int) $row['id']);
$recordStmt->execute();
}
}
if (Comparator::lessThan($oldVersion, '1.0.0-beta')) {
// Add the "revert review status" dataset configuration column.
$conn->exec("
ALTER TABLE datascribe_dataset
ADD COLUMN revert_review_status TINYINT(1) DEFAULT '0' NOT NULL AFTER guidelines");
}
if (Comparator::lessThan($oldVersion, '1.0.1')) {
// Add the "export missing illegible" dataset configuration column.
$conn->exec("
ALTER TABLE datascribe_dataset
ADD COLUMN export_missing_illegible TINYINT(1) DEFAULT '0' NOT NULL AFTER revert_review_status");
}
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager)
{
$sharedEventManager->attach(
'Datascribe\Api\Adapter\DatascribeProjectAdapter',
'api.search.query',
[$this, 'filterForVisibility']
);
$sharedEventManager->attach(
'Datascribe\Api\Adapter\DatascribeProjectAdapter',
'api.find.query',
[$this, 'filterForVisibility']
);
$sharedEventManager->attach(
'Datascribe\Api\Adapter\DatascribeDatasetAdapter',
'api.search.query',
[$this, 'filterForVisibility']
);
$sharedEventManager->attach(
'Datascribe\Api\Adapter\DatascribeDatasetAdapter',
'api.find.query',
[$this, 'filterForVisibility']
);
$sharedEventManager->attach(
'Datascribe\Entity\DatascribeRecord',
'entity.persist.pre',
[$this, 'assertCreateRecordPrivilege']
);
$sharedEventManager->attach(
'Datascribe\Entity\DatascribeRecord',
'entity.persist.pre',
[$this, 'handleRecordPositionChange'],
-100
);
$sharedEventManager->attach(
'Datascribe\Entity\DatascribeRecord',
'entity.update.pre',
[$this, 'handleRecordPositionChange'],
-100
);
$sharedEventManager->attach(
'Datascribe\Entity\DatascribeRecord',
'entity.remove.post',
[$this, 'restoreRecordPositions'],
-100
);
$controllers = [
'Datascribe\Controller\Admin\Index',
'Datascribe\Controller\Admin\Project',
'Datascribe\Controller\Admin\Dataset',
'Datascribe\Controller\Admin\DatasetRecord',
'Datascribe\Controller\Admin\Item',
'Datascribe\Controller\Admin\Record',
];
foreach ($controllers as $controller) {
$sharedEventManager->attach(
$controller,
'view.layout',
[$this, 'appendToLayout']
);
}
}
/**
* Add ACL rules for this module.
*/
protected function addAclRules()
{
$acl = $this->getServiceLocator()->get('Omeka\Acl');
// Set controller/action privileges.
$acl->allow(
null,
'Datascribe\Controller\Admin\Index',
['index']
);
$acl->allow(
null,
'Datascribe\Controller\Admin\Project',
['browse', 'show-details', 'show']
);
$acl->allow(
null,
'Datascribe\Controller\Admin\Dataset',
['browse', 'show-details', 'show']
);
$acl->allow(
null,
'Datascribe\Controller\Admin\Item',
['browse', 'show-details', 'search', 'show', 'batch-edit', 'batch-edit-all']
);
$acl->allow(
null,
'Datascribe\Controller\Admin\Record',
['browse', 'show-details', 'search', 'show', 'batch-edit', 'batch-edit-all', 'add', 'edit', 'delete']
);
// Set API adapter privileges.
$acl->allow(
null,
'Datascribe\Api\Adapter\DatascribeProjectAdapter',
[
'search',
'read',
'update',
]
);
$acl->allow(
null,
'Datascribe\Api\Adapter\DatascribeDatasetAdapter',
[
'search',
'read',
'update',
'datascribe_view_item_batch_update',
'datascribe_view_record_batch_actions',
]
);
$acl->allow(
null,
'Datascribe\Api\Adapter\DatascribeItemAdapter',
[
'search',
'read',
'update',
'batch_update',
'datascribe_view_record_batch_actions',
'datascribe_mark_item_submitted',
'datascribe_mark_item_not_submitted',
'datascribe_mark_item_approved',
'datascribe_mark_item_not_reviewed',
'datascribe_mark_item_not_approved',
'datascribe_edit_transcriber_notes',
'datascribe_edit_reviewer_notes',
'datascribe_unlock_item',
'datascribe_lock_item_to_self',
'datascribe_lock_item_to_other',
'datascribe_mark_item_prioritized',
'datascribe_mark_item_not_prioritized',
'datascribe_edit_submit_action',
'datascribe_edit_review_action',
'datascribe_edit_lock_action',
'datascribe_edit_priority_action',
'datascribe_add_record',
'datascribe_flag_record_needs_review',
'datascribe_flag_record_needs_work',
'datascribe_change_record_position',
]
);
$acl->allow(
null,
'Datascribe\Api\Adapter\DatascribeRecordAdapter',
[
'search',
'read',
'create',
'update',
'delete',
]
);
// Set entity privileges.
$userCanAssertion = new AssertionAggregate;
$userCanAssertion->addAssertions([
new ReviewerCanAssertion,
new TranscriberCanAssertion,
]);
$userCanAssertion->setMode(AssertionAggregate::MODE_AT_LEAST_ONE);
// If an Omeka admin user is also a DataScribe project user, deny all
// privileges specific to DataScribe items and records. Their access
// will rely on their respective reviewer or transcriber privileges.
$acl->deny(
[
Acl::ROLE_GLOBAL_ADMIN,
Acl::ROLE_SITE_ADMIN,
],
[
'Datascribe\Entity\DatascribeItem',
'Datascribe\Entity\DatascribeRecord',
],
null,
new IsDatascribeUserAssertion
);
$acl->allow(
[
Acl::ROLE_GLOBAL_ADMIN,
Acl::ROLE_SITE_ADMIN,
Acl::ROLE_EDITOR,
Acl::ROLE_REVIEWER,
Acl::ROLE_AUTHOR,
Acl::ROLE_RESEARCHER,
],
[
'Datascribe\Entity\DatascribeProject',
'Datascribe\Entity\DatascribeDataset',
'Datascribe\Entity\DatascribeItem',
'Datascribe\Entity\DatascribeRecord',
],
'read'
);
$acl->allow(
[
Acl::ROLE_GLOBAL_ADMIN,
Acl::ROLE_SITE_ADMIN,
Acl::ROLE_EDITOR,
Acl::ROLE_REVIEWER,
Acl::ROLE_AUTHOR,
Acl::ROLE_RESEARCHER,
],
'Datascribe\Entity\DatascribeDataset',
[
'datascribe_view_item_batch_update',
'datascribe_view_record_batch_actions',
],
new ReviewerCanAssertion
);
$acl->allow(
[
Acl::ROLE_GLOBAL_ADMIN,
Acl::ROLE_SITE_ADMIN,
Acl::ROLE_EDITOR,
Acl::ROLE_REVIEWER,
Acl::ROLE_AUTHOR,
Acl::ROLE_RESEARCHER,
],
'Datascribe\Entity\DatascribeItem',
[
'batch_update',
'datascribe_view_record_batch_actions',
'datascribe_mark_item_approved',
'datascribe_mark_item_not_reviewed',
'datascribe_mark_item_not_approved',
'datascribe_edit_reviewer_notes',
'datascribe_lock_item_to_other',
'datascribe_mark_item_prioritized',
'datascribe_mark_item_not_prioritized',
'datascribe_edit_review_action',
'datascribe_edit_priority_action',
'datascribe_flag_record_needs_work',
],
new ReviewerCanAssertion
);
$acl->allow(
[
Acl::ROLE_GLOBAL_ADMIN,
Acl::ROLE_SITE_ADMIN,
Acl::ROLE_EDITOR,
Acl::ROLE_REVIEWER,
Acl::ROLE_AUTHOR,
Acl::ROLE_RESEARCHER,
],
'Datascribe\Entity\DatascribeItem',
[
'update',
'datascribe_mark_item_submitted',
'datascribe_mark_item_not_submitted',
'datascribe_edit_transcriber_notes',
'datascribe_unlock_item',
'datascribe_lock_item_to_self',
'datascribe_edit_submit_action',
'datascribe_edit_lock_action',
'datascribe_add_record',
'datascribe_flag_record_needs_review',
'datascribe_change_record_position',
],
$userCanAssertion
);
// Note that we allow general create privilege for records because we
// rely on special handling in self::assertCreateRecordPrivilege().
$acl->allow(
[
Acl::ROLE_GLOBAL_ADMIN,
Acl::ROLE_SITE_ADMIN,
Acl::ROLE_EDITOR,
Acl::ROLE_REVIEWER,
Acl::ROLE_AUTHOR,
Acl::ROLE_RESEARCHER,
],
'Datascribe\Entity\DatascribeRecord',
'create'
);
$acl->allow(
[
Acl::ROLE_GLOBAL_ADMIN,
Acl::ROLE_SITE_ADMIN,
Acl::ROLE_EDITOR,
Acl::ROLE_REVIEWER,
Acl::ROLE_AUTHOR,
Acl::ROLE_RESEARCHER,
],
'Datascribe\Entity\DatascribeRecord',
[
'update',
'delete',
],
$userCanAssertion
);
}
/**
* Filter for visibility (projects and datasets).
*
* @param Event $event
*/
public function filterForVisibility(Event $event)
{
$qb = $event->getParam('queryBuilder');
// Users can view projects that are public.
$expression = $qb->expr()->eq('omeka_root.isPublic', true);
$auth = $this->getServiceLocator()->get('Omeka\AuthenticationService');
$acl = $this->getServiceLocator()->get('Omeka\Acl');
if ($auth->hasIdentity()) {
$identity = $auth->getIdentity();
if ($acl->isAdminRole($identity->getRole())) {
// Admin users can view all projects.
return;
}
$adapter = $event->getTarget();
if ($adapter instanceof DatascribeDatasetAdapter) {
// Filter datasets
$projectAlias = $adapter->createAlias();
$qb->leftJoin('omeka_root.project', $projectAlias);
$userAlias = $adapter->createAlias();
$qb->leftJoin("$projectAlias.users", $userAlias);
} elseif ($adapter instanceof DatascribeProjectAdapter) {
// Filter projects
$userAlias = $adapter->createAlias();
$qb->leftJoin('omeka_root.users', $userAlias);
} else {
// Not a filterable adapter.
return;
}
$expression = $qb->expr()->orX(
$expression,
// Users can view projects that they belong to.
$qb->expr()->eq(
"$userAlias.user",
$adapter->createNamedParameter($qb, $identity)
)
);
}
$qb->andWhere($expression);
}
/**
* Assert that the current user has permission to create a DataScribe record.
*
* Asserting create privilege for a record is a special case that cannot be
* done using the ACL becuase the ACL checks permissions before the record
* is hydrated, so there would be no way for the existing ACL assertions to
* tell if the current user has permissions to transcribe the project.
*
* @throws PermissionDeniedException
* @param Event $event
*/
public function assertCreateRecordPrivilege(Event $event)
{
$services = $this->getServiceLocator();
$user = $services->get('Omeka\AuthenticationService')->getIdentity();
$userIsAdmin = in_array($user->getRole(), [Acl::ROLE_GLOBAL_ADMIN, Acl::ROLE_SITE_ADMIN]);
$errorMessage = 'Permission denied for the current user to create the Datascribe\Entity\DatascribeRecord resource.';
$record = $event->getTarget();
$item = $record->getItem();
$dataset = $item->getDataset();
$project = $dataset->getProject();
$projectUser = $project->getUsers()->get($user->getId());
if ($projectUser) {
if (DatascribeUser::ROLE_TRANSCRIBER === $projectUser->getRole()) {
// - The item must be locked to the transcriber
// - AND the item must not be approved
$itemIsNotLockedByUser = ($user !== $item->getLockedBy());
$itemIsApproved = (true === $item->getIsApproved());
if ($itemIsNotLockedByUser || $itemIsApproved) {
throw new PermissionDeniedException($errorMessage);
}
}
} elseif (!$userIsAdmin) {
// The user is not assigned to this project and is not an admin.
throw new PermissionDeniedException($errorMessage);
}
}
/**
* Handle a position change before persisting or updating a record.
*
* This assures that an item's record positions remain as consecutive
* positive integers (1,2,3,...).
*
* This could be done in the record adapter, but given that it will likely
* affect the state of other records, it's safer to do at the last possible
* moment (note the low priority on attach()). Otherwise, a permission error
* or some other error could happen after the state changes.
*
* @param Event $event
*/
public function handleRecordPositionChange(Event $event)
{
$services = $this->getServiceLocator();
$conn = $services->get('Omeka\Connection');
$record = $event->getTarget();
$item = $record->getItem();
$positionChange = $record->getPositionChange();
// Get the original position. Return if no position change is needed or
// if no adjustments to the positions of the surrounding records need to
// be made.
if ($record->getId()) {
// This is an existing record.
if (null === $positionChange) {
// An existing record keeps its current position by default.
return;
}
if ($record->getId() === $positionChange['record_id']) {
// An existing record cannot be before or after itself.
return;
}
$originalPosition = $record->getPosition();
} else {
// This is a new record.
$sql = '
SELECT MAX(position) AS max_position
FROM datascribe_record
WHERE item_id = ?';
$maxPosition = (int) $conn->fetchColumn($sql, [$item->getId()], 0);
if (null === $positionChange) {
// A new record has the maximum position by default.
$record->setPosition($maxPosition + 1);
return;
}
$originalPosition = $maxPosition + 1;
}
// Get the reference position.
$sql = 'SELECT position FROM datascribe_record WHERE id = ?';
$referencePosition = (int) $conn->fetchColumn($sql, [$positionChange['record_id']], 0);
// Set the new position and make adjustments to the positions of the
// surrounding records to maintain consecutive order.
if ('before' === $positionChange['direction']) {
if ($originalPosition < $referencePosition) {
// Starting at the original position number, increment from
// where the position is greater than the original position to
// where the position is less than the reference position, then
// set the new position to one less than the reference position.
$conn->exec(sprintf('SET @position := %d', $originalPosition - 1));
$sql = '
UPDATE datascribe_record
SET position = (@position := @position + 1)
WHERE item_id = ?
AND position > ?
AND position < ?
ORDER BY position ASC';
$conn->executeUpdate($sql, [$item->getId(), $originalPosition, $referencePosition]);
$record->setPosition($referencePosition - 1);
}
if ($originalPosition > $referencePosition) {
// Starting at one more than the reference position number,
// increment from where the position is greater than or equal to
// the reference position to where the position is less than the
// original position, then set the new position to the reference
// position.
$conn->exec(sprintf('SET @position := %d', $referencePosition));
$sql = '
UPDATE datascribe_record
SET position = (@position := @position + 1)
WHERE item_id = ?
AND position >= ?
AND position < ?
ORDER BY position ASC';
$conn->executeUpdate($sql, [$item->getId(), $referencePosition, $originalPosition]);
$record->setPosition($referencePosition);
}
}
if ('after' === $positionChange['direction']) {
if ($originalPosition < $referencePosition) {
// Starting at one less than the original position, increment
// from where the position is greater than the original position
// to where the position is less than or equal to the reference
// position, then set the new position to the reference position.
$conn->exec(sprintf('SET @position := %d', $originalPosition - 1));
$sql = '
UPDATE datascribe_record
SET position = (@position := @position + 1)
WHERE item_id = ?
AND position > ?
AND position <= ?
ORDER BY position ASC';
$conn->executeUpdate($sql, [$item->getId(), $originalPosition, $referencePosition]);
$record->setPosition($referencePosition);
}
if ($originalPosition > $referencePosition) {
// Starting at the reference position, increment from where the
// position is greater than the reference position to where the
// position is less than the original position, then set the new
// position to one more than the reference position.
$conn->exec(sprintf('SET @position := %d', $referencePosition + 1));
$sql = '
UPDATE datascribe_record
SET position = (@position := @position + 1)
WHERE item_id = ?
AND position > ?
AND position < ?
ORDER BY position ASC';
$conn->executeUpdate($sql, [$item->getId(), $referencePosition, $originalPosition]);
$record->setPosition($referencePosition + 1);
}
}
}
/**
* Restore consecutive (1,2,3,...) record position for an item.
*
* Note that this does not reset the positions to their default state
* (ordered by record ID), rather it restores positions after a gap in
* position is made (typically when a record is deleted).
*
* @param Event $event
*/
public function restoreRecordPositions(Event $event)
{
$services = $this->getServiceLocator();
$conn = $services->get('Omeka\Connection');
$item = $event->getTarget()->getItem();
$conn->exec(sprintf('SET @position := %d', 0));
$sql = '
UPDATE datascribe_record
SET position = (@position := @position + 1)
WHERE item_id = ?
ORDER BY position ASC';
$conn->executeUpdate($sql, [$item->getId()]);
}
/**
* Append stylesheets and scripts to all DataScribe pages.
*
* @param Event $event
*/
public function appendToLayout(Event $event)
{
$view = $event->getTarget();
$view->headLink()->appendStylesheet($view->assetUrl('css/admin.css', 'Datascribe'));
$view->headScript()->prependFile($view->assetUrl('js/datascribe.js', 'Datascribe'));
$view->headScript()->appendFile($view->assetUrl('js/admin.js', 'Datascribe'));
}
}