光 Hikari No kun
光 Hikari No kun

Reputation: 418

ADD PortletResource permission for specific users to VIEW custom portlet PROGRAMATICALLY

I want my custom portlet to be view-able by specific users but they're ALL MEMBERS OF SAME GROUP/SITE. i.e...

User 1: my custom Portlet is not view-able

Admin: my custom portlet is view-able

User 2: my custom portlet is view-able

How to ADD Portlet-Resource permission for particular user to VIEW custom portlet PROGRAMATICALLY in my Jsp???

Here's my code..

<%@page import="com.liferay.portal.security.permission.ActionKeys"%>
<%@page import="com.liferay.portal.security.permission.PermissionChecker"%>
<%@page import="com.liferay.portal.model.Permission"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ page import="com.liferay.portal.theme.ThemeDisplay" %>
<%@ page import="com.liferay.portal.kernel.util.WebKeys" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@page import="com.liferay.portal.service.RoleLocalServiceUtil"%>
<%@page import="com.liferay.portal.service.UserLocalServiceUtil"%>

<portlet:defineObjects />

<theme:defineObjects/>

<% ThemeDisplay themeDisplay = ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
PermissionChecker permissionChecker = themeDisplay.getPermissionChecker();
long companyId = themeDisplay.getCompanyId();
long roleId = RoleLocalServiceUtil.getRole(companyId, "Administrator").getRoleId();
long userId = themeDisplay.getUserId();
UserLocalServiceUtil.hasRoleUser(roleId, userId);

if(permissionChecker.hasPermission(roleId, "com.test.sokbu", 10162, ActionKeys.VIEW)){System.out.println("Ok!");}%>

Thank you in advance!

Best Regards,

Cjohn

Upvotes: 2

Views: 1373

Answers (1)

光 Hikari No kun
光 Hikari No kun

Reputation: 418

I just followed this link http://agile-reflections.opnworks.com/2011/07/experimenting-with-liferay-permissions.html and viola! it's now working! =) Thanks to Sir Laurent Gauthier

THIS IS MY SAMPLE.

STEP 1

  • Create a resource-actions folder inside docroot/WEB-INF/src

  • Create perm.xml

<?xml version='1.0' encoding='UTF-8'?>
<resource-action-mapping>
  <portlet-resource>
    <portlet-name>permissions</portlet-name>
    <permissions>
      <supports>
        <action-key>VIEW</action-key>
        <!-- <action-key>DELETE</action-key> -->
      </supports>
      <site-member-defaults>
        <!-- <action-key>VIEW</action-key> -->
        <!-- <action-key>DELETE</action-key> -->
      </site-member-defaults>
      <guest-defaults>
        <!-- <action-key>VIEW</action-key> -->
      </guest-defaults>
      <guest-unsupported>
        <action-key>VIEW</action-key>
      </guest-unsupported>
    </permissions>
  </portlet-resource>
</resource-action-mapping>
  • Create default.xml
<?xml version="1.0"?>
<resource-action-mapping>
  <resource file="resource-actions/perm.xml" />
</resource-action-mapping>

STEP 2
- Create a file named portlet.properties inside docroot/WEB-INF/src
- Insert this.. resource.actions.configs=resource-actions/default.xml

STEP 3
- Insert <add-default-resource>true</add-default-resource> after </css-class-wrapper> tag in liferay-portlet.xml.

STEP 4 Here I followed what's on the blog of Sir Laurent..

  • Open a browser on your Liferay instance as Liferay administrator
  • Create a new role named "Example Role"
  • Create a new user named "Example User" and set the user's password
  • Assign the "Example Role" to the newly created user
  • Open a different browser on your Liferay instance so as to have a different session, log in as "Example User" and navigate to the page containing this portlet

Pls Note Whenever I have changes in perm.xml, I restart my server for changes to take effect.

Upvotes: 1

Related Questions