2 /* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
3 and is published under the AGPL license. */
5 class LoginController extends Zend_Controller_Action
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');
13 $this->view->headLink()->appendStylesheet('css/login.css');
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();
24 $this->_jsLocaleStrings();
27 if (empty ($formData) or !$form->isValid($formData)) {
29 throw new Syj_Exception_Request();
35 /* form has been filled */
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']));
42 $auth = Zend_Auth::getInstance();
43 $result = $auth->authenticate($authAdapter);
44 if (!$result->isValid()) {
46 throw new Syj_Exception_Forbidden();
48 $form->addError('Wrong login/password');
53 $userid = $authAdapter->getResultRowObject('id')->id;
54 $auth->getStorage()->write(array('user' => $userid));
55 Zend_Session::rememberMe(); // zend default expiration delay is 2 weeks. Ok, use that value
59 $api = $this->_helper->SyjApi->setCode(200);
61 $login_geom_id = $formData['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();
68 if ($path->owner->id === $userid) {
69 $api->setBody("1"); // owner of displayed geometry
74 $api->setBody("1"); // no geometry displayed: owner of the (future) geometry
81 public function logoutAction() {
82 Zend_Session::start();
83 Zend_Session::destroy();
87 protected function redirect($target = null) {
88 if (!isset($target)) {
89 $target = $this->getRequest()->getQuery('redirect');
92 if (!isset($target)) {
93 $target = $this->view->baseUrl();
99 $this->_helper->Redirector->gotoURL($target, array('prependBase' => false));
102 protected function _jsLocaleStrings() {
103 $this->view->jslocales = array(
104 'userEmptyWarn' => __("you must enter a login name"));