Feature #89983
closed
Allow comma separated list of roles in f:security.ifHasRole
Added by Dieter Porth almost 5 years ago.
Updated 5 months ago.
Description
Instead of
protected static function evaluateCondition($arguments = null)
{
$role = $arguments['role'];
/** @var UserAspect $userAspect */
$userAspect = GeneralUtility::makeInstance(Context::class)->getAspect('frontend.user');
if (!$userAspect->isLoggedIn()) {
return false;
}
if (is_numeric($role)) {
$groupIds = $userAspect->getGroupIds();
return in_array((int)$role, $groupIds, true);
}
return in_array($role, $userAspect->getGroupNames(), true);
}
use
// Code ist not tested.
protected static function evaluateCondition($arguments = null)
{
$role = $arguments['role'];
/** @var UserAspect $userAspect */
$userAspect = GeneralUtility::makeInstance(Context::class)->getAspect('frontend.user');
if (!$userAspect->isLoggedIn()) {
return false;
}
if (is_numeric($role)) {
$groupIds = $userAspect->getGroupIds();
$roles = array_filter( array_map('intval',explode(',',$role)));
return ((array_sect(($roles, $groupIds) >0);
}
$roles = array_filter( array_map('trim',explode(',',$role)));
return (array_sect($roles, $userAspect->getGroupNames())>0);
}
- Project changed from 9 to TYPO3 Core
Better (Tested for list of Ints)
protected static function evaluateCondition($arguments = null)
{
$role = $arguments['role'];
/** @var UserAspect $userAspect */
$userAspect = GeneralUtility::makeInstance(Context::class)->getAspect('frontend.user');
if (!$userAspect->isLoggedIn()) {
return false;
}
if ((int)trim($role) >0) {
$groupIds = array_filter( array_map('intval',$userAspect->getGroupIds()));
$roles = array_filter( array_map('intval',explode(',',$role)));
return (count(array_intersect($roles, $groupIds)) >0);;
}
$roles = array_filter( array_map('trim',explode(',',$role)));
return (count(array_intersect($roles, $userAspect->getGroupNames()))>0);
}
- Status changed from New to Accepted
- Target version set to Candidate for Major Version
- Related to Feature #95175: security.ifByRoles-Viewhelper, which allows to check different groups and allowes mixed use of usergruop-IDs and -names added
- Status changed from Accepted to Rejected
hey!
thanks for sharing your ideas. just as with #95175 this looks like this is code which should be in your extension. there can be a lot of edge cases like "a user needs to be long to every group" or "a user needs to belong to at least 2 groups", .. which are all related to your extension and very hard to make every usecase possible with one viewhelper.
feel free to contact me on slack to discuss it further if you want to.
Also available in: Atom
PDF