From: <fg...@us...> - 2012-05-06 15:11:10
|
Revision: 3975 http://openutils.svn.sourceforge.net/openutils/?rev=3975&view=rev Author: fgiust Date: 2012-05-06 15:11:04 +0000 (Sun, 06 May 2012) Log Message: ----------- MGNLUTILS-34 Add methods from info.magnolia.cms.taglibs.CmsFunctions Modified Paths: -------------- magnoliamodules/branches/magnolia44/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/el/MgnlUtilsElFunctions.java magnoliamodules/branches/magnolia44/openutils-mgnlutils/src/main/resources/META-INF/tld/mgnlutils.tld Modified: magnoliamodules/branches/magnolia44/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/el/MgnlUtilsElFunctions.java =================================================================== --- magnoliamodules/branches/magnolia44/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/el/MgnlUtilsElFunctions.java 2012-05-06 14:42:42 UTC (rev 3974) +++ magnoliamodules/branches/magnolia44/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/el/MgnlUtilsElFunctions.java 2012-05-06 15:11:04 UTC (rev 3975) @@ -21,14 +21,18 @@ import info.magnolia.cms.beans.config.ContentRepository; import info.magnolia.cms.beans.config.ServerConfiguration; +import info.magnolia.cms.core.AggregationState; import info.magnolia.cms.core.Content; import info.magnolia.cms.core.HierarchyManager; import info.magnolia.cms.core.ItemType; +import info.magnolia.cms.core.NodeData; import info.magnolia.cms.core.Path; import info.magnolia.cms.core.SystemProperty; import info.magnolia.cms.i18n.I18nContentSupportFactory; import info.magnolia.cms.i18n.I18nContentWrapper; import info.magnolia.cms.i18n.MessagesManager; +import info.magnolia.cms.security.Permission; +import info.magnolia.cms.security.SecurityUtil; import info.magnolia.cms.security.User; import info.magnolia.cms.security.auth.Entity; import info.magnolia.cms.util.NodeMapWrapper; @@ -49,6 +53,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Properties; import java.util.Set; import javax.jcr.ItemNotFoundException; @@ -1005,4 +1010,169 @@ .getFirstResult(); } + /** + * Check if the current user belongs the given role + * @param role role name + * @return true if the user belongs to the role + */ + public static boolean userHasRole(String role) + { + return MgnlContext.getUser().getAllRoles().contains(role); + } + + /** + * Check if the current user belongs the given group + * @param group group name + * @return true if the user belongs to the group + */ + public static boolean userHasGroup(String group) + { + return MgnlContext.getUser().getAllGroups().contains(group); + } + + /** + * Returns the current active page (can be set using the loadPage tag). + * @return current page + */ + public static Content currentPage() + { + return MgnlContext.getAggregationState().getCurrentContent(); + } + + /** + * Returns the main loaded page (doesn't change when using the loadPage tag). + * @return loaded page + */ + public static Content mainPage() + { + return MgnlContext.getAggregationState().getMainContent(); + } + + /** + * Returns the current paragraph. + * @return current paragraph + */ + public static Content currentParagraph() + { + return MgnlContext.getAggregationState().getCurrentContent(); + } + + /** + * Returns the value of a system property. + * @param key property key + * @return property value + */ + public static String systemProperty(String key) + { + return SystemProperty.getProperty(key); + } + + /** + * Returns the system properties. + * @return Property instance + */ + public static Properties systemProperties() + { + return SystemProperty.getProperties(); + } + + /** + * Check if a user is currently logged in (not anonymous). + * @return true if a user is currently logged in. + */ + public static boolean isLoggedIn() + { + return SecurityUtil.isAuthenticated(); + } + + /** + * Check if the current user can edit the active page. + * @return true if the current user can edit the active page. + */ + public static boolean canEdit() + { + return MgnlContext.getAggregationState().getMainContent().isGranted(Permission.SET); + } + + /** + * Check if the current page is open in editing mode. Shortcut for checking if the server is admin, preview unset, + * permissions to modify the page available for the current user. + * @return true if the page is open in edit mode and user has permissions to edit + */ + public static boolean isEditMode() + { + final AggregationState aggregationState = MgnlContext.getAggregationState(); + Content activePage = aggregationState.getMainContent(); + return ServerConfiguration.getInstance().isAdmin() + && !aggregationState.isPreviewMode() + && activePage != null + && activePage.isGranted(Permission.SET); + } + + /** + * Find and load the first parent page containing a named node. This function can be useful while building pages + * that should inherit a paragraph from parent pages. Loaded page must be unloaded using the + * <code><cms:unloadPage /></code> tag. Sample use: + * + * <pre> + * <c:if test="${cmsfn:firstPageWithNode("column", 3)}"> + * content inherited from page ${cmsfn:currentPage().handle}.html + * <cms:contentNodeIterator contentNodeCollectionName="column"> + * <cms:includeTemplate /> + * </cms:contentNodeIterator> + * <cms:unloadPage /> + * </c:if> + * </pre> + * @param nodeName paragraph name + * @param minlevel level at which we will stop also if no page is found + * @return <code>true</code> if a page has been found and loaded, <code>false</code> otherwise + */ + public static boolean firstPageWithNode(String nodeName, int minlevel) + { + Content actpage = MgnlContext.getAggregationState().getCurrentContent(); + if (actpage == null) + { + actpage = MgnlContext.getAggregationState().getMainContent(); + } + + try + { + while (actpage.getLevel() > minlevel) + { + actpage = actpage.getParent(); + + if (actpage.hasContent(nodeName)) + { + MgnlContext.getAggregationState().setCurrentContent(actpage); + return true; + } + } + } + catch (RepositoryException e) + { + log.error("Error looking for node " + nodeName + " in " + actpage.getHandle(), e); + } + + return false; + } + + /** + * Function to iterate over a node Data that has "checkbox" as control type, for example. See + * http://jira.magnolia-cms.com/browse/MAGNOLIA-1969 + */ + public static Collection<NodeData> nodeDataIterator(Content c, String collection) + { + try + { + return c.getContent(collection).getNodeDataCollection(); + } + catch (RepositoryException e) + { + log.error( + "Error when getting nodedata collection from " + c + " / " + collection + " :" + e.getMessage(), + e); + return null; + } + } + } Modified: magnoliamodules/branches/magnolia44/openutils-mgnlutils/src/main/resources/META-INF/tld/mgnlutils.tld =================================================================== --- magnoliamodules/branches/magnolia44/openutils-mgnlutils/src/main/resources/META-INF/tld/mgnlutils.tld 2012-05-06 14:42:42 UTC (rev 3974) +++ magnoliamodules/branches/magnolia44/openutils-mgnlutils/src/main/resources/META-INF/tld/mgnlutils.tld 2012-05-06 15:11:04 UTC (rev 3975) @@ -1,5 +1,4 @@ -<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" +<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description>Magnolia utility Tags and EL functions</description> <display-name>Magnolia utility Tags and EL functions</display-name> @@ -327,22 +326,115 @@ <function-signature>java.util.Map userProperties()</function-signature> </function> <function> + <description>Returns the value of a page property, by iterating on active page ancestor till a value is found</description> <name>pagePropertyInherited</name> - <description>Returns the value of a page property, by iterating on active page ancestor till a value is found</description> <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> <function-signature>java.lang.String pagePropertyInherited( java.lang.String)</function-signature> </function> <function> + <description>Sets the given content as the active page, calling MgnlContext.getAggregationState().setCurrentContent)</description> <name>setActivePage</name> - <description>Sets the given content as the active page, calling MgnlContext.getAggregationState().setCurrentContent)</description> <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> <function-signature>void setActivePage(info.magnolia.cms.core.Content)</function-signature> </function> <function> + <description>Get a Content node by its UUID. Internally uses JCR Criteria. Params are UUID and repository name.</description> <name>contentByUUID</name> - <description>Get a Content node by its UUID. Internally uses JCR Criteria. Params are UUID and repository name.</description> <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> <function-signature>info.magnolia.cms.core.Content contentByUUID(java.lang.String, java.lang.String)</function-signature> </function> + <function> + <description>Check if the current user belongs the given role</description> + <name>userHasRole</name> + <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> + <function-signature>boolean userHasRole(java.lang.String)</function-signature> + </function> + <function> + <description>Check if the current user belongs the given group</description> + <name>userHasGroup</name> + <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> + <function-signature>boolean userHasGroup(java.lang.String)</function-signature> + </function> + <function> + <description>Returns the current active page (can be set using the loadPage tag).</description> + <name>currentPage</name> + <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> + <function-signature>info.magnolia.cms.core.Content currentPage()</function-signature> + </function> + <function> + <description>Returns the main loaded page (doesn't change when using the loadPage tag).</description> + <name>mainPage</name> + <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> + <function-signature>info.magnolia.cms.core.Content mainPage()</function-signature> + </function> + <function> + <description>Returns the current paragraph.</description> + <name>currentParagraph</name> + <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> + <function-signature>info.magnolia.cms.core.Content currentParagraph()</function-signature> + </function> + <function> + <description>Returns the value of a system property</description> + <name>systemProperty</name> + <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> + <function-signature>java.lang.String systemProperty(java.lang.String)</function-signature> + </function> + <function> + <description>Returns the system properties</description> + <name>systemProperties</name> + <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> + <function-signature>java.util.Properties systemProperties()</function-signature> + </function> + <function> + <description>Check if a user is currently logged in (not anonymous)</description> + <name>isLoggedIn</name> + <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> + <function-signature>boolean isLoggedIn()</function-signature> + </function> + <function> + <description>Check if the current user can edit the active page</description> + <name>canEdit</name> + <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> + <function-signature>boolean canEdit()</function-signature> + </function> + <function> + <description> + Check if the current page is open in editing mode. Shortcut for checking if the server is admin, preview unset, + permissions to modify the page available for the current user. + </description> + <name>editMode</name> + <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> + <function-signature>boolean isEditMode()</function-signature> + </function> + <function> + <description> + <![CDATA[ + Find and load the first parent page containing a named node. + This function can be useful while building pages that should inherit a paragraph from parent pages. + Loaded page must be unloaded using the <cms:unloadPage /> tag. + Sample use: + <pre> + <c:if test="${cmsfn:firstPageWithNode("node", 3)}"> + content inherited from page ${cmsfn:currentPage().handle}.html + <<cms:includeTemplate contentNodeName="node" /> + <cms:unloadPage /> + </c:if> + </pre> + ]]> + </description> + <name>firstPageWithNode</name> + <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> + <function-signature>boolean firstPageWithNode(java.lang.String,int)</function-signature> + </function> + <function> + <description> + Function to iterate over a node Data that has "checkbox" as control type, for example. + See http://jira.magnolia-cms.com/browse/MAGNOLIA-1969 + </description> + <name>nodeDataIterator</name> + <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> + <function-signature>java.util.Collection nodeDataIterator(info.magnolia.cms.core.Content, java.lang.String)</function-signature> + </function> + <!-- please don't add new funtions without a meaningfull <description> --> </taglib> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |