2 /* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
3 and is published under the AGPL license. */
5 class UserController extends Zend_Controller_Action
8 public function existsAction() {
9 $name = $this->getRequest()->getUserParam('name');
11 $userMapper = new Syj_Model_UserMapper();
12 $user = new Syj_Model_User();
14 if ($userMapper->findByPseudo($name, $user)) {
15 $this->_helper->SyjApi->setCode(200);
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(' ');
23 public function userAction() {
24 $formData = $this->_helper->SyjPostData->getPostData('Syj_Form_User');
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();
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"];
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();
45 $userMapper = new Syj_Model_UserMapper();
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");
64 $this->_helper->SyjSession->login($user->id);
65 $this->_helper->SyjApi->setCode(200);