Zend Auth Action Helper
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 74 7576 77 78 79 8081 82 83 84 8586 87 88 89 9091 92 93 | <?php require_once "Zend/Controller/Action/Helper/Abstract.php"; require_once "Zend/Auth.php"; class My_Controller_Action_Helper_AuthUsers extends Zend_Controller_Action_Helper_Abstract{ /** * Constructor * * @return void */ public function __construct() { } public function preDispatch() { if ($this->getRequest()->isPost() && $this->getRequest()->getParam("username")) { $username = $this->getRequest()->getParam("username"); $password = $this->getRequest()->getParam("password"); if ($this->authenticateUser($username, $password)){ ; }else{ $this->getActionController()->view->loginformerror = true; } } $this->getActionController()->view->isLoggedInUser = $this->isLoggedInUser(); } /** * Returns true if user is authenticated as user * @return bool */ public function isLoggedInUser() { $session = new Zend_Session_Namespace("auth"); return (isset($session->userLoggedIn) && $session->userLoggedIn); } /** * * @param string $username - username or email * @param string $password - password * @return bool */ public function authenticateUser($username, $password) { $auth = Zend_Auth::getInstance(); $db = Zend_Db_Table::getDefaultAdapter(); if (strpos($username, "@") === false){ $authAdapter = new Zend_Auth_Adapter_DbTable($db, "users", "username", "password"); }else{ $authAdapter = new Zend_Auth_Adapter_DbTable($db, "users", "email", "password"); } $authAdapter->setIdentity($username)->setCredential(hash("sha256", "23ns" . $password . "cx89a")); $authResult = $auth->authenticate($authAdapter); if ($authResult->isValid()){ //valid username and password $userInfo = $authAdapter->getResultRowObject(); $user = new ZF_User(); $user = $user->find($userInfo->id); //save userinfo in session $session = new Zend_Session_Namespace("auth"); $session->userLoggedIn = true; $session->userId = $userInfo->id; $session->user = $user; return true; }else{ return false; } } public function logout() { $auth = Zend_Auth::getInstance(); $auth->clearIdentity(); $session = new Zend_Session_Namespace("auth"); $session->userLoggedIn = false; $session->userId = null; $session->user = null; Zend_Session::forgetMe(); } } |
Comments
You must login before commenting on a snippet. If you do not have an account, please register.
Snippet description
This action helper authenticates user login. It expects either email address or username from the username field of login form and save the password using one way encryption "hash". It creates a namespace with the name of "auth" in session. Variable "isLoggedInUser()" method can be called (for example in preDispatch) to check whether user is logged in or not.
Snippet details
- Created:
-
neeraj
- Edited:
-
neeraj
- Revision Id:
- 224
- Edit Message:
- Initial Release
- ZF Version
- 1.8.0
- Tags:
- zend auth action helper
- Comments:
- 0
- Views:
- 854
- Points:
- 0 (0 votes)