Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Symfony]Entity jako service?
mundeck
post
Post #1





Grupa: Zarejestrowani
Postów: 43
Pomógł: 0
Dołączył: 2.09.2012

Ostrzeżenie: (0%)
-----


Czy istnieje jakieś przeciwskazanie, aby klasę User rejestrować jako service? Chodzi o to, że ustawiam w niej role użytkownika na podstawie różnych parametrów i niewielkich algroytmów. Dotychczas były to parametry zapisywane w bazie, w tabeli User, natomiast teraz chcę nadać rolę na podstawie parametru zapisywanego w configu, a żeby się do niego dostać, potrzebowałbym wstrzyknąć - sam jeszcze nie wiem co, ale choćby np. cały container. Ale czy to jest zalecane rozwiązanie?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
kpt_lucek
post
Post #2





Grupa: Zarejestrowani
Postów: 428
Pomógł: 77
Dołączył: 10.07.2011
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


Ewentualnie zrobić to na Voter'ach, definiować swoje uprawnienia i w nich je sprawdzać dokładnie tak, jak ma to miejsce w przypadku roli.

  1. //controller
  2. $this->isGranted('ROLE_ADMIN');


A tak jakiś custom:

  1. //controller
  2. $this->isGranted(MojVoter::MOJE_SUPER_UPRAWNIENIE, $this->getUser());


I wtedy Voter wygląda tak:

  1.  
  2. <?php
  3.  
  4. namespace MojBundle\Security\Voter;
  5.  
  6. use Doctrine\ORM\EntityManager;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9.  
  10. /**
  11.  * Class MojVoter
  12.  */
  13. class MojVoter extends Voter
  14. {
  15. const MOJE_SUPER_UPRAWNIENIE = 'MOJE_SUPER_UPRAWNIENIE';
  16.  
  17. /**
  18. * Determines if the attribute and subject are supported by this voter.
  19. *
  20. * @param string $attribute An attribute
  21. * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
  22. *
  23. * @return bool True if the attribute and subject are supported, false otherwise
  24. */
  25. protected function supports($attribute, $subject)
  26. {
  27. if (self::MOJE_SUPER_UPRAWNIENIE !== $attribute) {
  28. return false;
  29. }
  30.  
  31. return $subject instanceof UserInterface; // Lub co tam sobie chcesz
  32. }
  33.  
  34. /**
  35. * Perform a single access check operation on a given attribute, subject and token.
  36. * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  37. *
  38. * @param string $attribute
  39. * @param mixed $subject
  40. * @param TokenInterface $token
  41. *
  42. * @return bool
  43. */
  44. protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
  45. {
  46. //Twoja logika która określa czy masz uprawnienie, zwracasz (boolean)
  47. }
  48. }


Ten post edytował kpt_lucek 12.12.2016, 14:27:12
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 11.10.2025 - 13:10