Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.83 KB | None | 0 0
  1. /*
  2. Getting these errors when trying to log in. I'm using zend_auth with a doctrine adapter.
  3.  
  4. Strict Standards: Declaration of HR_Controller_Plugin_Auth::preDispatch() should be compatible with that of Zend_Controller_Plugin_Abstract::preDispatch() in /media/data/dev/php/mjfreg/library/HR/Controller/Plugin/Auth.php on line 61
  5.  
  6. Fatal error: Cannot redeclare class Registration_Model_Acl in /media/data/dev/php/mjfreg/application/models/Acl.php on line 38
  7.  
  8.  
  9. When I remove the Auth test and set the role to admin or guest the acl works fine.
  10. */
  11.  
  12.  
  13.  
  14. Class HR_Controller_Plugin_Auth extends Zend_Controller_Plugin_Abstract
  15. {
  16.     private $_auth;
  17.     private $_acl;
  18.  
  19.     private $_noauth = array('module' => 'default',
  20.                         'controller' => 'login',
  21.                         'action' => 'index');
  22.  
  23.     private $_noacl = array('module' => 'default',
  24.                         'controller' => 'error',
  25.                         'action' => 'privileges');
  26.  
  27.     public function __construct($auth, $acl)
  28.     {
  29.         $this->_auth = $auth;
  30.         $this->_acl = $acl;
  31.     }
  32.  
  33.     public function preDispatch($request)
  34.     {
  35.         if ($this->_auth->hasIdentity()) {
  36.             $role = $this->_auth->getIdentity()->role;
  37.         }
  38.         else {
  39.             $role = 'guest';
  40.         }
  41.  
  42.         $controller = $request->controller;
  43.         $action = $request->action;
  44.         $module = $request->module;
  45.         $resource = $controller;
  46.  
  47.         if (!$this->_acl->has($resource)) {
  48.             $resource = null;
  49.         }
  50.  
  51.         if (!$this->_acl->isAllowed($role, $resource, $action)) {
  52.             if (!$this->_auth->hasIdentity()) {
  53.                 $module = $this->_noauth['module'];
  54.                 $controller = $this->_noauth['controller'];
  55.                 $action = $this->_noauth['action'];
  56.             } else {
  57.                 $module = $this->_noacl['module'];
  58.                 $controller = $this->_noacl['controller'];
  59.                 $action = $this->_noacl['action'];
  60.             }  
  61.         }
  62.  
  63.         $request->setModuleName($module);
  64.         $request->setControllerName($controller);
  65.         $request->setActionName($action);
  66.     }
  67. }
  68.  
  69. class Registration_Model_Acl extends Zend_Acl
  70. {
  71.  
  72.     public function  __construct()
  73.     {
  74.        
  75.         $this->addRole(new Zend_Acl_Role('admin'));
  76.         $this->addRole(new Zend_Acl_Role('guest'));
  77.  
  78.         $this->add(new Zend_Acl_Resource('form'));
  79.         $this->add(new Zend_Acl_Resource('user'));
  80.         $this->add(new Zend_Acl_Resource('field'));
  81.         $this->add(new Zend_Acl_Resource('field-extra'));
  82.         $this->add(new Zend_Acl_Resource('field-reply'));
  83.         $this->add(new Zend_Acl_Resource('form-type'));
  84.         $this->add(new Zend_Acl_Resource('admin'));
  85.         $this->add(new Zend_Acl_Resource('reply'));
  86.         $this->add(new Zend_Acl_Resource('index'));
  87.  
  88.         $this->allow('guest', 'index');
  89.         $this->allow('guest', 'form', 'fill');
  90.         $this->allow('guest', 'form', 'thanks');
  91.         $this->allow("admin","admin");
  92.  
  93.     }
  94.    
  95. }
  96.  
  97. // in bootstrap.php
  98. protected function _initAclPlugin()
  99.     {
  100.         $front = Zend_Controller_Front::getInstance();
  101.         $auth = Zend_Auth::getInstance();
  102.         $acl = new Registration_Model_Acl();
  103.         $front->registerPlugin(new HR_Controller_Plugin_Auth($auth, $acl));
  104.     }
  105.  
  106.  
  107. class AuthenticationService
  108. {
  109.     private $_authenticationMessage = '';
  110.  
  111.     public function getAuthenticationMessage()
  112.     {
  113.         return $this->_authenticationMessage;
  114.     }
  115.  
  116.     public function isAuthenticated()
  117.     {
  118.         return Zend_Auth::getInstance()->hasIdentity();
  119.     }
  120.  
  121.     public function authenticate($username, $password)
  122.     {
  123.         $doctrineAuthAdapter = new ZendX_Doctrine_Auth_Adapter(
  124.             Doctrine_core::getConnectionByTableName('User')
  125.         );
  126.         $doctrineAuthAdapter->setTableName('User u')
  127.             ->setIdentityColumn('u.username')
  128.             ->setCredentialColumn('u.password')
  129.             ->setIdentity($username)
  130. //            ->setCredential($password);
  131.             ->setCredential(HR_Utility::generateHash($password));
  132.  
  133.         $myAuth = Zend_Auth::getInstance();
  134.         $authResult = $myAuth->authenticate($doctrineAuthAdapter);
  135.         if(!$authResult->isValid()) {
  136.             $this->_authenticationMessage = 'Feil brukernavn eller passord';
  137.             return false;
  138.         } else {
  139. //            $identity = $doctrineAuthAdapter->getResultRowObject(null, 'password');
  140.             $identity = User::findByUsername($username);
  141.             $myAuth->getStorage()->write($identity);
  142.             return true;
  143.         }
  144.     }
  145. }
  146.  
  147.  
  148. class LoginController extends Zend_Controller_Action
  149. {
  150.  
  151.     public function indexAction()
  152.     {
  153.         $loginForm = new Registration_Form_Login();
  154.         $this->view->headTitle('Login');
  155.  
  156.         $authService = new AuthenticationService();
  157.         if($authService->isAuthenticated() == true) {
  158.             $this->_redirect('/admin');
  159.         }
  160.  
  161.         if($this->getRequest()->isPost()) {
  162.             // collect the data from the user
  163.             $loginUsername = $this->getRequest()->getParam('username', '');
  164.             $loginPassword = $this->getRequest()->getParam('password', '');
  165.  
  166.             $authResult = $authService->authenticate(
  167.                 $loginUsername,
  168.                 $loginPassword
  169.             );
  170.  
  171.             if($authResult == true) {
  172.                 if ($remember) {
  173.                     Zend_Session::RememberMe($seconds);
  174.                 }
  175.                 else {
  176.                     Zend_Session::ForgetMe();
  177.                 }
  178.                 return $this->_helper->redirector('index', 'admin', 'default');
  179.  
  180.             }
  181.             else {
  182.                 echo 'bad auth!';
  183.             }
  184.         }
  185.  
  186.         $this->view->loginForm = $loginForm;
  187.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement