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