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