Esempio n. 1
0
 /**
  * Create controller function
  *
  * This function creates a new user.
  * 
  * The function validates the passed data
  * and return error and description of error
  * if the data is invalid. When the data is valid
  * a new user is created. 
  *
  * @return json|xml true on success false on error
  */
 public function create()
 {
     if ($this->isRequestValid("create")) {
         // Create a new user object, and fill it with the given data.
         $user = new LoveUser();
         $username = isset($_REQUEST['username']) ? trim($_REQUEST['username']) : '';
         $password = isset($_REQUEST['password']) ? $_REQUEST['password'] : '';
         $nickname = isset($_REQUEST['nickname']) ? trim($_REQUEST['nickname']) : '';
         $token = isset($_REQUEST['confirm_string']) ? trim($_REQUEST['confirm_string']) : uniqid();
         // TODO: Disable nickname collision checks, and enable soft failing.
         if (!$user->loadByUsername($username)) {
             if (!$user->loadByNickname($nickname)) {
                 $data = array("Username" => $username, "Password" => $password, "Nickname" => $nickname, "Active" => 1, "Confirmed" => 1, "Removed" => 0, "Admin" => 0, "Token" => $token, "DateAdded" => 0, "DateModified" => 0);
                 $user->loadData($data);
                 $id = $user->save();
                 // Push user created to the applications
                 if (!$this->getResponse()->pushUser($this->AppAuth->getAppName(), $id, 'pushCreateUser')) {
                     $this->setError("User could not be pushed to the registred applications.");
                 }
                 $this->response->addParams(array("id" => $user->getId(), "username" => $user->getUsername(), "nickname" => $user->getNickname(), "confirm_string" => $token, "token" => $_REQUEST["token"]));
             } else {
                 $this->setError("Nickname already registered!");
             }
         } else {
             $this->setError("Username already registered!");
         }
     }
     $this->completeResponse();
 }