In Liferay when a site page is added, it is assigned VIEW
permission automatically for Owner
role, Guest
role and Site Member
role.
Is it possible to assign VIEW
permissions dynamically to custom role when page is created instead of manually assigning VIEW
permission from Manage-permission tab of the site-page?
One of the possible way is Using LayoutListener
via hook
For this you need to create hook (portal properties) and override following property:
value.object.listener.com.liferay.portal.model.Layout
See the following example:
value.object.listener.com.liferay.portal.model.Layout=com.smb.mypermissions.hook.listeners.LayoutListener
Here LayoutListener
is the custom class created under package com.smb.mypermissions.hook.listeners
to override default LayoutListener
.
Signature for this class: public class LayoutListener extends BaseModelListener<Layout>
Now override the method
public void onAfterCreate(Layout model)throws ModelListenerException
to assign permission to the role, use following one liner:
ResourcePermissionLocalServiceUtil.setResourcePermissions(
companyId, Layout.class.getName(),
ResourceConstants.SCOPE_INDIVIDUAL,
String.valueOf(primKey), role.getRoleId(),
new String[] {
ActionKeys.VIEW
});
where role can be obtained from RoleLocalServiceUtil
and primkey
is the page unique id i.e plid
long primKey = model.getPlid();
long companyId = model.getGroup().getCompanyId();
Role role = RoleLocalServiceUtil.fetchRole(companyId, "<Your Role name here>");