/** * @method setEmailAddress * @param {string} $emailAddress * @param {boolean} [$verified=false] * @throws {Q_Exception_MissingRow} * If e-mail address is missing * @throws {Users_Exception_AlreadyVerified} * If user is already verified * @throws {Users_Exception_WrongState} * If verification state is wrong */ function setEmailAddress($emailAddress, $verified = false) { $email = new Users_Email(); Q_Valid::email($emailAddress, $normalized); $email->address = $normalized; $retrieved = $email->retrieve('*', array('ignoreCache' => true)); if (empty($email->activationCode)) { $email->activationCode = ''; $email->activationCodeExpires = '0000-00-00 00:00:00'; } $email->authCode = md5(microtime() + mt_rand()); if ($verified) { $email->userId = $this->id; } else { if (!$retrieved) { throw new Q_Exception_MissingRow(array('table' => "an email", 'criteria' => "address {$emailAddress}"), 'emailAddress'); } if ($email->userId != $this->id) { // We're going to tell them it's verified for someone else, // even though it may not have been verified yet. // In the future, might throw a more accurate exception. throw new Users_Exception_AlreadyVerified(array('key' => 'email address', 'userId' => $email->userId)); } if (!in_array($email->state, array('unverified', 'active'))) { throw new Users_Exception_WrongState(array('key' => $email->address, 'state' => $email->state), 'emailAddress'); } } // Everything is okay. Assign it! $email->state = 'active'; $email->save(); $ui = new Users_Identify(); $ui->identifier = "email_hashed:" . Q_Utils::hash($normalized); $ui->state = 'verified'; $ui->userId = $this->id; $ui->save(true); $this->emailAddressPending = ''; $this->emailAddress = $emailAddress; $this->save(); $user = $this; Q_Response::removeNotice('Users/email'); /** * @event Users/setEmailAddress {after} * @param {string} user * @param {string} email */ Q::event('Users/setEmailAddress', compact('user', 'email'), 'after'); return true; }
function setEmailAddress($email_address) { $e = new Users_Email(); $e->address = $email_address; if (!$e->retrieve()) { throw new Pie_Exception_MissingRow(array('table' => "an email", 'criteria' => "address {$email_address}"), 'email_address'); } if ($e->user_id != $this->id) { // We're going to tell them it's verified for someone else, // even though it may not have been verified yet. // In the future, might throw a more accurate exception. throw new Users_Exception_AlreadyVerified(array('key' => $e->address, 'user_id' => $e->user_id)); } if ($e->state != 'unverified') { throw new Users_Exception_WrongState(array('key' => $e->address, 'state' => $e->state), 'email_address'); } // Everything is okay. Assign it! $this->email_address = $email_address; $e->state = 'active'; $e->save(); Pie::event('users/setEmailAddress', compact('email_address'), 'after'); return true; }