You are using the fetching method "fetchCol" which returns always an indexed array. This causes an error that you always masquerade yourself. Changing the named method to "fetchColumn" will fix this issue.

An example:

function masquerade_init() {
	global $user;

	$uid = db_query("SELECT uid_from FROM {masquerade} WHERE sid = :sid AND uid_as = :uid_as", array(
				':sid' => session_id(),
				':uid_as' => $user->uid,
			))->fetchColumn();

	/* $uid is never a boolean */
	if ($uid === FALSE) { 
		if (isset($_SESSION)) {
			unset($_SESSION['masquerading']);
		}
	} else {
		$_SESSION['masquerading'] = $uid;
	}
}

Comments

Renderintent’s picture

My example shows the fixed version, here is the original ;-)

function masquerade_init() {
	global $user;

	$uid = db_query("SELECT uid_from FROM {masquerade} WHERE sid = :sid AND uid_as = :uid_as", array(
				':sid' => session_id(),
				':uid_as' => $user->uid,
			))->fetchCol();

	/* $uid is never a boolean */
	if ($uid === FALSE) {
		if (isset($_SESSION)) {
			unset($_SESSION['masquerading']);
		}
	} else {
		$_SESSION['masquerading'] = $uid;
	}
}
bfroehle’s picture

Status: Active » Needs work

If this is a bug, the correct fix would be to use fetchField(), not fetchColumn() ?

bfroehle’s picture

Title: fetchCol -> fetchColumn » Use fetchCol / fetchField appropriately
Version: 7.x-1.0-rc2 » 7.x-1.x-dev
Status: Needs work » Needs review
StatusFileSize
new3.72 KB

Patch attached, using fetchField() for most (but not all!) instances of fetchCol(). In general, we call fetchField() when we just want one value out of the database query, and fetchCol() when we want a whole array.

From http://drupal.org/node/310072:

// Retrieve a 1-column result set as one single array.
$result->fetchCol();

// Get a single value out of the database.
$title = db_query("SELECT title FROM {node} WHERE nid = :nid", array(':nid' => $nid))->fetchField();
pmorel’s picture

Status: Needs review » Reviewed & tested by the community

This patch fix the issue where users are always masquerading themselves (tested only for the admin user)

andypost’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

  • Commit dfaf124 on master, 8.x-2.x, 8.x-2.x-admin-menu, 8.x-1.x-1836516 by andypost:
    Issue #1055940 by Renderintent, bfroehle : Use fetchCol / fetchField...

  • Commit dfaf124 on master, 8.x-2.x, 8.x-2.x-admin-menu, 8.x-1.x-1836516 by andypost:
    Issue #1055940 by Renderintent, bfroehle : Use fetchCol / fetchField...