As mentioned on Liferay Dcoumentation we can add permissions to your custom portlets using four easy steps (also known as DRAC):
1. Define all resources and their permissions.
2. Register all defined resources in the permissions system. This is also known as adding resources.
3. Associate the necessary permissions with resources.
4. Check permission before returning resources.
Here we are implementing Liferay Permission for custom portlet named StudentMaster.
1. The first step is to define your resources and the actions that can be defined on them. Create a file named default.xml in /src/resource-actions folder -
2. After defining resource permissions for our custom portlet, we need to refer Liferay to the resource-actions XML file that contains definitions. Create a properties file named portlet.properties that references the the file default.xml. In this portlet properties file, create a property named resource.actions.configs with the relative path to portlet’s resource-action mapping file (e.g.default.xml) as its value. Here’s what this property specification might look like:
3. Adding a Resource
After defining resources and actions, it’s time to add resources into the permissions system. Resources are added at the same time entities are added to the database. Each Entity that requires access permission must be added as a resource every time it is stored.
4. Adding Permission:
On the portlet level, no code needs to be written in order to have the permission system work for custom portlet. If we have defined any custom permissions (supported actions) in configuration file’s portlet-resource tag, they’re automatically added to a list of permissions in Liferay’s permissions UI. What good, however, are permissions that are available but can’t be set by users?
To let a user set permissions on model resources, we must expose the permission interface to the user. Just add these two Liferay UI tags to JSP:
1. : Returns a URL to the permission settings configuration page.
2. : Shows an icon to the user. These are defined in the theme, and one of them (see below) is used for permissions.
5. Checking Permission:
1. Define all resources and their permissions.
2. Register all defined resources in the permissions system. This is also known as adding resources.
3. Associate the necessary permissions with resources.
4. Check permission before returning resources.
Here we are implementing Liferay Permission for custom portlet named StudentMaster.
1. The first step is to define your resources and the actions that can be defined on them. Create a file named default.xml in /src/resource-actions folder -
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<!DOCTYPE resource-action-mapping PUBLIC "-//Liferay//DTD Resource Action Mapping 6.1.0//EN" "http://www.liferay.com/dtd/liferay-resource-action-mapping_6_1_0.dtd"> | |
<resource-action-mapping> | |
<portlet-resource> | |
<portlet-name>StudentMaster</portlet-name> | |
<permissions> | |
<supports> | |
<action-key>ADD_STUDENT</action-key> | |
<action-key>CONFIGURATION</action-key> | |
<action-key>VIEW</action-key> | |
</supports> | |
<site-member-defaults> | |
<action-key>VIEW</action-key> | |
</site-member-defaults> | |
<guest-defaults> | |
<action-key>VIEW</action-key> | |
</guest-defaults> | |
<guest-unsupported> | |
<action-key>CONFIGURATION</action-key> | |
</guest-unsupported> | |
</permissions> | |
</portlet-resource> | |
<model-resource> | |
<model-name>com.harish.model.Student</model-name> | |
<portlet-ref> | |
<portlet-name>StudentMaster</portlet-name> | |
</portlet-ref> | |
<permissions> | |
<supports> | |
<action-key>EDIT_STUDENT</action-key> | |
</supports> | |
<site-member-defaults /> | |
<guest-defaults /> | |
<guest-unsupported> | |
<action-key>EDIT_STUDENT</action-key> | |
</guest-unsupported> | |
</permissions> | |
</model-resource> | |
</resource-action-mapping> |
2. After defining resource permissions for our custom portlet, we need to refer Liferay to the resource-actions XML file that contains definitions. Create a properties file named portlet.properties that references the the file default.xml. In this portlet properties file, create a property named resource.actions.configs with the relative path to portlet’s resource-action mapping file (e.g.default.xml) as its value. Here’s what this property specification might look like:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resource.actions.configs=resource-actions/default.xml |
3. Adding a Resource
After defining resources and actions, it’s time to add resources into the permissions system. Resources are added at the same time entities are added to the database. Each Entity that requires access permission must be added as a resource every time it is stored.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Student addNewStudent(String studentName, String course, long companyId, long groupId, long userId) throws SystemException | |
{ | |
long studentId = counterLocalService.increment(Student.class.getName()); | |
Student student = studentPersistence.create(studentId); | |
student.setStudentName(studentName); | |
student.setCourse(course); | |
studentPersistence.update(student, false); | |
try { | |
resourceLocalService.addResources(companyId, groupId, userId, Student.class.getName(), studentId, false, true, true); | |
} catch (PortalException e) { | |
e.printStackTrace(); | |
} | |
return student; | |
} |
On the portlet level, no code needs to be written in order to have the permission system work for custom portlet. If we have defined any custom permissions (supported actions) in configuration file’s portlet-resource tag, they’re automatically added to a list of permissions in Liferay’s permissions UI. What good, however, are permissions that are available but can’t be set by users?
To let a user set permissions on model resources, we must expose the permission interface to the user. Just add these two Liferay UI tags to JSP:
1. : Returns a URL to the permission settings configuration page.
2. : Shows an icon to the user. These are defined in the theme, and one of them (see below) is used for permissions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<liferay-security:permissionsURL | |
modelResource="<%= Student.class.getName() %>" | |
modelResourceDescription="<%= \"Student\" %>" | |
resourcePrimKey="1" | |
var="entryURL" | |
/> | |
<liferay-ui:icon image="permissions" url="<%= entryURL %>" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<portlet:defineObjects /> | |
<liferay-theme:defineObjects /> | |
<% | |
long groupId = scopeGroupId; | |
String name = portletDisplay.getRootPortletId(); | |
String primKey = portletDisplay.getResourcePK(); | |
String actionId = "ADD_STUDENT"; | |
%> | |
This is the <b>Student Master</b> portlet. | |
<portlet:actionURL var="addStudentUrl" name="addStudent" /> | |
<br/><br/> | |
<strong> | |
<%-- portlet level permission check --% | |
<c:if test="<%= permissionChecker.hasPermission(groupId, name, primKey, actionId) %>"> | |
<a href="<%=addStudentUrl.toString()%>">Add New Student</a> | |
</c:if> | |
</strong> | |
<br/><br/> | |
<liferay-ui:search-container delta="5" emptyResultsMessage="No Students were found!!"> | |
<liferay-ui:search-container-results results="<%=ListUtil.subList((List<Student>)request.getAttribute(\"students\"), searchContainer.getStart(), searchContainer.getEnd())%>" total="${students.size()}" /> | |
<liferay-ui:search-container-row className="com.harish.model.Student" keyProperty="studentId" modelVar="student"> | |
<liferay-ui:search-container-column-text name="name" value="${student.studentName}" /> | |
<liferay-ui:search-container-column-text name="subject" value="${student.course}" /> | |
<%-- model level permission check --% | |
<c:if test="<%= permissionChecker.hasPermission(groupId, Student.class.getName(), student.getStudentId(), \"EDIT_STUDENT\") %>"> | |
<liferay-ui:search-container-column-jsp path="/studentActions.jsp" align="right" /> | |
</c:if> | |
</liferay-ui:search-container-row> | |
<liferay-ui:search-iterator /> | |
</liferay-ui:search-container> |
No comments:
Post a Comment