/** * Make sure username is valid * Make sure it's not already in use * * @return object $this */ protected function validateUsername() { $username = strtolower($this->getSubmittedValue('username')); if (false === Validate::username($username)) { $this->setError('username', 'This username is invalid. Username must contain only letters, numbers and a hyphen and MUST start and end with letter or number and MUST be at least 3 characters long'); } $aReserved = \Lampcms\getReservedNames(); if (in_array($username, $aReserved)) { $this->setError('username', 'This username is already in use'); } $a = $this->Registry->Mongo->USERS->findOne(array('username_lc' => $username)); if (!empty($a)) { $this->setError('username', 'This username is already in use'); } return $this; }
/** * Validation to check that user * with this username or email address * exists in the database * If user exists, set the $this->forgottenUid * to the value of this user's id * * @return bool true if user found, otherwise false * and in case of false also sets form errors * so that user will see the form with errors */ protected function validateUser() { $this->login = \mb_strtolower($this->Form->getSubmittedValue('login')); d('$this->login: '******'cp'); $this->byEmail = true; $aEmail = $this->Registry->Mongo->EMAILS->findOne(array('email' => $this->login)); if (empty($aEmail)) { $this->Form->setError('login', 'No user with this email address'); return false; } d('$aEmail: ' . print_r($aEmail, 1)); $aResult = $this->Registry->Mongo->USERS->findOne(array('_id' => (int) $aEmail['i_uid'])); } else { if (false === \Lampcms\Validate::username($this->login)) { d('cp'); $this->Form->setError('login', 'This username is invalid'); return false; } $aResult = $this->Registry->Mongo->USERS->findOne(array('username_lc' => $this->login)); } if (empty($aResult)) { d('cp'); $this->Form->setError('login', 'User Not found'); return false; } /** * @todo * * if 'usertype' == 'email' * then user does not have login * Just test and then throw an exception? * Actually maybe it's better if user could just login * then edit profile and become regular user... * * But how would we do that? We would bacially activate * a user on first login. */ d('$aResult: ' . \print_r($aResult, 1)); /** * If username exists but email does not * such as the case when user is external user who has * not yet provided email address * */ if (empty($aResult['email'])) { throw new \Lampcms\Exception('This is an external account and you have not provided a valid email address for it'); } /** * @todo if user does not have username * then we should use email address instead * user should be able to login using email address! * */ $this->uid = $aResult['_id']; $this->login = !empty($aResult['username']) ? $aResult['username'] : $aResult['email']; $this->emailAddress = $aResult['email']; return true; }
/** * @todo check that this username does not already * exist * * @throws \Lampcms\FormException is username is invalid or already taken * * @return object $this */ protected function checkUsername() { if (empty($this->Request['username'])) { return $this; } /** * If user has not changed suggested username than * we don't have to worry about validating it */ if ($this->Request['username'] === $this->Registry->Viewer->username) { return $this; } if (false === Validate::username($this->Request['username'])) { throw new \Lampcms\FormException('@@Invalid username. Username can only contain English letters, numbers and a hyphen (but cannot start or end with the hyphen)@@', 'username'); } $aReserved = \Lampcms\getReservedNames(); $username = \mb_strtolower($this->Request['username']); $aUser = $this->Registry->Mongo->USERS->findOne(array(Schema::USERNAME_LOWERCASE => $username)); if (!empty($aUser) || in_array($username, $aReserved)) { throw new \Lampcms\FormException('@@Someone else is already using this username@@. <br> @@Please choose a different username and resubmit the form@@', 'username'); } /** * Need to set $this->username because * it's used in sendActivationEmail() */ $this->username = $this->Request['username']; return $this; }