Friday, March 23, 2018

Programmatically Change Permission of a File/Document in Lilferay


Sometimes we need to change the permission of a document to Guest in Liferay. For this we can change this by Groovy Script or programatically. Today we will change this programatically:-

So lets start this:-
You can simply use this code to change the permission to guest. In this first we check the folder permission and then file permission:-

public static void changeFileEntryToGuest(DLFileEntry file) throws SystemException {
final long companyId = Long.parseLong(PropsUtil.get(LifemartMobileAppConstants.LIFEMART_COMPANY_ID));
String dlFileFolderResourceName = DLFolder.class.getName();
String folderId = String.valueOf(file.getFolderId());
String[] actionIds = new String[] { ActionKeys.VIEW };
String dlFileEntryResourceName = DLFileEntry.class.getName();
String fileEntryId = String.valueOf(file.getFileEntryId());
LOG.debug("changeFileEntryToGuest companyId:" + companyId + " folderId: " + folderId + " fileEntryId:" + fileEntryId);
try {
Role guestRole = RoleLocalServiceUtil.getRole(companyId, RoleConstants.GUEST);
// Change Folder Permission to Guest View
if (!ResourcePermissionLocalServiceUtil.hasResourcePermission(companyId, dlFileFolderResourceName, ResourceConstants.SCOPE_INDIVIDUAL, folderId, guestRole.getRoleId(), ActionKeys.VIEW)) {
ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, dlFileFolderResourceName, ResourceConstants.SCOPE_INDIVIDUAL, folderId, guestRole.getRoleId(), actionIds);
}
// Change File Permission To Guest View
if (!ResourcePermissionLocalServiceUtil.hasResourcePermission(companyId, dlFileEntryResourceName, ResourceConstants.SCOPE_INDIVIDUAL, fileEntryId, guestRole.getRoleId(), ActionKeys.VIEW)) {
ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, dlFileEntryResourceName, ResourceConstants.SCOPE_INDIVIDUAL, fileEntryId, guestRole.getRoleId(), actionIds);
}
} catch (PortalException e) {
LOG.error(e.getMessage(), e);
}
}
view raw Demo.java hosted with ❤ by GitHub




No comments:

Post a Comment

Total Pageviews

1039181

Number Of Unique Visitor

Free counters!