I am wanting to create a link an an admin report where the username is as masquerade link in the same way that is is on the in the admin/people/users page

Masquerade as jimbob

I notice the link isn't straight forward as there is a token.

So How can I do about getting the token so I can create a link?

Comments

wjaspers’s picture

<?php
// Get the link token.
// You can move this further down if you like and add an optional value parameter to it.
$token = drupal_get_token();

// Find the user you want.
// Don't reload the user unless you have to--in many cases, you may already have access to them as an object.
$account = user_load(4); // whatever user you want.

// Generate the title of the link.
$link_text = t('Masquerade as @username', array('@username' => $account->name));

// Generates a "masquerade as <username>" link.
$link = l($link_text, 'masquerade/switch/' . $account->uid, array(
 
'query' => array(
   
'token' => $token
 
),
));
?>

Shameless plug, as well, but I've already addressed this problem with http://dgo.to/masquerade_extras if you use the Views integration.

wjaspers’s picture

Status:Active» Fixed

Unless there's additional feedback or you have other questions, I think this can be marked as fixed.

chaloum’s picture

what if your not using views integration?

wjaspers’s picture

I should have clarified.
The problem of generating a custom link is solved with masquerade_views by generating lists from contextual arguments.

Alternatively, if you need a one-off solution, the above code will work.
Load the user you want in a custom module, and attach the link to the output you want, or in your theme.

chaloum’s picture

thanks I'll give it a go

Status:Fixed» Closed (fixed)

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

chaloum’s picture

I have tried this but I get the message, Access denied, You are not authorized to access this page.

If I copy the token from the users list it works but If i use the generated token $token = drupal_get_token(); I get the error.

It doesn't seem to like the the token generated by my module

any ideas on how to generate a token that masquerade likes?

wjaspers’s picture

Oops, I missed a parameter on drupal_get_token().
You need to tell it the path you want to hit (i.e. it should be the same as the "masquerade/switch/###" where ### is the UID you want.

Example:

$token = drupal_get_token('masquerade/switch/272817');
chaloum’s picture

Thanks after a bit of mucking around I figured it out