]> dev.renevier.net Git - syj.git/blob - application/controllers/LoginController.php
remember me checkbox for login
[syj.git] / application / controllers / LoginController.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010-2011 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         if (!$this->_helper->SyjUserManager->validate($formData['login_user'],
36                                                      sha1($formData['login_password']),
37                                                      $formData['login_rememberme'])) {
38             if ($httprequest) {
39                 throw new Syj_Exception_Forbidden();
40             } else {
41                 $form->addError('Wrong login/password');
42                 return;
43             }
44         }
45
46         $user = $this->_helper->SyjUserManager->current();
47
48         if ($httprequest) {
49             $api = $this->_helper->SyjApi->setCode(200);
50             $data = array('pseudo' => $user->pseudo);
51
52             $login_geom_id = $formData['login_geom_id'];
53             if ($login_geom_id) {
54                 $path = new Syj_Model_Path();
55                 $pathMapper = new Syj_Model_PathMapper();
56                 if (!$pathMapper->find((int)$login_geom_id, $path)) {
57                     throw new Syj_Exception_Request();
58                 }
59                 $data['iscreator'] = ($path->creator->id === $userid);
60             } else {
61                 $data['iscreator'] = true;
62             }
63             $api->setBodyJson($data);
64         } else {
65             $this->redirect();
66         }
67     }
68
69     public function logoutAction() {
70         $this->_helper->SyjUserManager->logout();
71         $this->redirect();
72     }
73
74     protected function redirect($target = null) {
75         if (!isset($target)) {
76             $target = $this->getRequest()->getQuery('redirect');
77         }
78
79         if (!isset($target)) {
80             $target = $this->view->baseUrl();
81         }
82         if (!$target) {
83             $target = '/';
84         }
85
86         $this->_helper->Redirector->gotoURL($target, array('prependBase' => false));
87     }
88
89     protected function _jsLocaleStrings() {
90         $this->view->jslocales = array(
91             'userEmptyWarn' => __("you must enter a login name"));
92     }
93 }