-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[kbss-cvut/termit-ui#571] Authorize TermOccurrence modifications (app…
…roval, removal, etc.).
- Loading branch information
Showing
7 changed files
with
181 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...z/cvut/kbss/termit/service/security/authorization/TermOccurrenceAuthorizationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cz.cvut.kbss.termit.service.security.authorization; | ||
|
||
import cz.cvut.kbss.termit.model.Vocabulary; | ||
import cz.cvut.kbss.termit.model.assignment.TermDefinitionalOccurrence; | ||
import cz.cvut.kbss.termit.model.assignment.TermFileOccurrence; | ||
import cz.cvut.kbss.termit.model.assignment.TermOccurrence; | ||
import cz.cvut.kbss.termit.model.resource.Resource; | ||
import cz.cvut.kbss.termit.persistence.dao.TermOccurrenceDao; | ||
import cz.cvut.kbss.termit.service.repository.ResourceRepositoryService; | ||
import cz.cvut.kbss.termit.service.repository.TermRepositoryService; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.net.URI; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
@Service | ||
public class TermOccurrenceAuthorizationService { | ||
|
||
private final TermOccurrenceDao dao; | ||
|
||
private final TermRepositoryService termService; | ||
|
||
private final ResourceRepositoryService resourceService; | ||
|
||
private final VocabularyAuthorizationService vocabularyAuthorizationService; | ||
|
||
private final ResourceAuthorizationService resourceAuthorizationService; | ||
|
||
public TermOccurrenceAuthorizationService(TermOccurrenceDao dao, TermRepositoryService termService, | ||
ResourceRepositoryService resourceService, | ||
VocabularyAuthorizationService vocabularyAuthorizationService, | ||
ResourceAuthorizationService resourceAuthorizationService) { | ||
this.dao = dao; | ||
this.termService = termService; | ||
this.resourceService = resourceService; | ||
this.vocabularyAuthorizationService = vocabularyAuthorizationService; | ||
this.resourceAuthorizationService = resourceAuthorizationService; | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public boolean canModify(TermOccurrence occurrence) { | ||
Objects.requireNonNull(occurrence); | ||
if (occurrence instanceof TermDefinitionalOccurrence definitionalOccurrence) { | ||
final Optional<URI> vocabularyUri = termService.findTermVocabulary( | ||
definitionalOccurrence.getTarget().getSource()); | ||
return vocabularyUri.map(vUri -> vocabularyAuthorizationService.canModify(new Vocabulary(vUri))) | ||
.orElse(false); | ||
} else { | ||
final TermFileOccurrence fo = (TermFileOccurrence) occurrence; | ||
final Optional<Resource> file = resourceService.find(fo.getTarget().getSource()); | ||
return file.map(resourceAuthorizationService::canModify).orElse(false); | ||
} | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public boolean canModify(URI occurrenceId) { | ||
return dao.find(occurrenceId).map(this::canModify).orElse(false); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
...ut/kbss/termit/service/security/authorization/TermOccurrenceAuthorizationServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package cz.cvut.kbss.termit.service.security.authorization; | ||
|
||
import cz.cvut.kbss.termit.environment.Generator; | ||
import cz.cvut.kbss.termit.model.Vocabulary; | ||
import cz.cvut.kbss.termit.model.assignment.DefinitionalOccurrenceTarget; | ||
import cz.cvut.kbss.termit.model.assignment.FileOccurrenceTarget; | ||
import cz.cvut.kbss.termit.model.assignment.TermDefinitionalOccurrence; | ||
import cz.cvut.kbss.termit.model.assignment.TermFileOccurrence; | ||
import cz.cvut.kbss.termit.model.assignment.TermOccurrence; | ||
import cz.cvut.kbss.termit.model.resource.File; | ||
import cz.cvut.kbss.termit.persistence.dao.TermOccurrenceDao; | ||
import cz.cvut.kbss.termit.service.repository.ResourceRepositoryService; | ||
import cz.cvut.kbss.termit.service.repository.TermRepositoryService; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.net.URI; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class TermOccurrenceAuthorizationServiceTest { | ||
|
||
@Mock | ||
private TermOccurrenceDao toDao; | ||
|
||
@Mock | ||
private TermRepositoryService termService; | ||
|
||
@Mock | ||
private ResourceRepositoryService resourceService; | ||
|
||
@Mock | ||
private VocabularyAuthorizationService vocabularyAuthorizationService; | ||
|
||
@Mock | ||
private ResourceAuthorizationService resourceAuthorizationService; | ||
|
||
@InjectMocks | ||
private TermOccurrenceAuthorizationService sut; | ||
|
||
@Test | ||
void canModifyResolvesTermVocabularyAndChecksIfUserCanModifyItWhenTermOccurrenceIsDefinitional() { | ||
final URI vocabularyUri = Generator.generateUri(); | ||
final TermOccurrence to = new TermDefinitionalOccurrence(Generator.generateUri(), | ||
new DefinitionalOccurrenceTarget( | ||
Generator.generateTermWithId(vocabularyUri))); | ||
to.setUri(Generator.generateUri()); | ||
when(termService.findTermVocabulary(to.getTarget().getSource())).thenReturn(Optional.of(vocabularyUri)); | ||
when(vocabularyAuthorizationService.canModify(new Vocabulary(vocabularyUri))).thenReturn(true); | ||
when(toDao.find(to.getUri())).thenReturn(Optional.of(to)); | ||
|
||
assertTrue(sut.canModify(to.getUri())); | ||
verify(vocabularyAuthorizationService).canModify(new Vocabulary(vocabularyUri)); | ||
} | ||
|
||
@Test | ||
void canModifyResolvesResourceVocabularyAndChecksIfUserCanModifyItWhenTermOccurrenceIsFileOccurrence() { | ||
final URI vocabularyUri = Generator.generateUri(); | ||
final File file = Generator.generateFileWithId("test.html"); | ||
file.setDocument(Generator.generateDocumentWithId()); | ||
file.getDocument().setVocabulary(vocabularyUri); | ||
final TermOccurrence to = new TermFileOccurrence(Generator.generateUri(), new FileOccurrenceTarget(file)); | ||
to.setUri(Generator.generateUri()); | ||
when(resourceService.find(file.getUri())).thenReturn(Optional.of(file)); | ||
when(resourceAuthorizationService.canModify(file)).thenReturn(true); | ||
when(toDao.find(to.getUri())).thenReturn(Optional.of(to)); | ||
|
||
assertTrue(sut.canModify(to.getUri())); | ||
verify(resourceAuthorizationService).canModify(file); | ||
} | ||
|
||
@Test | ||
void canModifyReturnsFalseWhenTermOccurrenceDoesNotExist() { | ||
when(toDao.find(any())).thenReturn(Optional.empty()); | ||
assertFalse(sut.canModify(Generator.generateUri())); | ||
} | ||
} |