Foren

Adding custom portlet resource to a role

Martin Patefield-Smith, geändert vor 12 Jahren.

Adding custom portlet resource to a role

New Member Beitrag: 1 Beitrittsdatum: 20.09.11 Neueste Beiträge
Hi,

I have developed a portlet to which I want to add my own portlet-level custom permissions. The portlet has a silverlight UI and I am checking the permissions in back-end java code rather than the jsp. I am currently testing that I can get permission handling to work and am encountering a problem. I just want to check that what I have done so far is correct and ask why it is not behaving as I expect.

Here is what I have done:

1. Defined a portlet.properties file which is in the WEB-INF/classes directory.


include-and-override=portlet-ext.properties

language.bundle=content.Language

resource.actions.configs=resource-actions/default.xml


2. Defined a default.xml file in WEB-INF/classes/resource-actions where CCBaseDataManagement is the portlet-name in portlet.xml

<!--?xml version="1.0"?-->

<resource-action-mapping>
	<portlet-resource>
		<portlet-name>CCBaseDataManagement</portlet-name>
		<permissions>
			<supports>
				<action-key>ADD_TO_PAGE</action-key>
				<action-key>CONFIGURATION</action-key>
				<action-key>VIEW</action-key>
				<action-key>MY_NEW_ACTION</action-key>
			</supports>
			<community-defaults />
			<guest-defaults />
			<guest-unsupported>
				<action-key>ADD_TO_PAGE</action-key>
				<action-key>CONFIGURATION</action-key>
				<action-key>VIEW</action-key>
				<action-key>MY_NEW_ACTION</action-key>
			</guest-unsupported>
		</permissions>
	</portlet-resource>
</resource-action-mapping>


3. Defined a Language.properties file in WEB-INF/classes/content

action.MY_NEW_ACTION=My New Action


4. In the back end code I am checking the portlet permission as follows, where portletId = "CCBaseDataManagement" and actionId="MY_NEW_ACTION". THis code is called when I press a button on the Silverlight UI.

public boolean hasPortletPermission(String portletId, String actionId) {
		logger.debug("hasPermission called");

		// Check if permission is granted
		ThemeDisplay themeDisplay = (ThemeDisplay) this.resourceRequest
				.getAttribute(WebKeys.THEME_DISPLAY);
		PermissionChecker permissionChecker = themeDisplay
				.getPermissionChecker();
		long plid = themeDisplay.getPlid();
		boolean hasPermission = false;
		try {
			if (logger.isDebugEnabled()) {
				logger.debug("hasPortletPermission called for user: "
						+ this.resourceRequest.getRemoteUser() + ", plid: "
						+ plid + ", portletId: " + portletId + ", actionId: "
						+ actionId);
			}
			hasPermission = PortletPermissionUtil.contains(permissionChecker,
					plid, portletId, actionId);
		} catch (PortalException e) {
			String error = "Portal exception thrown while checking portlet permission: "
					+ e.getMessage();
			logger.error(error);
			throw new RuntimeException(error, e);
		} catch (SystemException e) {
			String error = "System exception thrown while checking portlet permission: "
					+ e.getMessage();
			logger.error(error);
			throw new RuntimeException(error, e);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("Q: Does user: "
					+ this.resourceRequest.getRemoteUser()
					+ " have permission for plid: " + plid + ", portletId: "
					+ portletId + ", actionId: " + actionId + "? A: "
					+ hasPermission);
		}

		return hasPermission;
	}


5. Logged into Liferay as the default test user and seen that my portlet has a new action associated with it.

6. While logged in as the default test user, added a public page with my portlet on it, pressed the button and saw that hasPermission is set to true.

6. Created a new User Group containing a private page which has my custom portlet on it

7. Created a new User and added them to this user group. The new user has the Power User role as his only role.

8. Used the Define Permissions screen for the Power User role to add "My New Action" for my portlet to the Power User role.

9. Logged in as my new user, selected my private pages and pressed the button on the Silverlight UI. Expected to see hasPermission is set to true as my new user is a power user and the power user role has the permission to perform the custom action on my portlet. What I actually see is the hasPermission is false.

I've also noticed that if I add my new portlet onto a Public Page, rather than a Private Page and click the button on the Silverlight UI which results in the permissions check then an exception appears in the Tomcat console window:

ERROR [AdvancedPermissionChecker:651] com.liferay.portal.NoSuchResourceActionException: CCBaseDataManagement#MY_NEW_ACTION

If anyone could help me to figure out why I can't enable this new action for an existing role then I'd be a happy man. I've been looking at this all day. I'm sure I must be missing something obvious.

Thanks,

Martin