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/form.css', 'all');
12 $this->view->headLink()->appendStylesheet('css/login.css', 'all');
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();
23 $this->_jsLocaleStrings();
26 if (empty ($formData) or !$form->isValid($formData)) {
28 throw new Syj_Exception_Request();
34 /* form has been filled */
36 $adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
37 $authAdapter = new Zend_Auth_Adapter_DbTable($adapter, 'users', 'pseudo', 'password');
38 $authAdapter->setIdentity($formData['login_user'])
39 ->setCredential(sha1($formData['login_password']));
41 $auth = Zend_Auth::getInstance();
42 $result = $auth->authenticate($authAdapter);
43 if (!$result->isValid()) {
45 throw new Syj_Exception_Forbidden();
47 $form->addError('Wrong login/password');
52 $userid = $authAdapter->getResultRowObject('id')->id;
53 $this->_helper->SyjSession->login($userid);
54 $user = $this->_helper->SyjSession->user();
57 $api = $this->_helper->SyjApi->setCode(200);
58 $data = array('pseudo' => $user->pseudo);
60 $login_geom_id = $formData['login_geom_id'];
62 $path = new Syj_Model_Path();
63 $pathMapper = new Syj_Model_PathMapper();
64 if (!$pathMapper->find((int)$login_geom_id, $path)) {
65 throw new Syj_Exception_Request();
67 $data['iscreator'] = ($path->creator->id === $userid);
69 $data['iscreator'] = true;
71 $api->setBodyJson($data);
77 public function logoutAction() {
78 $this->_helper->SyjSession->logout();
82 protected function redirect($target = null) {
83 if (!isset($target)) {
84 $target = $this->getRequest()->getQuery('redirect');
87 if (!isset($target)) {
88 $target = $this->view->baseUrl();
94 $this->_helper->Redirector->gotoURL($target, array('prependBase' => false));
97 protected function _jsLocaleStrings() {
98 $this->view->jslocales = array(
99 'userEmptyWarn' => __("you must enter a login name"));