package com.cgi.eoss.ftep.api.controllers;

import com.cgi.eoss.ftep.api.projections.ShortFtepFile;
import com.cgi.eoss.ftep.model.FtepFile;
import com.cgi.eoss.ftep.model.FtepFileType;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.security.access.method.P;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;

import java.util.List;

@RepositoryRestResource(
        path = "ftepFiles",
        itemResourceRel = "ftepFile",
        collectionResourceRel = "ftepFiles",
        excerptProjection = ShortFtepFile.class)
public interface FtepFilesApi extends CrudRepository<FtepFile, Long> {

    @Override
    @RestResource(exported = false)
    <S extends FtepFile> Iterable<S> save(Iterable<S> ftepFiles);

    @Override
    @PreAuthorize("#ftepFile.id != null and (hasAnyRole('ROLE_CONTENT_AUTHORITY', 'ROLE_ADMIN') or hasPermission(#ftepFile, 'write'))")
    <S extends FtepFile> S save(@P("ftepFile") S ftepFile);

    @PostFilter("hasAnyRole('ROLE_CONTENT_AUTHORITY', 'ROLE_ADMIN') or hasPermission(filterObject, 'read')")
    List<FtepFile> findByType(@Param("type") FtepFileType type);

    @Override
    @PreAuthorize("hasAnyRole('ROLE_CONTENT_AUTHORITY', 'ROLE_ADMIN')")
    void delete(Iterable<? extends FtepFile> ftepFiles);

    @Override
    @PreAuthorize("hasAnyRole('ROLE_CONTENT_AUTHORITY', 'ROLE_ADMIN') or hasPermission(#ftepFile, 'administration')")
    void delete(@P("ftepFile") FtepFile ftepFile);

}

@@ -491,16 +491,82 @@
 thod.P;%0A
+import org.springframework.security.access.prepost.PostAuthorize;%0A
 import o
@@ -686,16 +686,37 @@
 orize;%0A%0A
+import java.net.URI;%0A
 import j
@@ -1487,24 +1487,187 @@
 ype type);%0A%0A
+    @PostAuthorize(%22hasAnyRole('ROLE_CONTENT_AUTHORITY', 'ROLE_ADMIN') or hasPermission(returnObject, 'read')%22)%0A    FtepFile findOneByUri(@Param(%22uri%22) URI uri);%0A%0A
     @Overrid

Allow FtepFiles to be retrieved by URI