Advertisement
Guest User

Untitled

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