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->_helper->SyjMedias->addScripts('login');
10 $this->view->headLink()->appendStylesheet('css/generic.css', 'all');
11 $this->view->headLink()->appendStylesheet('css/login.css', 'all');
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();
22 $this->_jsLocaleStrings();
25 if (empty ($formData) or !$form->isValid($formData)) {
27 throw new Syj_Exception_Request();
33 /* form has been filled */
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']));
40 $auth = Zend_Auth::getInstance();
41 $result = $auth->authenticate($authAdapter);
42 if (!$result->isValid()) {
44 throw new Syj_Exception_Forbidden();
46 $form->addError('Wrong login/password');
51 $userid = $authAdapter->getResultRowObject('id')->id;
52 $this->_helper->SyjSession->login($userid);
53 $user = $this->_helper->SyjSession->user();
56 $api = $this->_helper->SyjApi->setCode(200);
57 $data = array('pseudo' => $user->pseudo);
59 $login_geom_id = $formData['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();
66 $data['iscreator'] = ($path->creator->id === $userid);
68 $data['iscreator'] = true;
70 $api->setBodyJson($data);
76 public function logoutAction() {
77 $this->_helper->SyjSession->logout();
81 protected function redirect($target = null) {
82 if (!isset($target)) {
83 $target = $this->getRequest()->getQuery('redirect');
86 if (!isset($target)) {
87 $target = $this->view->baseUrl();
93 $this->_helper->Redirector->gotoURL($target, array('prependBase' => false));
96 protected function _jsLocaleStrings() {
97 $this->view->jslocales = array(
98 'userEmptyWarn' => __("you must enter a login name"));