global $wgUser, $wgOut;
$user = User::newFromConfirmationCode( $code );
if( is_object( $user ) ) {
- if( $user->confirmEmail() ) {
- $message = $wgUser->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
- $wgOut->addWikiMsg( $message );
- if( !$wgUser->isLoggedIn() ) {
- $title = SpecialPage::getTitleFor( 'Userlogin' );
- $wgOut->returnToMain( true, $title->getPrefixedText() );
- }
- } else {
- $wgOut->addWikiMsg( 'confirmemail_error' );
+ $user->confirmEmail();
+ $user->saveSettings();
+ $message = $wgUser->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
+ $wgOut->addWikiMsg( $message );
+ if( !$wgUser->isLoggedIn() ) {
+ $title = SpecialPage::getTitleFor( 'Userlogin' );
+ $wgOut->returnToMain( true, $title->getPrefixedText() );
}
} else {
$wgOut->addWikiMsg( 'confirmemail_invalid' );
$user = User::newFromConfirmationCode( $code );
if( is_object( $user ) ) {
$user->invalidateEmail();
- if( $user->invalidateEmail() ) {
- $wgOut->addWikiMsg( 'confirmemail_invalidated' );
- if( !$wgUser->isLoggedIn() ) {
- $wgOut->returnToMain();
- }
- } else {
- $wgOut->addWikiMsg( 'confirmemail_error' );
+ $user->saveSettings();
+ $wgOut->addWikiMsg( 'confirmemail_invalidated' );
+ if( !$wgUser->isLoggedIn() ) {
+ $wgOut->returnToMain();
}
} else {
$wgOut->addWikiMsg( 'confirmemail_invalid' );
# Can't load from ID, user is anonymous
return false;
}
-
$this->saveToCache();
} else {
wfDebug( "Got user {$this->mId} from cache\n" );
*/
function saveToCache() {
$this->load();
+ $this->loadGroups();
if ( $this->isAnon() ) {
// Anonymous users are uncached
return;
return $user;
}
+ /**
+ * Create a new user object from a user row.
+ * The row should have all fields from the user table in it.
+ */
+ static function newFromRow( $row ) {
+ $user = new User;
+ $user->loadFromRow( $row );
+ return $user;
+ }
+
/**
* Get username given an id.
* @param integer $id Database user id
if ( $s !== false ) {
# Initialise user table data
- $this->mName = $s->user_name;
- $this->mRealName = $s->user_real_name;
- $this->mPassword = $s->user_password;
- $this->mNewpassword = $s->user_newpassword;
- $this->mNewpassTime = wfTimestampOrNull( TS_MW, $s->user_newpass_time );
- $this->mEmail = $s->user_email;
- $this->decodeOptions( $s->user_options );
- $this->mTouched = wfTimestamp(TS_MW,$s->user_touched);
- $this->mToken = $s->user_token;
- $this->mEmailAuthenticated = wfTimestampOrNull( TS_MW, $s->user_email_authenticated );
- $this->mEmailToken = $s->user_email_token;
- $this->mEmailTokenExpires = wfTimestampOrNull( TS_MW, $s->user_email_token_expires );
- $this->mRegistration = wfTimestampOrNull( TS_MW, $s->user_registration );
- $this->mEditCount = $s->user_editcount;
+ $this->loadFromRow( $s );
+ $this->mGroups = null; // deferred
$this->getEditCount(); // revalidation for nulls
+ return true;
+ } else {
+ # Invalid user_id
+ $this->mId = 0;
+ $this->loadDefaults();
+ return false;
+ }
+ }
+
+ /**
+ * Initialise the user object from a row from the user table
+ */
+ function loadFromRow( $row ) {
+ $this->mDataLoaded = true;
+
+ if ( isset( $row->user_id ) ) {
+ $this->mId = $row->user_id;
+ }
+ $this->mName = $row->user_name;
+ $this->mRealName = $row->user_real_name;
+ $this->mPassword = $row->user_password;
+ $this->mNewpassword = $row->user_newpassword;
+ $this->mNewpassTime = wfTimestampOrNull( TS_MW, $row->user_newpass_time );
+ $this->mEmail = $row->user_email;
+ $this->decodeOptions( $row->user_options );
+ $this->mTouched = wfTimestamp(TS_MW,$row->user_touched);
+ $this->mToken = $row->user_token;
+ $this->mEmailAuthenticated = wfTimestampOrNull( TS_MW, $row->user_email_authenticated );
+ $this->mEmailToken = $row->user_email_token;
+ $this->mEmailTokenExpires = wfTimestampOrNull( TS_MW, $row->user_email_token_expires );
+ $this->mRegistration = wfTimestampOrNull( TS_MW, $row->user_registration );
+ $this->mEditCount = $row->user_editcount;
+ }
- # Load group data
+ /**
+ * Load the groups from the database if they aren't already loaded
+ * @private
+ */
+ function loadGroups() {
+ if ( is_null( $this->mGroups ) ) {
+ $dbr = wfGetDB( DB_MASTER );
$res = $dbr->select( 'user_groups',
array( 'ug_group' ),
array( 'ug_user' => $this->mId ),
while( $row = $dbr->fetchObject( $res ) ) {
$this->mGroups[] = $row->ug_group;
}
- return true;
- } else {
- # Invalid user_id
- $this->mId = 0;
- $this->loadDefaults();
- return false;
}
}
function getEmail() {
$this->load();
+ wfRunHooks( 'UserGetEmail', array( $this, &$this->mEmail ) );
return $this->mEmail;
}
function getEmailAuthenticationTimestamp() {
$this->load();
+ wfRunHooks( 'UserGetEmailAuthenticationTimestamp', array( $this, &$this->mEmailAuthenticated ) );
return $this->mEmailAuthenticated;
}
function setEmail( $str ) {
$this->load();
$this->mEmail = $str;
+ wfRunHooks( 'UserSetEmail', array( $this, &$this->mEmail ) );
}
function getRealName() {
*/
function getEffectiveGroups( $recache = false ) {
if ( $recache || is_null( $this->mEffectiveGroups ) ) {
- $this->load();
- $this->mEffectiveGroups = $this->mGroups;
+ $this->mEffectiveGroups = $this->getGroups();
$this->mEffectiveGroups[] = '*';
- if( $this->mId ) {
+ if( $this->getId() ) {
$this->mEffectiveGroups[] = 'user';
$this->mEffectiveGroups = array_unique( array_merge(
* @param string $group
*/
function addGroup( $group ) {
- $this->load();
$dbw = wfGetDB( DB_MASTER );
if( $this->getId() ) {
$dbw->insert( 'user_groups',
array( 'IGNORE' ) );
}
+ $this->loadGroups();
$this->mGroups[] = $group;
$this->mRights = User::getGroupPermissions( $this->getEffectiveGroups( true ) );
),
'User::removeGroup' );
+ $this->loadGroups();
$this->mGroups = array_diff( $this->mGroups, array( $group ) );
$this->mRights = User::getGroupPermissions( $this->getEffectiveGroups( true ) );
'user_id' => $this->mId
), __METHOD__
);
+ wfRunHooks( 'UserSaveSettings', array( $this ) );
$this->clearSharedCache();
}
-
/**
* Checks if a user with the given name exists, returns the ID.
*/
* Generate a new e-mail confirmation token and send a confirmation/invalidation
* mail to the user's given address.
*
+ * Call saveSettings() after calling this function to commit the confirmation
+ * token to the database.
+ *
* @return mixed True on success, a WikiError object on failure.
*/
function sendConfirmationMail() {
/**
* Generate, store, and return a new e-mail confirmation code.
* A hash (unsalted since it's used as a key) is stored.
+ *
+ * Call saveSettings() after calling this function to commit
+ * this change to the database.
+ *
* @param &$expiration mixed output: accepts the expiration time
* @return string
* @private
$this->load();
$this->mEmailToken = $hash;
$this->mEmailTokenExpires = $expiration;
- $this->saveSettings();
return $token;
}
}
/**
- * Mark the e-mail address confirmed and save.
+ * Mark the e-mail address confirmed.
+ *
+ * Call saveSettings() after calling this function to commit the change.
*/
function confirmEmail() {
- $this->load();
- $this->mEmailAuthenticated = wfTimestampNow();
- $this->saveSettings();
+ $this->setEmailAuthenticationTimestamp( wfTimestampNow() );
return true;
}
/**
* Invalidate the user's email confirmation, unauthenticate the email
- * if it was already confirmed and save.
+ * if it was already confirmed.
+ *
+ * Call saveSettings() after calling this function to commit the change.
*/
function invalidateEmail() {
$this->load();
$this->mEmailToken = null;
$this->mEmailTokenExpires = null;
- $this->mEmailAuthenticated = null;
- $this->saveSettings();
+ $this->setEmailAuthenticationTimestamp( null );
return true;
}
+ function setEmailAuthenticationTimestamp( $timestamp ) {
+ $this->load();
+ $this->mEmailAuthenticated = $timestamp;
+ wfRunHooks( 'UserSetEmailAuthenticationTimestamp', array( $this, &$this->mEmailAuthenticated ) );
+ }
+
/**
* Is this user allowed to send e-mails within limits of current
* site configuration?