Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Ustawienie "akcji" dla całego routera.
Forum PHP.pl > Forum > PHP
Whisller
Witam,
mam pewien problem z systemem logowania użytkowników.
A mianowicie Panel administratora mam zrobiony na zasadzie. Dodałem nowy router $router->addRoute('admin', 'admin/:controller/:action'); i teraz do kontrolerów panelu administratora odwołuje się poprzez example.com/admin/ogłoszenia/dodaj.
I czy jest możliwość ustawienia akcji która będzie wywoływana po każdym odwołaniu się do routera admin ? Chodzi mi o sprawdzanie czy użytkownik jest zalogowany.
Czy może znacie jakiś inny wydajniejszy sposób na sprawdzanie autoryzacji? Bo nie widzi mi się sprawdzanie w każdej akcji kontrolera czy użytkownik ma do niej uprawnienia :/

Pozdrawiam.
envp
Bardzo dobrym rozwiązaniem jest zastosowanie Pluginow (takich jak w zendzie)
robisz sobie we FrontControllerze:

  1. <?php
  2. ...
  3. public function execute()
  4. {
  5.  
  6. $this->_oDispatcher = new Dispatcher();
  7. $this->_oActionCollectioner = new ActionCollectioner;
  8. $this->_oRouter = new Router($this->_oDispatcher, $this->_oActionCollectioner);
  9.  
  10. $aError404Action = $this->getAppConfig()->get('Errors','Error404');
  11.  
  12.  
  13. $this->_oPluginManager->routeStartup(); //filtr przed startem routera
  14.  
  15. $oActionToken = $this->_oRouter->route();
  16.  
  17. $oActionToken = $this->_oPluginManager->routeShutdown($oActionToken); //filtr po starcie
  18.  
  19. $this->_oActionCollectioner->pushAct($oActionToken);
  20.  
  21. foreach ($this->_oActionCollectioner as $iKey => $oActionToken)
  22. {
  23. $oActionToken = $this->_oPluginManager->preDispatch($oActionToken);
  24.  
  25. if(Dispatcher::isDispatchable($oActionToken))
  26. {
  27. NyssSystem::setLibraryPath($oActionToken->getControllerName(),NyssSystem::formatControllerPath($oActionToken->getControllerName()));
  28.  
  29. $sController = $oActionToken->getControllerName();
  30.  
  31. $oCurrentController = new $sController;
  32.  
  33. if(!method_exists($oCurrentController, $oActionToken->getActionName()))
  34. $oActionToken->setActionName(_DEFAULT_ACT_NAME);
  35.  
  36. unset($oCurrentController);
  37. }
  38.  
  39. else
  40. $oActionToken = new DispatcherToken($aError404Action['Controller'], $aError404Action['Action'], $this->_oRouter->getActionParams());
  41.  
  42.  
  43. if (!Dispatcher::isDispatchable($oActionToken)) {
  44. throw new FrontControllerException('Request could not be mapped to a route and Error404 Controller has not been crea
    ted.'
    );
  45. }
  46.  
  47. $oActionToken = $this->_oPluginManager->preExecution($oActionToken); //przed execute I TO WAZNE
  48.  
  49. $this->_CurrentAction = $oActionToken->getActionName();
  50.  
  51.  
  52. $this->_oDispatcher->executeAction($oActionToken);
  53.  
  54. }
  55.  
  56.  
  57. $this->_oPluginManager->dispatchLoopShutdown(); //Filtr po zakonczeniu petli, ktora sprawdza akcje
  58.  
  59.  
  60. }
  61. ...
  62. ?>


Dalej resjestrujemy sobie nowy plugin - w indeksie
  1. <?php
  2. ...
  3. $oFrontController->registerPlugin(new AclPlugin);
  4. ...
  5. ?>


No i sam plugin wyglada nastepujaco:

  1. <?php
  2. class AclPlugin extends PluginLayer implements IPlugin
  3. {
  4. public function preExecution(IDispatcherToken $oToken)
  5. {
  6.  
  7. $oACL = new ACL;
  8.  
  9. $sController = $oToken->getControllerName();
  10. $sAction = $oToken->getActionName();
  11.  
  12.  
  13.  
  14. NyssSystem::setLibraryPath($sController, NyssSystem::formatControllerPath($oToken->getControllerName()));
  15.  
  16.  
  17. $sController = ucfirst($sController);
  18.  
  19. $oController = new $sController;
  20.  
  21.  
  22. $oConfig = $oController->getConfig();
  23.  
  24. unset($oController);
  25. /**
  26.  * If security section in configuration has not been defined,
  27.  * leave the plugin.
  28.  */
  29.  
  30. if(!$oConfig->has('Actions', $sAction, 'Security'))
  31. return $oToken;
  32. /**
  33.  * Add allow groups to the current container.
  34.  */
  35.  
  36. if($oConfig->has('Actions', $sAction, 'Security', 'Groups', 'Allow'))
  37. {
  38. $aAllow = $oConfig->get('Actions', $sAction, 'Security', 'Groups', 'Allow');
  39.  
  40. if(!is_array($aAllow))
  41. {
  42. $sError = sprintf('Allow groups for action %s in controller %s must be an array', $sAction, $sController);
  43.  
  44. throw new ACLPluginException($sError);
  45. }
  46.  
  47. $oAccess = new Access;
  48.  
  49. foreach($aAllow as $sGroup)
  50. $oAccess->AddAllowGroup($sGroup);
  51.  
  52. $oACL->add($oAccess, $sController, $sAction);
  53. }
  54.  
  55. /**
  56.  * Add deny groups to the current container.
  57.  */
  58.  
  59. if($oConfig->has('Actions', $sAction, 'Security', 'Groups', 'Deny'))
  60. {
  61. $aDeny = $oConfig->get('Actions', $sAction, 'Security', 'Groups', 'Deny');
  62.  
  63. if(!is_array($aDeny))
  64. {
  65. $sError = sprintf('Deny groups for action %s in controller %s must be an array', $sAction, $sController);
  66.  
  67. throw new ACLPluginException($sError);
  68. }
  69.  
  70. $oDeny = new Access;
  71.  
  72. foreach($aDeny as $sGroup)
  73. $oDeny->deny($sGroup);
  74.  
  75. $oACL->add($oDeny, $sController, $sAction);
  76. }
  77.  
  78. /**
  79.  * Make the access control test and forward to the another action,
  80.  * if user has not got enough privilages.
  81.  */
  82. $oConfig = FrontController::getInstance()->getAppConfig();
  83.  
  84. $Error403Controller = $oConfig->get('Error403','Controller') ? $oConfig->get('Error403','Controller') : 'Errors';
  85. $Error403Action = $oConfig->get('Error403','Action') ? $oConfig->get('Error403','Action') : 'Error403';
  86.  
  87.  
  88. if(!$oACL->access(new User, $sController, $sAction))
  89. {
  90. $oToken = new DispatcherToken($Error403Controller,$Error403Action);
  91. }
  92.  
  93. return $oToken;
  94. }
  95. }
  96. ?>


Czyli to powyzej wczytuje konfig sprawdza jaka grupe musi miec user, zeby dostac sie do danej akcji, a nastepnie jesli nie ma dostepu wstawia tokena Error403 i zwraca do petli we FrontControllerze.
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.