diff --git a/source/development/index.rst b/source/development/index.rst index c07ce0fbb..f521ef369 100644 --- a/source/development/index.rst +++ b/source/development/index.rst @@ -28,6 +28,7 @@ As Chill rely on the `symfony `_ framework, reading the fram Timelines Exports Testing + Useful snippets manual/index.rst Layout and UI diff --git a/source/development/useful-snippets.rst b/source/development/useful-snippets.rst new file mode 100644 index 000000000..435790b0f --- /dev/null +++ b/source/development/useful-snippets.rst @@ -0,0 +1,35 @@ + + + + +Useful snippets +############### + + +Security +******** + +Get the circles a user can reach +================================ + +.. code-block:: php + + use Symfony\Component\Security\Core\Role\Role; + + $authorizationHelper = $this->get('chill.main.security.authorization.helper'); + $circles = $authorizationHelper + ->getReachableCircles( + $this->getUser(), # from a controller + new Role('CHILL_ROLE'), + $center + ); + + +Controller +********** + +Secured controller for person +============================= + +.. literalinclude:: useful-snippets/controller-secured-for-person.php + :language: php diff --git a/source/development/useful-snippets/controller-secured-for-person.php b/source/development/useful-snippets/controller-secured-for-person.php new file mode 100644 index 000000000..501fb46c1 --- /dev/null +++ b/source/development/useful-snippets/controller-secured-for-person.php @@ -0,0 +1,55 @@ +get('chill.person.repository.person') + ->find($id); + + if ($person === null) { + throw $this->createNotFoundException("The person is not found"); + } + + $this->denyAccessUnlessGranted(PersonVoter::SEE, $person); + + /* @var $authorizationHelper \Chill\MainBundle\Security\Authorization\AuthorizationHelper */ + $authorizationHelper = $this->get('chill.main.security.' + . 'authorization.helper'); + + $circles = $authorizationHelper->getReachableCircles( + $this->getUser(), + new Role(ConsultationVoter::SEE), + $person->getCenter() + ); + + // create a query which take circles into account + $consultations = $this->getDoctrine()->getManager() + ->createQuery('SELECT c FROM ChillHealthBundle:Consultation c ' + . 'WHERE c.patient = :person AND c.circle IN(:circles) ' + . 'ORDER BY c.date DESC') + ->setParameter('person', $person) + ->setParameter('circles', $circles) + ->getResult(); + + return $this->render('ChillHealthBundle:Consultation:list.html.twig', array( + 'person' => $person, + 'consultations' => $consultations + )); + } +} +