If a user does not have permission to access (other) users profiles, he does have permission to view his own. The link in the logged-in block is not shown, but it should.

Comments

hunmonk’s picture

Status: Active » Closed (works as designed)

i use theme_username to generate that link, which is a standard drupal core theme call. if you're not happy with how it works, you'll need to file an issue against drupal core.

jkr’s picture

I think theme_username was designed for theming other peoples usernames, not so much your own. But I agree, it would be best to change this in core: #1138704: have template_preprocess_username return link if processing current user.

jamesoakley’s picture

Version: 7.x-1.2 » 7.x-1.x-dev
Priority: Minor » Normal
Status: Closed (works as designed) » Needs review
StatusFileSize
new561 bytes

How about this for a patch?

The reason you don't get a link unless the administer users permission exists is because we are trying to get the themed username for an arbitrary user, where it just happens to be the currently logged in user.

One way to solve that would be to patch core to make an exception when a user tries to access their own username as a (linked) HTML entity.

But another way is for Login Toboggan not to link to /user/[uid], but simply to link to /user, putting the username as the content of the anchor.

Hence this patch. (check_plain is needed because the format_username returns the raw username)

Anonymous’s picture

Status: Needs review » Reviewed & tested by the community

@JamesOakley thank you very much for this patch!

While I agree this could be done by fixing core, but let's be real about this that we're now at version 7.23 already and I don't think a minor theme thing should require core devs to get involved... because that is going to require doing it in D8 first, and by the time we get a backport we're all going to be upgrading to D8.

The patch lets users get to their accounts without introducing any security vulnerabilities.

Yes, I realize this could be done in a custom theme override... but a patch is easier in my case, and also more secure knowing that the check_plain() is there. +1 RTBC. I'm going into production with this patch.

gbirch’s picture

A slightly different approach, which still invokes the protections offered by template_preprocess_username(). All you need to do is patch theme_lt_loggedinblock() to add a link_attributes and link_path to the array that is passed to theme_username.

I have profile2 installed, so YMMV in terms of the name of the permission in user_access(), but this override in my template.php works like a charm

/**
 * Override the default LoginTobbogan user block so that users get links to their own profiles
 * (Drupal's template_preprocess_username only adds a link for users who may see ALL profiles)
 */
function MYTHEME_lt_loggedinblock($variables){
  $account = $variables['account'];
  $theme_data = array('account' => $account);
  if ( (int) $account->uid && user_access('view own main profile') ) {
    //copying code from template_preprocess_username()
    $theme_data['link_attributes'] = array('title' => t('View user profile.'));
    $theme_data['link_path'] = 'user/' . $account->uid;
  }
  return theme('username', $theme_data) .' | ' . l(t('Log out'), 'user/logout');
}
jamesoakley’s picture

But surely this shouldn't have to be done at the theme level. The module should output a valid link at this point whatever the theme, and the patch that's at RTBC establishes that.

I understand that the maintainers don't want to progress feature-requests, but I wonder if either of them would like to commit this bug-fix patch now it's RTBC?

  • JamesOakley authored 99de70a on 7.x-1.x
    Issue #1138122 by JamesOakley: Profile link in logged-in block is...
stevecowie’s picture

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

Finally committed! I agree with the point at #6 so have committed as is.

stevecowie’s picture

Status: Fixed » Closed (fixed)
jamesoakley’s picture

Thanks, Steve

gbirch’s picture

This is a very old thread, and I should have responded to comment #6 when it came in (I don't know why I missed it). But I think this is a mistake. The reality is that Drupal core is doing some stuff in the theming layer that this patch now bypasses. So whether it's silly to do that in the theming layer or not, the fact is that that is where it happens for Drupal core.

The code in template_preprocess_username() that is bypassed by this patch includes:

$name = $variables['name_raw'] = format_username($account);
  if (drupal_strlen($name) > 20) {
    $name = drupal_substr($name, 0, 15) . '...';
  }
  $variables['name'] = check_plain($name);

and, this:

// We do not want the l() function to check_plain() a second time.
  $variables['link_options']['html'] = TRUE;
  // Set a default class.
  $variables['attributes_array'] = array('class' => array('username'));

That seems like a mistake: among other things, the loss of the "username" class may break styling on existing sites.

jamesoakley’s picture

I understand the problems you highlight. But, surely, whatever the correct solution is, someone should be able to install

  • vanilla Drupal 7
  • Login Toboggan
  • Bartik

and the block should work correctly.

The patch as committed may have undesired side-effects in the block. But, in the absence of a change to core (which seems unlikely this late in the lifecycle of Drupal 7), we need an alternative patch against this module alone that will solve the problem.

gbirch’s picture

I'm not proposing a change to core, I'm proposing a different change to theme_lt_loggedinblock(). I apologize for not supplying a patch, and will try to do so later today, but the code is posted above.

jamesoakley’s picture

OK, thanks. I misunderstood. I thought you were suggesting a template change at the theme level, rather than a patch for the module.

gbirch’s picture

Here's the promised patch.