I found a bug where ctools_access_get_loggedin_context doesn't fully load the current user when building the logged in user context.

This meant fields from the user weren't available.

The logged in user context is currently created like this:

/**
 * Create a context for the logged in user.
 */
function ctools_access_get_loggedin_context() {
  global $user;
  $context = ctools_context_create('entity:user', $user);
  $context->identifier = t('Logged in user');
  $context->keyword    = 'viewer';
  $context->id         = 0;

  return $context;
}

However, the related function ctools_context_create_user never actually loads the user entity ( Because $conf is passed as FALSE and $data ($user object) is not numeric )

See:

/**
 * It's important to remember that $conf is optional here, because contexts
 * are not always created from the UI.
 */
function ctools_context_create_user($empty, $data = NULL, $conf = FALSE) {
  $context = new ctools_context(array('entity:user', 'entity', 'user'));
  $context->plugin = 'user';

  if ($empty) {
    return $context;
  }

  if ($conf) {
    if ($data['type'] == 'current') {
      global $user;
      $data = user_load($user->uid);
      $data->logged_in_user = TRUE;
    }
    else {
      $data = user_load($data['uid']);
    }
  }
  // Load entity if the data provided is a numeric value. This kind of data is
  // passed by some relationships.
  if (is_numeric($data)) {
    $data = user_load($data);
  }

  if (!empty($data)) {
    $context->data     = $data;
    $context->title    = isset($data->name) ? $data->name : t('Anonymous');
    $context->argument = $data->uid;
    return $context;
  }
}

The attached patch simply makes the function ctools_access_get_loggedin_context user the $data['type'] == 'current' parameter, which is supported by ctools_context_create_user and I believe should be used by ctools_access_get_loggedin_context anyway.

The result is a properly fully loaded current logged in user context.

Please review and commit if possible, thanks.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Angry Dan’s picture

Status: Needs review » Needs work

Couple of points:

Another approach could have been to change ctools_context_create_user(), e.g:

if ($data['type'] == 'current' || $data === $GLOBALS['user']]) {

Other than that, patch looks good. One note: ctools_access_get_loggedin_context() won't need the global $user any more.

davidwhthomas’s picture

Status: Needs work » Needs review
FileSize
593 bytes

Here's another version of the patch that removes the now unused global $user;

I think passing the 'current' parameter is most suitable here, as that's already supported in ctools_context_create_user

jhedstrom’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

I can confirm that the patch in #2 resolves an issue I was seeing with user entity fields not being available during variant selection.

japerry’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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

Kojo Unsui’s picture

This issue seems to be related : https://drupal.org/node/2212641#comment-8804059 :
on any panelized page (Commons distrib) , ctools_context_create_user() gets Array ([type] => current) as 2nd parameter, then sets $data->logged_in_user = TRUE; even for anonymous...

Kojo Unsui’s picture

Status: Closed (fixed) » Active
davidwhthomas’s picture

Status: Active » Needs review
FileSize
530 bytes

@Kojo

Yes, there does appear to be a related issue that affects getting the current user context for an anonymous user.

I've attached a patch that checks the `user_is_logged_in` and will only set `$data->logged_in_user = TRUE;` if the user is indeed authenticated.

Kojo Unsui’s picture

Status: Needs review » Fixed

Fine, applied successfully. I didn't know user_is_logged_in(). Thanks

Status: Fixed » Closed (fixed)

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

ACF’s picture

Status: Closed (fixed) » Reviewed & tested by the community

Not sure why this was closed. This bug is still there and this patch fixes it.

japerry’s picture

Status: Reviewed & tested by the community » Fixed

Fixed! thanks for the report.

  • japerry committed ac9abf5 on 6.x-1.x authored by davidwhthomas
    Issue #2010124 by davidwhthomas: ctools_access_get_loggedin_context...

  • japerry committed a5345ad on 7.x-1.x authored by davidwhthomas
    Issue #2010124 by davidwhthomas: ctools_access_get_loggedin_context...

Status: Fixed » Closed (fixed)

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