/** * @NotLogged * @param RegisterBindingModel $model * @throws \Exception */ public function register(RegisterBindingModel $model) { if ($model->getPassword() !== $model->getConfirm()) { throw new \Exception("Password don't match Confirm Password!", 400); } if (!preg_match('/^[\\w]{3,15}$/', $model->getUsername())) { throw new \Exception("Invalid username format!", 400); } // Check for already registered with the same name $this->db->prepare("SELECT id\n FROM users\n WHERE username = ?", array($model->getUsername())); $response = $this->db->execute()->fetchRowAssoc(); $id = $response['id']; if ($id !== null) { $username = $model->getUsername(); throw new \Exception("Username '{$username}' already taken!", 400); } // Check for already registered with the same email $this->db->prepare("SELECT id\n FROM users\n WHERE email = ?", array($model->getEmail())); $response = $this->db->execute()->fetchRowAssoc(); $id = $response['id']; if ($id !== null) { $email = $model->getEmail(); throw new \Exception("Email '{$email}' already taken!", 400); } $this->db->prepare("INSERT\n INTO users\n (username, password, email)\n VALUES (?, ?, ?)", array($model->getUsername(), $model->getPassword(), $model->getEmail()))->execute(); $loginBindingModel = new LoginBindingModel(array('username' => $model->getUsername(), 'password' => $model->getPassword())); // Work around to avoid double crypting passwords. $loginBindingModel->afterRegisterPasswordPass($model->getPassword()); $this->login($loginBindingModel); }
/** * @param RegisterBindingModel $model * @return int * @throws \Exception */ function register(RegisterBindingModel $model) : int { $db = SimpleDB::getInstance('conference_scheduler'); $username = $model->getUsername(); if (self::usernameExists($model->getUsername())) { throw new \Exception("Username {$username} already exists."); } $result = $db->prepare("INSERT INTO users (username, password, email)\r\n VALUES(?, ?, ?)"); $result->execute([$model->getUsername(), $model->getPassword(), $model->getEmail()]); if ($result->affectedRows() < 1) { throw new \Exception("Cannot register the user."); } return intval($db->getLastInsertedId()); }
/** * @BindingModels RegisterBindingModel */ public function registerpost(RegisterBindingModel $bindingModel) { if ($bindingModel) { $user = new User(); $user->setUsername($bindingModel->getUsername()); $user->setPassword($bindingModel->getPassword()); $user->setCash(); $user->setRole(); $success = $this->data->register($user); if ($success) { $this->initLogin($user->getUsername(), $user->getPassword()); } else { throw new \Exception('Cannot register user'); } } }