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