1

How does one limit "what" can access a page. I have little understanding on how to use hook_permission() to set user permission based on a role to control access to hook_menu() items. However, what if I have a hook_menu() item that is to be used solely by the system itself? For example, say I have a registration page in a custom module: module/register - Anyone can access that page. Then, say I have an another page which is only for admin. So, I register another item in hook_menu(), call it module/register/reg_user_details - I do not want anyone to be able to browse to module/register/reg_user_details

Instead I want page to be visible only when user is logged in, I have already created a hook_permission

/**
 * Implements hook_menu()
 * 
 */ 
function video_subtitles_menu() {
  $items = array();
  $items['video_subtitles/upload'] = array( //this creates a URL that will call this form at "video_subtitles_test/upload"
    'title' => 'Upload Subtitle', //page title
    'description' => 'Uploading subtitle for videos',
    'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed.  for a form, drupal_get_form need to be used 
    'page arguments' => array('video_upload_subtitles_form'), //Name of the Uplaod Form 
    'access callback' => 'user_access',
    'access arguments' => array('administer video_subtitles module'),
  );

  $items['player/video_subtitle_status'] = array(
     'page callback' => 'video_subtitle_status',
     'access callback' => 'user_access',
     'access arguments' => array('administer video_subtitles status'),
  );
  return $items;
}

/**
 * Implements hook_permission.
 */
function video_subtitles_permission() {
  return array(
    'administer video_subtitles module' => array(
      'title' => t('Administer video_subtitles module'),
      'description' => t('Access the video_subtitles upload module Page'),
     ),
    'administer video_subtitles status' => array(
      'title' => t('Administer video_subtitles module status'),
      'description' => t('Access the video_subtitles module status Page'),
  ));
}

So User is not able to access my page unless user is logged in, but what I need user should be redirected to the login page when he is not logged in as admin , if he successfully login it should be redirected to my page.

  1. visit url d-7/example/my-module
  2. if already logged in show the page
  3. if not logged in redirect to the login page
  4. if user logged in successfully redirect to d-7/example/my-module page

What is best way to achieve this

Similar Question

using-hook-menu-with-hook-permission-access-denied

using-hook-menu-and-hook-permission-to-control-access

can-someone-explain-access-arguments-in-drupal

2 Answers 2

2

Easiest way will be specifying this logic in page callback function. example:

function SOME_PAGE_CALLBACK($someArg) {
  if(user_is_anonymous()) {
    drupal_goto('PATH_TO_LOGIN_PAGE');
  }

  // do_something();
}

another way is checking the user in hook_preprocess_page function:

function MY_THEME_preprocess_page(&$variables) {
  if(curent_path() == 'SOME_PATH' && user_is_anonymous()) {
    drupal_goto('PATH_TO_LOGIN_PAGE');
  }
}

Access callback function checks the user's access to the request and returns boolean value, TRUE or FALSE, if you want to check if the user has an access to the page, you can do somthing like this:

$path = current_path();
if ($router_item = menu_get_item($path)) {
  if (!$router_item['access']) {
    drupal_goto('PATH_TO_LOGIN_PAGE');
  }
}
10
  • 1
    Why would you do access checks in the page callback and not the access callback?
    – Clive
    Apr 3, 2014 at 9:28
  • @Clive I don't know, this way is much more easier and interesting for me :)
    – xurshid29
    Apr 3, 2014 at 9:31
  • I had never even considered redirecting in the access callback, however it does seem more logical...
    – Felix Eve
    Apr 3, 2014 at 9:33
  • @Clive Also redirecting with drupal_goto would stop access callback function's proccess. Which breaks hook_menu's functionality I think (because, access callback must return boolean value)...
    – xurshid29
    Apr 3, 2014 at 9:37
  • 1
    But it wont' return a boolean value in this case as the user has already been redirected off to the login page. Seems logical.
    – Felix Eve
    Apr 3, 2014 at 9:40
1

Allow access to all users in hook_menu:

$items['player/my_page'] = array(
    'title' => t('My Page'), // note this is a required parameter
    'page callback' => 'my_page',
    'access callback' => TRUE,
);

And then in your page callback function redirect users that are not logged in:

function my_page() {

    global $user;   
    if(!$user->uid) {
        $dest = drupal_get_destination();
        drupal_goto('user/login', $dest); / this remembers where the user is coming from
    }

    //  Carry on with rest of code...

}

An alternative approach here pointed out by Clive is to do this checking in the access callback function and then leaving the page callback function for logic associated with building the page. I think this is a better approach as separates these 2 distinct tasks into their own functions.

0

Your Answer

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

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