1

Scenario: An external service wakes up. This could be a remote Drupal site running a cron , a mobile application being started or something completely different.

The service has a session id from it's last interaction with a Drupal site running Services.

How does this service go about asking the Drupal site: "Based on this session ID, who am I?"?

For a regular user on the Drupal site, /user will tell you who you are. However, when accessing [endpoints]/user through Services, you get an index of all users, instead of a specific user object.

If possible, please include json as the content type for any code examples.

5
  • When you connect to an endpoint with session authentication you should be receiving a user object back, which matches the user that's logged in. I think that would be the most reliable way to check, short of writing your own 'whoami' resource (which actually would be very easy, let me know if you want me to dump some code in)
    – Clive
    Commented Nov 9, 2012 at 8:59
  • Hmm, are you saying all resources in an endpoint will return the user? That doesn't seem to be the case for me, nor do I understand why that would be? If there is no "whoami" by defalt in services, then please dump the code :) If so, are you ok with me making it a patch against services?
    – Letharion
    Commented Nov 9, 2012 at 9:06
  • Nah it won't come back for all resources, but standard practice is to authenticate, save the session id and any other user information you need for the rest of the session somewhere local to the calling app, then use that session id to make subsequent connections. That way you've got everything linked up and you don't need to establish a new session for every call (unless there's enough time between them that the session will expire, your app should be checking for that). I'll put some code together for a resource after I've made a coffee ;) Oh and yeah feel free to submit it as a patch
    – Clive
    Commented Nov 9, 2012 at 9:18
  • Ok, thanks. I sort of realised that between my comment and your second one. In my case, use of IP Login means the login action is implicit, and the user object doesn't get returned.
    – Letharion
    Commented Nov 9, 2012 at 9:33
  • Ah ok, not used that module before it looks interesting
    – Clive
    Commented Nov 9, 2012 at 9:41

1 Answer 1

2

This service resource simply return an object representing the currently authenticated user:

/**
 * Implements hook_services_resources().
 */
function MYMODULE_services_resources() {
  $resources['MYMODULE'] = array(
    'actions' => array(
      'whoami' => array(
        'help' => 'Get an object referring to the currently authenticated user.',
        'callback' => '_MYMODULE_whoami_service_resource',
        'access callback' => 'services_access_menu'
      )
    )
  );

  return $resources;
}


/**
 * Resource callback
 */
function _MYMODULE_whoami_service_resource() {
  return $GLOBALS['user'];
}

Once enabled you should be able to access the resource at http://site.com/[endpoint]/MYMODULE/whoami.json.

4
  • Thanks! I unfortunately get 404 Not found: Could not find the controller. when I try. I've ensured I copy paste the resource name in place of MYMODULE, and whoami from the action name, into the url. I'll try to debug it and see what I can figure out. Some quick hacking shows the access callback being called atleast.
    – Letharion
    Commented Nov 9, 2012 at 14:45
  • Ah, I was GETing an action, which doesn't work. With POST it worked as expected, thanks again! :)
    – Letharion
    Commented Nov 9, 2012 at 15:11
  • Oops yeah should have mentioned, sorry :)
    – Clive
    Commented Nov 9, 2012 at 15:21
  • Published as a sandbox project. Updated your answer to show both that it works and is published.
    – Letharion
    Commented Nov 13, 2012 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.