Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Getting these errors when trying to log in. I'm using zend_auth if a doctrine adapter.
- 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
- Fatal error: Cannot redeclare class Registration_Model_Acl in /media/data/dev/php/mjfreg/application/models/Acl.php on line 38
- 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
- zend_auth somehow.
- */
- Class HR_Controller_Plugin_Auth extends Zend_Controller_Plugin_Abstract
- {
- private $_auth;
- private $_acl;
- private $_noauth = array('module' => 'default',
- 'controller' => 'login',
- 'action' => 'index');
- private $_noacl = array('module' => 'default',
- 'controller' => 'error',
- 'action' => 'privileges');
- public function __construct($auth, $acl)
- {
- $this->_auth = $auth;
- $this->_acl = $acl;
- }
- public function preDispatch($request)
- {
- if ($this->_auth->hasIdentity()) {
- $role = $this->_auth->getIdentity()->role;
- }
- else {
- $role = 'guest';
- }
- $controller = $request->controller;
- $action = $request->action;
- $module = $request->module;
- $resource = $controller;
- if (!$this->_acl->has($resource)) {
- $resource = null;
- }
- if (!$this->_acl->isAllowed($role, $resource, $action)) {
- if (!$this->_auth->hasIdentity()) {
- $module = $this->_noauth['module'];
- $controller = $this->_noauth['controller'];
- $action = $this->_noauth['action'];
- } else {
- $module = $this->_noacl['module'];
- $controller = $this->_noacl['controller'];
- $action = $this->_noacl['action'];
- }
- }
- $request->setModuleName($module);
- $request->setControllerName($controller);
- $request->setActionName($action);
- }
- }
- class Registration_Model_Acl extends Zend_Acl
- {
- public function __construct()
- {
- $this->addRole(new Zend_Acl_Role('admin'));
- $this->addRole(new Zend_Acl_Role('guest'));
- $this->add(new Zend_Acl_Resource('form'));
- $this->add(new Zend_Acl_Resource('user'));
- $this->add(new Zend_Acl_Resource('field'));
- $this->add(new Zend_Acl_Resource('field-extra'));
- $this->add(new Zend_Acl_Resource('field-reply'));
- $this->add(new Zend_Acl_Resource('form-type'));
- $this->add(new Zend_Acl_Resource('admin'));
- $this->add(new Zend_Acl_Resource('reply'));
- $this->add(new Zend_Acl_Resource('index'));
- $this->allow('guest', 'index');
- $this->allow('guest', 'form', 'fill');
- $this->allow('guest', 'form', 'thanks');
- $this->allow("admin","admin");
- }
- }
- // in bootstrap.php
- protected function _initAclPlugin()
- {
- $front = Zend_Controller_Front::getInstance();
- $auth = Zend_Auth::getInstance();
- $acl = new Registration_Model_Acl();
- $front->registerPlugin(new HR_Controller_Plugin_Auth($auth, $acl));
- }
- class AuthenticationService
- {
- private $_authenticationMessage = '';
- public function getAuthenticationMessage()
- {
- return $this->_authenticationMessage;
- }
- public function isAuthenticated()
- {
- return Zend_Auth::getInstance()->hasIdentity();
- }
- public function authenticate($username, $password)
- {
- $doctrineAuthAdapter = new ZendX_Doctrine_Auth_Adapter(
- Doctrine_core::getConnectionByTableName('User')
- );
- $doctrineAuthAdapter->setTableName('User u')
- ->setIdentityColumn('u.username')
- ->setCredentialColumn('u.password')
- ->setIdentity($username)
- // ->setCredential($password);
- ->setCredential(HR_Utility::generateHash($password));
- $myAuth = Zend_Auth::getInstance();
- $authResult = $myAuth->authenticate($doctrineAuthAdapter);
- if(!$authResult->isValid()) {
- $this->_authenticationMessage = 'Feil brukernavn eller passord';
- return false;
- } else {
- // $identity = $doctrineAuthAdapter->getResultRowObject(null, 'password');
- $identity = User::findByUsername($username);
- $myAuth->getStorage()->write($identity);
- return true;
- }
- }
- }
- class LoginController extends Zend_Controller_Action
- {
- public function indexAction()
- {
- $loginForm = new Registration_Form_Login();
- $this->view->headTitle('Login');
- $authService = new AuthenticationService();
- if($authService->isAuthenticated() == true) {
- $this->_redirect('/admin');
- }
- if($this->getRequest()->isPost()) {
- // collect the data from the user
- $loginUsername = $this->getRequest()->getParam('username', '');
- $loginPassword = $this->getRequest()->getParam('password', '');
- $authResult = $authService->authenticate(
- $loginUsername,
- $loginPassword
- );
- if($authResult == true) {
- if ($remember) {
- Zend_Session::RememberMe($seconds);
- }
- else {
- Zend_Session::ForgetMe();
- }
- return $this->_helper->redirector('index', 'admin', 'default');
- }
- else {
- echo 'bad auth!';
- }
- }
- $this->view->loginForm = $loginForm;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement