]> dev.renevier.net Git - syj.git/blob - application/controllers/LoginController.php
update to latest Zend and modifiablepath versions
[syj.git] / application / controllers / LoginController.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
3     and is published under the AGPL license. */
4
5 class LoginController extends Zend_Controller_Action
6 {
7     public function init() {
8         $this->view->headTitle($this->view->translate("login"));
9         $this->_helper->SyjMedias->addScripts('login');
10         $this->view->headLink()->appendStylesheet('css/generic.css', 'all');
11         $this->view->headLink()->appendStylesheet('css/form.css', 'all');
12         $this->view->headLink()->appendStylesheet('css/login.css', 'all');
13     }
14
15     public function loginAction() {
16         $form = new Syj_Form_Login(array('name' => 'loginform'));
17         $request = $this->getRequest();
18         $formData = $request->getPost();
19         $this->view->form = $form;
20         $httprequest = $request->isXmlHttpRequest();
21
22         if (!$httprequest) {
23             $this->_jsLocaleStrings();
24         }
25
26         if (empty ($formData) or !$form->isValid($formData)) {
27             if ($httprequest) {
28                 throw new Syj_Exception_Request();
29             } else {
30                 return;
31             }
32         }
33
34         /* form has been filled */
35
36         $adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
37         $authAdapter = new Zend_Auth_Adapter_DbTable($adapter, 'users', 'pseudo', 'password');
38         $authAdapter->setIdentity($formData['login_user'])
39                 ->setCredential(sha1($formData['login_password']));
40
41         $auth = Zend_Auth::getInstance();
42         $result = $auth->authenticate($authAdapter);
43         if (!$result->isValid()) {
44             if ($httprequest) {
45                 throw new Syj_Exception_Forbidden();
46             } else {
47                 $form->addError('Wrong login/password');
48                 return;
49             }
50         }
51
52         $userid = $authAdapter->getResultRowObject('id')->id;
53         $this->_helper->SyjSession->login($userid);
54         $user = $this->_helper->SyjSession->user();
55
56         if ($httprequest) {
57             $api = $this->_helper->SyjApi->setCode(200);
58             $data = array('pseudo' => $user->pseudo);
59
60             $login_geom_id = $formData['login_geom_id'];
61             if ($login_geom_id) {
62                 $path = new Syj_Model_Path();
63                 $pathMapper = new Syj_Model_PathMapper();
64                 if (!$pathMapper->find((int)$login_geom_id, $path)) {
65                     throw new Syj_Exception_Request();
66                 }
67                 $data['iscreator'] = ($path->creator->id === $userid);
68             } else {
69                 $data['iscreator'] = true;
70             }
71             $api->setBodyJson($data);
72         } else {
73             $this->redirect();
74         }
75     }
76
77     public function logoutAction() {
78         $this->_helper->SyjSession->logout();
79         $this->redirect();
80     }
81
82     protected function redirect($target = null) {
83         if (!isset($target)) {
84             $target = $this->getRequest()->getQuery('redirect');
85         }
86
87         if (!isset($target)) {
88             $target = $this->view->baseUrl();
89         }
90         if (!$target) {
91             $target = '/';
92         }
93
94         $this->_helper->Redirector->gotoURL($target, array('prependBase' => false));
95     }
96
97     protected function _jsLocaleStrings() {
98         $this->view->jslocales = array(
99             'userEmptyWarn' => __("you must enter a login name"));
100     }
101 }