1

In hook menu, there is a url that is used to be called by some gps device to register its lcoation in the database. Let say the url is sitename.com/gps/location/123.123/456.456

But I want to make this url, so only authenticated devices/sources can call this.

How should we achieve this. May be we can use hash token, but I am not sure how to use this to verify the source which is calling the url.

Here is the code inside the hook_menu

$items['gps/location/%/%'] = array(
  'title' => '',
  'page callback' => 'gps_lcoation_store',
  'page arguments' => array(2,3),
  'access arguments' => array('access content'),
);

Any help?

  • You can give a try to access callback in hook_menu, where you can check for hash token or check if request is authenticated. – Yogesh Dec 20 '16 at 7:35
2
$items['gps/location/%/%'] = array(
  'title' => '',
  'page callback' => 'gps_lcoation_store',
  'page arguments' => array(2,3),
  'access callback' => 'custom_is_allowed_location',
  'access arguments' => array(2, 3),
);

function custom_is_allowed_location($arg1, $arg2) {
   //IF LOCATION IS XXXXX THEN ALLOWED
   if($arg1 == xxxx && $arg2 == xxxx) {
       return TRUE;
   }
   return FALSE;
}
1

Using the idea given by @khoomy and @Yogesh, here is what I have done.

$items['gps/location/%/%'] = array(
  'title' => '',
  'page callback' => 'gps_lcoation_store',
  'page arguments' => array(2,3),
  'access callback' => 'custom_is_allowed_location',
);

function custom_is_allowed_location($arg1, $arg2) {
  // Ip address of the accessing machine/device.
  $varrified_ip = '12.34.56.78';
  if (ip_address() == $varrified_ip) {
    return TRUE;
  }
  return FALSE;
}

Since it will be access by a specific device, having an IP address, so checking the IP address of the source.

0

I agreed with Yogesh, use can use access callback and/or to use hook_menu_alter to override if it is in contributed module. So in later case you can have full control over that menu

In both cases access callback will be the good option.

  • 1
    I think that you need to update your other answer with this info – Adrian Cid Almaguer Dec 30 '16 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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