public function createAction() { $this->_errorStack = Noobh_ErrorStackSingleton::getInstance(); $request = $this->getRequest(); $params = $request->getParams(); $firstName = htmlspecialchars($params['firstName']); $lastName = htmlspecialchars($params['lastName']); $username = htmlspecialchars($params['userName']); $password = htmlspecialchars($params['password']); $email = htmlspecialchars($params['email']); $password = md5($password); $response = array(); try { $user = new Models_User(); $user->setFirstName($firstName); $user->setLastName($lastName); $user->setEmail($email); $user->setUsername($username); $user->setPassword($password); if (!empty($this->_errorStack)) { $user->save(); $response['error_code'] = 0; $response['user'] = $user->getHash(); } else { $response['errors'] = $this->_errorStack->getErrorList(); } } catch (Exception $ex) { $response['error_code'] = $ex->getCode(); $response['error_message'] = $ex->getMessage(); } echo json_encode($response); exit; }
/** * Update user data using user id. * * @access public * @param int $userId [user id] * @param array $data [user data] * @return bool */ public function update($userId, $data) { $this->_errorStack = Noobh_ErrorStackSingleton::getInstance(); try { if (is_array($data)) { $userModel = new Models_User(); $user = $userModel->getUserBy($email = null, $userId); if ($user) { $firstName = htmlspecialchars($data['firstName']); $lastName = htmlspecialchars($data['lastName']); $username = htmlspecialchars($data['username']); $password = htmlspecialchars($data['password']); $userModel->setFirstName($firstName); $userModel->setLastName($lastName); $userModel->setUsername($username); $userModel->setPassword($password); if (empty($this->_errorStack->getErrorList())) { $userModel->save(); } } else { throw new Exception($this->_errorList[602], 602); } } else { throw new Exception($this->_errorList[600], 600); } } catch (Exception $ex) { $code = $ex->getCode(); $message = $ex->getMessage(); $this->_errorStack->push(self::VALIDATION_TYPE, $code, $message); Noobh_Log::error($message); throw new Exception($message); } }
public function createAction() { $response = array(); try { $request = $this->getRequest(); if ($request->isPOST()) { $params = $request->getParams(); /** TODO: Currently validting with munber of input parameters. Need to solve it in better way. */ if (count($params) != 5) { $response['error_code'] = 823; $response['errors'] = $this->_errorList[823]; echo json_encode($response); exit; } $firstName = htmlspecialchars($params['firstName']); $lastName = htmlspecialchars($params['lastName']); $username = htmlspecialchars($params['userName']); $password = htmlspecialchars($params['password']); $email = htmlspecialchars($params['email']); $password = md5($password); $user = new Models_User($email); if (!empty($user->getId())) { $response['error_code'] = 821; $response['errors'] = $this->_errorList[821]; echo json_encode($response); exit; } else { $user->setFirstName($firstName); $user->setLastName($lastName); $user->setEmail($email); $user->setUsername($username); $user->setPassword($password); if (!empty($this->_errorStack)) { if ($user->save()) { $response['error_code'] = 0; $response['user'] = $user->getHash(); } else { $response['error_code'] = 813; $response['error_message'] = $this->_errorList[813]; } } else { $response['error_code'] = 813; $response['error_message'] = $this->_errorStack->getErrorList(); } } } else { $response['error_code'] = 400; $response['error_message'] = 'Bad Request'; } } catch (Exception $ex) { $response['error_code'] = 813; $response['error_message'] = $this->_errorList[813]; } echo json_encode($response); exit; }