2121
2222package org .opencastproject .search .impl ;
2323
24- import static org .opencastproject .systems .OpencastConstants .DIGEST_USER_PROPERTY ;
25-
2624import org .opencastproject .elasticsearch .index .ElasticsearchIndex ;
2725import org .opencastproject .elasticsearch .index .rebuild .AbstractIndexProducer ;
2826import org .opencastproject .elasticsearch .index .rebuild .IndexProducer ;
4139import org .opencastproject .search .impl .persistence .SearchServiceDatabaseException ;
4240import org .opencastproject .security .api .AccessControlList ;
4341import org .opencastproject .security .api .AuthorizationService ;
42+ import org .opencastproject .security .api .Organization ;
4443import org .opencastproject .security .api .OrganizationDirectoryService ;
4544import org .opencastproject .security .api .Permissions ;
4645import org .opencastproject .security .api .Role ;
@@ -152,7 +151,7 @@ public SearchServiceIndex() {
152151 @ Activate
153152 public void activate (final ComponentContext cc ) throws IllegalStateException {
154153 createIndex ();
155- systemUserName = cc . getBundleContext (). getProperty ( DIGEST_USER_PROPERTY );
154+ systemUserName = SecurityUtil . getSystemUserName ( cc );
156155 }
157156
158157 private void createIndex () {
@@ -244,14 +243,14 @@ public void addSynchronously(MediaPackage mediaPackage)
244243 }
245244
246245 private void indexMediaPackage (MediaPackage mediaPackage , AccessControlList acl )
247- throws SearchException , UnauthorizedException , SearchServiceDatabaseException {
248- indexMediaPackage (mediaPackage , acl , null , null , securityService . getOrganization (). getId () );
246+ throws SearchException , SearchServiceDatabaseException {
247+ indexMediaPackage (mediaPackage , acl , null , null );
249248 }
250249
251- private void indexMediaPackage (MediaPackage mediaPackage , AccessControlList acl , Date modDate , Date delDate ,
252- String orgId )
253- throws SearchException , UnauthorizedException , SearchServiceDatabaseException {
250+ private void indexMediaPackage (MediaPackage mediaPackage , AccessControlList acl , Date modDate , Date delDate )
251+ throws SearchException , SearchServiceDatabaseException {
254252 String mediaPackageId = mediaPackage .getIdentifier ().toString ();
253+ String orgId = securityService .getOrganization ().getId ();
255254 //If the entry has been deleted then there's *probably* no dc file to load.
256255 DublinCoreCatalog dc = null == delDate
257256 ? DublinCoreUtil .loadEpisodeDublinCore (workspace , mediaPackage ).orElse (DublinCores .mkSimple ())
@@ -452,6 +451,9 @@ public boolean deleteSeriesSynchronously(String seriesId) throws SearchException
452451
453452 @ Override
454453 public void repopulate () throws IndexRebuildException {
454+ final Organization originalOrg = securityService .getOrganization ();
455+ final User originalUser = securityService .getUser ();
456+
455457 try {
456458 int total = persistence .countMediaPackages ();
457459 int pageSize = 50 ;
@@ -465,6 +467,11 @@ public void repopulate() throws IndexRebuildException {
465467 page .forEach (tuple -> {
466468 try {
467469 MediaPackage mediaPackage = tuple .getA ();
470+ Organization organization = organizationDirectory .getOrganization (tuple .getB ());
471+ final var systemUser = SecurityUtil .createSystemUser (systemUserName , organization );
472+ securityService .setUser (systemUser );
473+ securityService .setOrganization (organization );
474+
468475 String mediaPackageId = mediaPackage .getIdentifier ().toString ();
469476
470477 AccessControlList acl = persistence .getAccessControlList (mediaPackageId );
@@ -476,8 +483,9 @@ public void repopulate() throws IndexRebuildException {
476483 logger .debug ("Updating series ACL with merged access control list: {}" , seriesAcl );
477484
478485 current .getAndIncrement ();
479- indexMediaPackage (mediaPackage , acl , modificationDate , deletionDate , tuple .getB ());
480- } catch (SearchServiceDatabaseException | UnauthorizedException e ) {
486+
487+ indexMediaPackage (mediaPackage , acl , modificationDate , deletionDate );
488+ } catch (SearchServiceDatabaseException e ) {
481489 logIndexRebuildError (logger , total , current .get (), e );
482490 //NB: Runtime exception thrown to escape the functional interfacing
483491 throw new RuntimeException ("Internal Index Rebuild Failure" , e );
@@ -493,6 +501,9 @@ public void repopulate() throws IndexRebuildException {
493501 } catch (SearchServiceDatabaseException | RuntimeException e ) {
494502 logIndexRebuildError (logger , e );
495503 throw new IndexRebuildException ("Index Rebuild Failure" , e );
504+ } finally {
505+ securityService .setUser (originalUser );
506+ securityService .setOrganization (originalOrg );
496507 }
497508 }
498509
0 commit comments