Search in sources :

Example 1 with VreAuthorization

use of nl.knaw.huygens.timbuctoo.security.dto.VreAuthorization in project timbuctoo by HuygensING.

the class LocalFileVreAuthorizationAccess method getOrCreateAuthorization.

@Override
public VreAuthorization getOrCreateAuthorization(String vreId, String userId, String userRole) throws AuthorizationUnavailableException {
    Optional<VreAuthorization> authOptional = getAuthorization(vreId, userId);
    if (authOptional.isPresent()) {
        return authOptional.get();
    } else {
        try {
            synchronized (authorizationsFolder) {
                File file = getFileOfVre(vreId);
                List<VreAuthorization> authorizations = Lists.newArrayList();
                if (file.exists()) {
                    authorizations = objectMapper.readValue(file, new TypeReference<List<VreAuthorization>>() {
                    });
                }
                VreAuthorization vreAuthorization = VreAuthorization.create(vreId, userId, userRole);
                authorizations.add(vreAuthorization);
                objectMapper.writeValue(file, authorizations.toArray(new VreAuthorization[authorizations.size()]));
                return vreAuthorization;
            }
        } catch (IOException e) {
            throw new AuthorizationUnavailableException(e.getMessage());
        }
    }
}
Also used : TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException) File(java.io.File) AuthorizationUnavailableException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.AuthorizationUnavailableException) VreAuthorization(nl.knaw.huygens.timbuctoo.security.dto.VreAuthorization)

Example 2 with VreAuthorization

use of nl.knaw.huygens.timbuctoo.security.dto.VreAuthorization in project timbuctoo by HuygensING.

the class BasicPermissionFetcherTest method getPermissionsReturnsReadPermissionOnlyUserWithoutWritePermissionInDataSet.

@Test
public void getPermissionsReturnsReadPermissionOnlyUserWithoutWritePermissionInDataSet() throws Exception {
    VreAuthorization vreAuthorization = mock(VreAuthorization.class);
    given(vreAuthorization.isAllowedToWrite()).willReturn(false);
    given(vreAuthorizationCrud.getAuthorization(anyString(), any(User.class))).willReturn(Optional.of(vreAuthorization));
    Set<Permission> permissions = permissionFetcher.getPermissions(mock(User.class), dataSetMetaData);
    assertThat(permissions, contains(Permission.READ));
}
Also used : User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) Permission(nl.knaw.huygens.timbuctoo.v5.security.dto.Permission) VreAuthorization(nl.knaw.huygens.timbuctoo.security.dto.VreAuthorization) Test(org.junit.Test)

Example 3 with VreAuthorization

use of nl.knaw.huygens.timbuctoo.security.dto.VreAuthorization in project timbuctoo by HuygensING.

the class BasicPermissionFetcherTest method getPermissionsReturnsPermissionsForGivenUserAndDataSet.

@Test
public void getPermissionsReturnsPermissionsForGivenUserAndDataSet() throws Exception {
    VreAuthorization vreAuthorization = mock(VreAuthorization.class);
    given(vreAuthorization.isAllowedToWrite()).willReturn(true);
    given(vreAuthorizationCrud.getAuthorization(anyString(), any(User.class))).willReturn(Optional.of(vreAuthorization));
    Set<Permission> permissions = permissionFetcher.getPermissions(mock(User.class), dataSetMetaData);
    assertThat(permissions, containsInAnyOrder(Permission.WRITE, Permission.READ));
}
Also used : User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) Permission(nl.knaw.huygens.timbuctoo.v5.security.dto.Permission) VreAuthorization(nl.knaw.huygens.timbuctoo.security.dto.VreAuthorization) Test(org.junit.Test)

Example 4 with VreAuthorization

use of nl.knaw.huygens.timbuctoo.security.dto.VreAuthorization in project timbuctoo by HuygensING.

the class VreAuthorizationTest method isAllowedToWriteReturnsFalseIfTheRolesOnlyContainUnverifiedUser.

@Test
public void isAllowedToWriteReturnsFalseIfTheRolesOnlyContainUnverifiedUser() throws Exception {
    VreAuthorization instance = VreAuthorization.create("", "", UNVERIFIED_USER_ROLE);
    boolean allowedToWrite = instance.isAllowedToWrite();
    assertThat(allowedToWrite, is(false));
}
Also used : VreAuthorization(nl.knaw.huygens.timbuctoo.security.dto.VreAuthorization) Test(org.junit.Test)

Example 5 with VreAuthorization

use of nl.knaw.huygens.timbuctoo.security.dto.VreAuthorization in project timbuctoo by HuygensING.

the class VreAuthorizationTest method hasAdminAccessReturnsTrueIfTheRolesContainUnverifiedUserAndAdmin.

@Test
public void hasAdminAccessReturnsTrueIfTheRolesContainUnverifiedUserAndAdmin() throws Exception {
    VreAuthorization instance = VreAuthorization.create("", "", UNVERIFIED_USER_ROLE, ADMIN_ROLE);
    boolean allowedToWrite = instance.hasAdminAccess();
    assertThat(allowedToWrite, is(true));
}
Also used : VreAuthorization(nl.knaw.huygens.timbuctoo.security.dto.VreAuthorization) Test(org.junit.Test)

Aggregations

VreAuthorization (nl.knaw.huygens.timbuctoo.security.dto.VreAuthorization)26 Test (org.junit.Test)22 File (java.io.File)5 Permission (nl.knaw.huygens.timbuctoo.v5.security.dto.Permission)5 User (nl.knaw.huygens.timbuctoo.v5.security.dto.User)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 IOException (java.io.IOException)2 BasicDataSetMetaData (nl.knaw.huygens.timbuctoo.v5.dataset.dto.BasicDataSetMetaData)2 DataSetMetaData (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSetMetaData)2 AuthorizationUnavailableException (nl.knaw.huygens.timbuctoo.v5.security.exceptions.AuthorizationUnavailableException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Before (org.junit.Before)1