Creation Resources Available for Guest Users


Creation Resources Available for Guest Users

Overview

If you implement a public web-site on Liferay, you need it’s content to be publicly available. By default, when you invoke the Liferay API to create some resource (document, web content, etc.) - it’s not created in such a way. But it’s easy to make it work. 

Implementation

When you invoke the Liferay API to create some resource you need - you’ll notice it accepts the ServiceContext object. 

To make the resource available for Guest user - use:  

serviceContext.setAddGuestPermissions(true);

To make the resource available for Site Members - use: 

serviceContext.setAddGroupPermissions(true);

This is a sample code for File Entry:


ServiceContext serviceContext = new ServiceContext();
serviceContext.setUserId(userId);
serviceContext.setScopeGroupId(groupId);
if (setViewPermission) {
  serviceContext.setAddGroupPermissions(true);
  serviceContext.setAddGuestPermissions(true);
}
FileEntry fileEntry = _dlAppLocalService.addFileEntry(
     userId,
     groupId,
     folderId,
     title,
     mimeType,
     preparedTitle,
     "",
     "",
     bytes,
     serviceContext
);

If you check the permissions for the created file - you’ll see, that VIEW permissions is available for Guest and Site Member users:



Enjoy 😏

Comments

Popular posts from this blog

Liferay Search Container Example

Liferay DXP - max upload file size

Liferay Keycloak integration