Fórum

Liferay7: WebContent Freemarker template check DLFileEntryPermissions

Thomas Kellerer, modificado 6 Anos atrás.

Liferay7: WebContent Freemarker template check DLFileEntryPermissions

Expert Postagens: 490 Data de Entrada: 09/06/08 Postagens Recentes
We have a WebContent structure that shows download links to documents (the structure has a repeatable field of type "document-library")

We have a Freemarker template that displays these structures. Inside the template we would like to validate that the current user really has access to the linked documents.

I already have a workaround in place to retrieve the actual file name from the download item which involves parsing the URL from the download entry. I can successfully obtain the underlying DLFileEntry using the DLFileEntryService.

However I can't find a reliable way to check if the current user is allowed to view/download that file.

Currently I use this code to obtain the DLFileEntry object:
<#assign data = cur_link.getData()
         counter = 0 />

<#list data?split("/") as x>
  <#if counter == 2>
    <#assign groupId = x?number >
  <!--#if-->
  &lt;#if counter == 4&gt;
    &lt;#assign fname = x?replace("+", " ") /&gt;
  <!--#if-->
  &lt;#if counter == 5&gt;
    &lt;#-- Liferay sometimes appends a ?t=123456789 to the URL, so we need to take care of that as well --&gt;
    &lt;#assign uuId = x?keep_before("?") &gt;
  <!--#if-->
  &lt;#assign counter = counter+1 &gt;
<!--#list-->

&lt;#if groupId?? &amp;&amp; uuId??&gt;
  &lt;#assign file = dlService.getFileEntryByUuidAndGroupId(uuId,groupId) /&gt;
  &lt;#assign mayView = permissionChecker.hasPermission(file.getGroupId(), "com.liferay.document.library.kernel.model.DLFileEntry", file.getPrimaryKey(), "VIEW")/&gt;
<!--#if-->


However the mayView variable is always true, even if the user does not have the permission to do so (when clicking on the link an error is displayed - which is correct).

I also tried "DOWNLOAD" instead of "VIEW" when calling the permission checker, but that resulted in always returning FALSE.

What am I missing? How can I validate that the current user is allowed to view/download a document?
Thomas Kellerer, modificado 6 Anos atrás.

RE: Liferay7: WebContent Freemarker template check DLFileEntryPermissions

Expert Postagens: 490 Data de Entrada: 09/06/08 Postagens Recentes
After poking around in the Liferay Source I stumbled over DLFileEntryPermission which looked promising:

&lt;#assign fileEntryPermission = serviceLocator.findService('com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission') /&gt;
and then once I got the DLFileEntry instance, I tried:
&lt;#assign mayView = fileEntryPermission.contains(permissionChecker, file.getFileEntryId(), "VIEW")/&gt;
But that again always returns TRUE, even if the user has no access to the document.