]> dev.renevier.net Git - syj.git/blob - application/controllers/UserController.php
update to latest Zend and modifiablepath versions
[syj.git] / application / controllers / UserController.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
3     and is published under the AGPL license. */
4
5 class UserController extends Zend_Controller_Action
6 {
7
8     public function existsAction() {
9         $name = $this->getRequest()->getUserParam('name');
10
11         $userMapper = new Syj_Model_UserMapper();
12         $user = new Syj_Model_User();
13
14         if ($userMapper->findByPseudo($name, $user)) {
15             $this->_helper->SyjApi->setCode(200);
16         } else {
17             // opera needs some body content with 404 code, otherwise, it
18             // reports a xmlhttprequest.status of 0
19             $this->_helper->SyjApi->setCode(404)->setBody(' ');
20         }
21     }
22
23     public function userAction() {
24         $formData = $this->_helper->SyjPostData->getPostData('Syj_Form_User');
25
26         // XXX: we validate email server side *only* so we don't want to
27         // validate in SyjPostData. Then, we validate email outside Syj_Form_User
28         $emailValidator = new Syj_Validate_EmailAddress();
29         if (!$emailValidator->isValid($formData['user_email'])) {
30             throw new Syj_Exception_InvalidEmail();
31         }
32
33         $user = new Syj_Model_User();
34         $user->pseudo = $formData["user_pseudo"];
35         $user->password = sha1($formData["user_password"]);
36         $user->email = $formData["user_email"];
37
38         if (Zend_Registry::isRegistered('Zend_Translate')) {
39             $translator = Zend_Registry::get('Zend_Translate');
40             $adapter = $translator->getAdapter();
41             $locale = new Zend_Locale($adapter->getLocale());
42             $user->lang = $locale->getLanguage();
43         }
44
45         $userMapper = new Syj_Model_UserMapper();
46
47         try {
48             $userMapper->save ($user);
49         } catch(Zend_Db_Statement_Exception $e) {
50             if ($e->getCode() == 23505) { // 23505: Unique violation throw new Syj_Exception_Request();
51                 $message = $e->getMessage();
52                 if (strpos($message, 'users_pseudo_key') !== false) {
53                     throw new Syj_Exception_Request("uniquepseudo");
54                 } else if (strpos($message, 'users_email_key') !== false) {
55                     throw new Syj_Exception_Request("uniqueemail");
56                 } else {
57                     throw $e;
58                 }
59             } else {
60                 throw $e;
61             }
62         }
63
64         $this->_helper->SyjSession->login($user->id);
65
66         $data = array('pseudo' => $user->pseudo);
67         $this->_helper->SyjApi->setBodyJson($data)->setCode(200);
68     }
69 }