diff --git a/DependencyInjection/ChillActivityExtension.php b/DependencyInjection/ChillActivityExtension.php index 262925889..72dc28583 100644 --- a/DependencyInjection/ChillActivityExtension.php +++ b/DependencyInjection/ChillActivityExtension.php @@ -27,6 +27,8 @@ use Symfony\Component\Config\FileLocator; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\DependencyInjection\Loader; use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; +use Chill\ActivityBundle\Security\Authorization\ActivityStatsVoter; +use Chill\ActivityBundle\Security\Authorization\ActivityVoter; /** * This is the class that loads and manages your bundle configuration @@ -72,8 +74,9 @@ class ChillActivityExtension extends Extension implements PrependExtensionInterf { $container->prependExtensionConfig('security', array( 'role_hierarchy' => array( - 'CHILL_ACTIVITY_UPDATE' => array('CHILL_ACTIVITY_SEE'), - 'CHILL_ACTIVITY_CREATE' => array('CHILL_ACTIVITY_SEE') + ActivityVoter::UPDATE => array(ActivityVoter::SEE), + ActivityVoter::CREATE => array(ActivityVoter::SEE), + ActivityVoter::SEE => array(ActivityStatsVoter::STATS) ) )); } diff --git a/Export/Aggregator/ReasonAggregator.php b/Export/Aggregator/ReasonAggregator.php index 7a97cad47..5698d4071 100644 --- a/Export/Aggregator/ReasonAggregator.php +++ b/Export/Aggregator/ReasonAggregator.php @@ -23,6 +23,7 @@ use Symfony\Component\Form\FormBuilderInterface; use Doctrine\ORM\QueryBuilder; use Chill\MainBundle\Export\AggregatorInterface; use Symfony\Component\Security\Core\Role\Role; +use Chill\ActivityBundle\Security\Authorization\ActivityVoter; /** * @@ -86,9 +87,28 @@ class ReasonAggregator implements AggregatorInterface return "Aggregate by activity reason"; } - public function requiredRole() + public function addRole() { - return new Role('dummy'); + return new Role(ActivityVoter::SEE); + } + + public function getLabels($key, array $values, $data) + { + return array_combine($values, $values); + + } + + public function getQueryKeys($data) + { + // add select element + if ($data['level'] === 'reason') { + return array('activity_reason_id'); + } elseif ($data['level'] === 'category') { + return array ('activity_category_id'); + } else { + throw new \RuntimeException('the data provided are not recognised'); + } + } } diff --git a/Export/Export/CountActivity.php b/Export/Export/CountActivity.php index abfd8f89f..11f7aebf5 100644 --- a/Export/Export/CountActivity.php +++ b/Export/Export/CountActivity.php @@ -20,9 +20,10 @@ namespace Chill\ActivityBundle\Export\Export; use Chill\MainBundle\Export\ExportInterface; -use Doctrine\ORM\Query\Expr\Join; use Doctrine\ORM\QueryBuilder; use Symfony\Component\Security\Core\Role\Role; +use Doctrine\ORM\Query; +use Chill\ActivityBundle\Security\Authorization\ActivityStatsVoter; /** * @@ -77,7 +78,34 @@ class CountActivity implements ExportInterface public function requiredRole() { - return new Role('dummy'); + return new Role(ActivityStatsVoter::STATS); + } + + public function getAllowedFormattersTypes() + { + return array(\Chill\MainBundle\Export\FormatterInterface::TYPE_TABULAR); + } + + public function getLabels($key, array $values, $data) + { + if ($key !== 'export_count_activity') { + throw new \LogicException("the key $key is not used by this export"); + } + + $labels = array_combine($values, $values); + $labels['_header'] = 'Number of activities'; + + return $labels; + } + + public function getQueryKeys($data) + { + return array('export_count_activity'); + } + + public function getResult(QueryBuilder $qb, $data) + { + return $qb->getQuery()->getResult(Query::HYDRATE_SCALAR); } } diff --git a/Export/Filter/ActivityReasonFilter.php b/Export/Filter/ActivityReasonFilter.php index 6b0107200..9f2059fbe 100644 --- a/Export/Filter/ActivityReasonFilter.php +++ b/Export/Filter/ActivityReasonFilter.php @@ -27,6 +27,7 @@ use Chill\ActivityBundle\Entity\ActivityReason; use Chill\MainBundle\Templating\TranslatableStringHelper; use Doctrine\ORM\Query\Expr; use Symfony\Component\Security\Core\Role\Role; +use Chill\ActivityBundle\Security\Authorization\ActivityVoter; /** * @@ -90,8 +91,8 @@ class ActivityReasonFilter implements FilterInterface return 'Filter by reason'; } - public function requiredRole() + public function addRole() { - return new Role('dummy'); + return new Role(ActivityVoter::SEE); } } diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 090ff55ac..37383ab2e 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -38,6 +38,14 @@ services: - { name: security.voter } - { name: chill.role } + chill.activity.security.authorization.activity_stats_voter: + class: Chill\ActivityBundle\Security\Authorization\ActivityStatsVoter + arguments: + - "@chill.main.security.authorization.helper" + tags: + - { name: security.voter } + - { name: chill.role } + chill.activity.timeline: class: Chill\ActivityBundle\Timeline\TimelineActivityProvider diff --git a/Security/Authorization/ActivityStatsVoter.php b/Security/Authorization/ActivityStatsVoter.php new file mode 100644 index 000000000..94eedf1fb --- /dev/null +++ b/Security/Authorization/ActivityStatsVoter.php @@ -0,0 +1,76 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\ActivityBundle\Security\Authorization; + +use Chill\MainBundle\Security\Authorization\AbstractChillVoter; +use Chill\MainBundle\Security\Authorization\AuthorizationHelper; +use Chill\MainBundle\Security\ProvideRoleInterface; +use Chill\ActivityBundle\Security\Authorization\ActivityVoter; + +/** + * + * + * @author Julien Fastré + */ +class ActivityStatsVoter extends AbstractChillVoter implements ProvideRoleInterface +{ + const STATS = 'CHILL_ACTIVITY_STATS'; + + /** + * + * @var AuthorizationHelper + */ + protected $helper; + + public function __construct(AuthorizationHelper $helper) + { + $this->helper = $helper; + } + + protected function getSupportedAttributes() + { + return array(self::STATS, ActivityVoter::SEE); + } + + protected function getSupportedClasses() + { + return array('Chill\MainBundle\Entity\Center'); + } + + protected function isGranted($attribute, $object, $user = null) + { + if (!$user instanceof \Symfony\Component\Security\Core\User\UserInterface) { + return false; + } + + return $this->helper->userHasAccess($user, $object, $attribute); + } + + public function getRoles() + { + $this->getSupportedAttributes(); + } + + public function getRolesWithoutScope() + { + $this->getSupportedAttributes(); + } + +}