Posted by chaloum on August 3, 2012 at 1:29am
3 followers
Jump to:
| Project: | Masquerade |
| Version: | 7.x-1.0-rc4 |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed (fixed) |
Issue Summary
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
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
#1
<?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.
#2
Unless there's additional feedback or you have other questions, I think this can be marked as fixed.
#3
what if your not using views integration?
#4
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.
#5
thanks I'll give it a go
#6
Automatically closed -- issue fixed for 2 weeks with no activity.
#7
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?
#8
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');#9
Thanks after a bit of mucking around I figured it out