TYPO3 API  SVNRelease
IfHasRoleViewHelper.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script belongs to the FLOW3 package "Fluid".                      *
00005  *                                                                        *
00006  * It is free software; you can redistribute it and/or modify it under    *
00007  * the terms of the GNU Lesser General Public License as published by the *
00008  * Free Software Foundation, either version 3 of the License, or (at your *
00009  * option) any later version.                                             *
00010  *                                                                        *
00011  * This script is distributed in the hope that it will be useful, but     *
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00013  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
00014  * General Public License for more details.                               *
00015  *                                                                        *
00016  * You should have received a copy of the GNU Lesser General Public       *
00017  * License along with the script.                                         *
00018  * If not, see http://www.gnu.org/licenses/lgpl.html                      *
00019  *                                                                        *
00020  * The TYPO3 project - inspiring people to share!                         *
00021  *                                                                        */
00022 
00023 /**
00024  * This view helper implements an ifHasRole/else condition for FE users/groups.
00025  *
00026  * = Examples =
00027  *
00028  * <code title="Basic usage">
00029  * <f:security.ifHasRole role="Administrator">
00030  *   This is being shown in case the current FE user belongs to a FE usergroup (aka role) titled "Administrator" (case sensitive)
00031  * </f:security.ifHasRole>
00032  * </code>
00033  * <output>
00034  * Everything inside the <f:ifHasRole> tag is being displayed if the logged in FE user belongs to the specified role.
00035  * </output>
00036  *
00037  * <code title="Using the usergroup uid as role identifier">
00038  * <f:security.ifHasRole role="1">
00039  *   This is being shown in case the current FE user belongs to a FE usergroup (aka role) with the uid "1"
00040  * </f:security.ifHasRole>
00041  * </code>
00042  * <output>
00043  * Everything inside the <f:ifHasRole> tag is being displayed if the logged in FE user belongs to the specified role.
00044  * </output>
00045  *
00046  * <code title="IfRole / then / else">
00047  * <f:security.ifHasRole role="Administrator">
00048  *   <f:then>
00049  *     This is being shown in case you have the role.
00050  *   </f:then>
00051  *   <f:else>
00052  *     This is being displayed in case you do not have the role.
00053  *   </f:else>
00054  * </f:security.ifHasRole>
00055  * </code>
00056  * <output>
00057  * Everything inside the "then" tag is displayed if the logged in FE user belongs to the specified role.
00058  * Otherwise, everything inside the "else"-tag is displayed.
00059  * </output>
00060  *
00061  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00062  * @api
00063  */
00064 class Tx_Fluid_ViewHelpers_Security_IfHasRoleViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractConditionViewHelper {
00065 
00066     /**
00067      * renders <f:then> child if the current logged in FE user belongs to the specified role (aka usergroup)
00068      * otherwise renders <f:else> child.
00069      *
00070      * @param string $role The usergroup (either the usergroup uid or its title)
00071      * @return string the rendered string
00072      * @api
00073      */
00074     public function render($role) {
00075         if ($this->frontendUserHasRole($role)) {
00076             return $this->renderThenChild();
00077         } else {
00078             return $this->renderElseChild();
00079         }
00080     }
00081 
00082     /**
00083      * Determines whether the currently logged in FE user belongs to the specified usergroup
00084      *
00085      * @param string $role The usergroup (either the usergroup uid or its title)
00086      * @return boolean TRUE if the currently logged in FE user belongs to $role
00087      */
00088     protected function frontendUserHasRole($role) {
00089         if (!isset($GLOBALS['TSFE']) || !$GLOBALS['TSFE']->loginUser) {
00090             return FALSE;
00091         }
00092         if (is_numeric($role)) {
00093             return (is_array($GLOBALS['TSFE']->fe_user->groupData['uid']) && in_array($role, $GLOBALS['TSFE']->fe_user->groupData['uid']));
00094         } else {
00095             return (is_array($GLOBALS['TSFE']->fe_user->groupData['title']) && in_array($role, $GLOBALS['TSFE']->fe_user->groupData['title']));
00096         }
00097     }
00098 }
00099 ?>