@@ -434,15 +434,20 @@ private Mono<MailboxId> performConcurrentMailboxCreation(MailboxSession mailboxS
434434 .flatMap (any -> inheritRightsReactive (mailboxSession , mailboxPath ).thenReturn (any ));
435435 }
436436
437-
438- private Mono <Void > assertCanCreateReactive (MailboxSession session , MailboxPath path ) {
437+ private Mono <Boolean > canCreateReactive (MailboxSession session , MailboxPath path ) {
439438 if (path .belongsTo (session )) {
440- return Mono .empty ( );
439+ return Mono .just ( true );
441440 }
442441
443442 return nearestExistingParent (session , path )
444443 .filterWhen (parent -> hasRightReactive (parent , Right .CreateMailbox , session ))
445- .switchIfEmpty (Mono .error (new InsufficientRightsException ("user '" + session .getUser ().asString () + "' is not allowed to create the mailbox '" + path .asString () + "'" )))
444+ .hasElement ();
445+ }
446+
447+ private Mono <Void > assertCanCreateReactive (MailboxSession session , MailboxPath path ) {
448+ return canCreateReactive (session , path )
449+ .filter (canCreate -> canCreate )
450+ .switchIfEmpty (Mono .error (() -> new InsufficientRightsException ("user '" + session .getUser ().asString () + "' is not allowed to create the mailbox '" + path .asString () + "'" )))
446451 .then ();
447452 }
448453
@@ -469,7 +474,7 @@ public void deleteMailbox(final MailboxPath mailboxPath, final MailboxSession se
469474 MailboxMapper mailboxMapper = mailboxSessionMapperFactory .getMailboxMapper (session );
470475
471476 mailboxMapper .execute (() -> block (mailboxMapper .findMailboxByPath (mailboxPath )
472- .filterWhen (mailbox -> assertCanDeleteReactive (session , mailbox . generateAssociatedPath ()). thenReturn ( true ))
477+ .flatMap (mailbox -> assertCanDeleteReactive (session , mailbox ))
473478 .flatMap (mailbox -> doDeleteMailbox (mailboxMapper , mailbox , session ))
474479 .switchIfEmpty (Mono .error (() -> new MailboxNotFoundException (mailboxPath )))));
475480 }
@@ -480,7 +485,7 @@ public Mailbox deleteMailbox(MailboxId mailboxId, MailboxSession session) throws
480485 MailboxMapper mailboxMapper = mailboxSessionMapperFactory .getMailboxMapper (session );
481486
482487 return mailboxMapper .execute (() -> block (mailboxMapper .findMailboxById (mailboxId )
483- .filterWhen (mailbox -> assertCanDeleteReactive (session , mailbox . generateAssociatedPath ()). thenReturn ( true ))
488+ .flatMap (mailbox -> assertCanDeleteReactive (session , mailbox ))
484489 .flatMap (mailbox -> doDeleteMailbox (mailboxMapper , mailbox , session ))));
485490 }
486491
@@ -490,7 +495,7 @@ public Mono<Mailbox> deleteMailboxReactive(MailboxId mailboxId, MailboxSession s
490495 MailboxMapper mailboxMapper = mailboxSessionMapperFactory .getMailboxMapper (session );
491496
492497 return mailboxMapper .executeReactive (mailboxMapper .findMailboxById (mailboxId )
493- .filterWhen (mailbox -> assertCanDeleteReactive (session , mailbox . generateAssociatedPath ()). thenReturn ( true ))
498+ .flatMap (mailbox -> assertCanDeleteReactive (session , mailbox ))
494499 .flatMap (mailbox -> doDeleteMailbox (mailboxMapper , mailbox , session )));
495500 }
496501
@@ -500,33 +505,34 @@ public Mono<Void> deleteMailboxReactive(MailboxPath mailboxPath, MailboxSession
500505 MailboxMapper mailboxMapper = mailboxSessionMapperFactory .getMailboxMapper (session );
501506
502507 return mailboxMapper .executeReactive (mailboxMapper .findMailboxByPath (mailboxPath )
503- .filterWhen (mailbox -> assertCanDeleteReactive (session , mailbox . generateAssociatedPath ()). thenReturn ( true ))
508+ .flatMap (mailbox -> assertCanDeleteReactive (session , mailbox ))
504509 .flatMap (mailbox -> doDeleteMailbox (mailboxMapper , mailbox , session ))
505510 .switchIfEmpty (Mono .error (() -> new MailboxNotFoundException (mailboxPath ))))
506511 .then ();
507512 }
508513
509- private Mono <Void > assertCanDeleteReactive (MailboxSession session , MailboxPath path ) {
514+ private Mono <Mailbox > assertCanDeleteReactive (MailboxSession session , Mailbox mailbox ) {
515+ MailboxPath path = mailbox .generateAssociatedPath ();
510516 if (path .belongsTo (session )) {
511- return Mono .empty ( );
517+ return Mono .just ( mailbox );
512518 }
513519 return Mono .from (hasRightReactive (path , Right .DeleteMailbox , session ))
514520 .flatMap (hasRight -> {
515521 if (hasRight ) {
516- return Mono .empty ( );
522+ return Mono .just ( mailbox );
517523 }
518524 return Mono .error (new InsufficientRightsException ("user '" + session .getUser ().asString () + "' is not allowed to delete the mailbox '" + path .asString () + "'" ));
519525 });
520526 }
521527
522- private Mono <Void > assertCanDeleteWhenRename (MailboxSession session , MailboxPath path ) {
528+ private Mono <MailboxPath > assertCanDeleteWhenRename (MailboxSession session , MailboxPath path ) {
523529 if (path .belongsTo (session )) {
524- return Mono .empty ( );
530+ return Mono .just ( path );
525531 }
526532 return Mono .from (myRightsReactive (path , session ))
527533 .flatMap (rights -> {
528534 if (rights .contains (Right .DeleteMailbox )) {
529- return Mono .empty ( );
535+ return Mono .just ( path );
530536 } else if (!rights .contains (Right .Lookup )) {
531537 return Mono .error (new MailboxNotFoundException (path ));
532538 } else {
@@ -633,7 +639,8 @@ public Mono<List<MailboxRenamedResult>> renameMailboxReactive(MailboxId mailboxI
633639 MailboxMapper mapper = mailboxSessionMapperFactory .getMailboxMapper (session );
634640
635641 Mono <Mailbox > fromMailboxPublisher = mapper .findMailboxById (mailboxId )
636- .filterWhen (mailbox -> assertCanDeleteWhenRename (session , mailbox .generateAssociatedPath ()).thenReturn (true ))
642+ .flatMap (mailbox -> assertCanDeleteWhenRename (session , mailbox .generateAssociatedPath ())
643+ .thenReturn (mailbox ))
637644 .switchIfEmpty (Mono .error (() -> new MailboxNotFoundException (mailboxId )));
638645
639646 return sanitizedMailboxPath (newMailboxPath , session )
@@ -642,16 +649,15 @@ public Mono<List<MailboxRenamedResult>> renameMailboxReactive(MailboxId mailboxI
642649
643650 private Mono <MailboxPath > sanitizedMailboxPath (MailboxPath mailboxPath , MailboxSession session ) {
644651 Function <MailboxPath , Mono <Boolean >> assertNewMailboxPathDoesNotExist = newMailboxPath -> mailboxExists (mailboxPath , session )
645- .filter (FunctionalUtils .identityPredicate ().negate ())
646- .switchIfEmpty (Mono .error (new MailboxExistsException (mailboxPath .toString ())))
647- .thenReturn (true );
652+ .map (FunctionalUtils .negate ());
648653
649- Function <MailboxPath , Mono <Boolean >> assertRightToCreate = newMailboxPath -> assertCanCreateReactive (session , mailboxPath )
650- .thenReturn (true );
654+ Function <MailboxPath , Mono <Boolean >> assertRightToCreate = newMailboxPath -> canCreateReactive (session , mailboxPath );
651655
652656 return Mono .fromCallable (() -> mailboxPath .sanitize (session .getPathDelimiter ()))
653657 .filterWhen (assertNewMailboxPathDoesNotExist )
658+ .switchIfEmpty (Mono .error (() -> new MailboxExistsException (mailboxPath .toString ())))
654659 .filterWhen (assertRightToCreate )
660+ .switchIfEmpty (Mono .error (() -> new InsufficientRightsException ("user '" + session .getUser ().asString () + "' is not allowed to create the mailbox '" + mailboxPath .asString () + "'" )))
655661 .flatMap (newMailboxPath -> Mono .fromCallable (() -> newMailboxPath .assertAcceptable (session .getPathDelimiter ())));
656662 }
657663
0 commit comments