Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [ZendFramework]Zend Plugin, zalogowany?
Forum PHP.pl > Forum > PHP > Frameworki
trylas
Witam,
mam pytanie odnośnie Plugin w Zend FM , a więc tak chcialbym sprawdzic czy uzytkownik jest zalogowany?
Moje pytanie jest takie jak najprosciej to zrobic. Tzn ja chcialem napisac plugin ktory bylby wywolywany w kazdym controllerze i przez to moglbym przekazac do widoku kto jest zalogowany i jakie opcje wyswietlic.Problem w tym ze plugin nie przekazuje $identity. Jak to rozwiazac?pozdrawiam

  1. class App_Plugin_Checkusr extends Zend_Controller_Plugin_Abstract {
  2.  
  3. public function preDispatch( Zend_Controller_Request_Abstract $request )
  4. {
  5. if(!Zend_Auth::getInstance()->hasIdentity())
  6. {
  7. $identity = 'guest';
  8. }
  9. else
  10. {
  11. $auth = Zend_Auth::getInstance();
  12. $identity = $auth->getIdentity()->role;
  13. }
  14. return $identity;
  15. }
  16. }class App_Plugin_Checkusr extends Zend_Controller_Plugin_Abstract {
  17.  
  18. public function preDispatch( Zend_Controller_Request_Abstract $request )
  19. {
  20. if(!Zend_Auth::getInstance()->hasIdentity())
  21. {
  22. $identity = 'guest';
  23. }
  24. else
  25. {
  26. $auth = Zend_Auth::getInstance();
  27. $identity = $auth->getIdentity()->role;
  28. }
  29. return $identity;
  30. }
  31. }
pgrzelka
w pluginie sprawdzasz czy user ma dostęp i ewentualnie robisz przekierowania lub wyświetlasz błędy

tworzysz obiekt Zend_Acl
dodajesz resources oraz roles, potem uprawnienia i na końcu sprawdzasz

poniżej masz przykład z ostatniego projektu
  1. public function routeShutdown(Zend_Controller_Request_Abstract $request)
  2. {
  3.  
  4. $module = $request->getModuleName();
  5. $controller = $request->getControllerName();
  6. $action = $request->getActionName();
  7.  
  8. $user_role = Zend_Registry::get('user_role');
  9.  
  10.  
  11. $acl = new Zend_Acl();
  12.  
  13. $acl -> addRole(new Zend_Acl_Role('quest'))
  14. -> addRole(new Zend_Acl_Role('member'), 'quest')
  15. -> addRole(new Zend_Acl_Role('admin'))
  16. ;
  17.  
  18. $acl -> addResource(new Zend_Acl_Resource('uczelnie'))
  19. -> addResource(new Zend_Acl_Resource('kariery'))
  20. -> addResource(new Zend_Acl_Resource('quiz'))
  21. -> addResource(new Zend_Acl_Resource('newsy'))
  22. -> addResource(new Zend_Acl_Resource('komentarze'))
  23. -> addResource(new Zend_Acl_Resource('reklama'))
  24. -> addResource(new Zend_Acl_Resource('admin'))
  25. -> addResource(new Zend_Acl_Resource('index'))
  26. -> addResource(new Zend_Acl_Resource('kontakt'))
  27. -> addResource(new Zend_Acl_Resource('content'))
  28. -> addResource(new Zend_Acl_Resource('szukaj'))
  29. ;
  30.  
  31. $acl -> allow('quest', 'index', array('index', 'zobacz'));
  32. $acl -> allow('quest', 'uczelnie', array('index', 'zobacz'));
  33. $acl -> allow('quest', 'kariery', array('index', 'zobacz'));
  34. $acl -> allow('quest', 'quiz', array('index'));
  35. $acl -> allow('quest', 'newsy', array('index', 'zobacz'));
  36. $acl -> allow('quest', 'komentarze', array('index'));
  37. $acl -> allow('quest', 'kontakt', array('index'));
  38. $acl -> allow('quest', 'szukaj', array('index'));
  39. $acl -> allow('quest', 'content', array('index', 'regulamin'));
  40.  
  41. $acl -> allow('member', 'uczelnie', array('dodaj', 'edytuj', 'moje', 'zdjecia', 'kariery'));
  42. $acl -> allow('member', 'kariery', array('dodaj', 'edytuj', 'moje'));
  43.  
  44. $acl -> deny('member', 'komentarze', array('dodaj'));
  45.  
  46. $acl -> allow('admin');
  47.  
  48. Zend_Registry::set('acl',$acl);
  49.  
  50. if ($acl->has($controller)) {
  51. if ( ! $acl->isAllowed($user_role, $controller, $action)) {
  52. throw new exception('', 403);
  53. }
  54. };
  55.  
  56. }
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.