· 7 years ago · Aug 27, 2018, 01:16 AM
1<?php
2
3 public function loginAction() {
4
5 $this->view->showRegisterLink = true;
6 $query = $this->getRequest()->getQuery();
7 if (array_key_exists('next', $query)) {
8 $this->view->next = $query['next'];
9 } else {
10 $this->view->next = false;
11 }
12 $rules = with(new IsItLegit_Rule_Set())
13 ->add(new Q_Rule_NotNull(), array('username','password'), 'This is a required field');
14 if ($this->getRequest()->isPost()) {
15 $post = $this->getRequest()->getPost();
16 if (array_key_exists('next', $post)) {
17 $this->view->next = $post['next'];
18 }
19 if ($rules->validate($post)) {
20 $auth = Zend_Auth::getInstance();
21 $secretKey = $this->_config->security->secretKey;
22 $authAdapter = new Zend_Auth_Adapter_DbTable(
23 $this->_getModel('user')->getAdapter(),
24 $this->_getModel('user')->info(IsItLegit_Db_Table::NAME),
25 'username',
26 'password',
27 "SHA1(CONCAT(salt, '$', ?, '$', '" . $secretKey . "')) AND status = '" . DbTable_User::STATUS_ACTIVE . "'"
28 );
29 $authAdapter->setIdentity($post['username'])
30 ->setCredential($post['password']);
31 $result = $auth->authenticate($authAdapter);
32 if ($result->isValid()) {
33 $storage = $auth->getStorage();
34 $resultRowObject = $authAdapter->getResultRowObject();
35 $storage->write($resultRowObject);
36 $message = 'You are now logged in as ' . $result->getIdentity();
37 // @todo Put this somewhere where it happens automatically on login
38 $this->_helper->logger->setUserId($resultRowObject->user_id);
39 $this->_helper->logger('Logged in', 'user');
40 $this->_checkMessages();
41 if (array_key_exists('next', $post)) {
42 $next = $post['next'];
43 $this->_helper->flashMessenger($message, 'success');
44 $this->_redirect($next);
45 } else {
46 $this->_flashToRoute($message, 'success', 'home');
47 }
48 } else {
49 $this->_flashToRoute('Invalid login', 'error', 'login');
50 }
51 } else {
52 $this->_flash('You must fill out the form correctly', 'notice');
53 }
54 }
55
56 }