loginradiusloginradius Blog

Separate Drupal Login Page for Admin and User

Are you afraid of hackers and feel unsafe for admin and front user to login through same area? No need to worry as in this article, I am going to guide you with…

Are you afraid of hackers and feel unsafe for admin and front user to login through same area?
No need to worry as in this article, I am going to guide you with how to create separate login area for admin. Along with that, I will also provide the required steps to disable administrator login through user/login page.

Please follow the following implementation steps.

Step by step Guide:

  1. In the first step, you need to implement hook_menu in your module file.
 t('Admin Login Page'),
    'page callback' => 'yourmodule_admin_login_page',
    'page arguments' => array(),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  return $menu_items;
}-->
  1. After adding menu in module file, the next step is to define the page callback function yourmoduleadminlogin_page. In this function, we’ll define the layout of admin login page using theme.
function yourmodule_admin_login_page() {
  return theme('yourmodule_admin_login_template');
}
  1. Now that you have defined page callback function, the next step is to implement hook_theme in your module file.
array(
      'template' => 'yourmodule-admin_login',
      'render element' => 'form',
      'path' => $path,
    ),
  );
  return $theme;
}-->
  1. After implementation of hook theme in module file, the next step is to implement preprocess function for template file.
 'hidden',
    '#value' => 'admin_login',
    '#name' => 'hidden_admin_login_form',
  );
  $variables['rendered'] = drupal_render($form);
}-->

Note : In the above code, we have used the hidden element to identify the admin login page.

  1. After adding menu in module file, the next step is to define the page callback function yourmodule_admin_login_page. In this function, we’ll define the layout of admin login page using theme.

'yourmodule-admin_login.tpl.php'

  1. Now paste the following code in yourmodule-admin_login.tpl.php file.
<!--
  
-->
  1. After that, clear your website’s cache and check the following url for your admin login page

URL : your-site-domain/admin/login

All above steps are used to show admin login page. But if you want that only administrator can login through page admin/login then follow the following the next steps.

  1. In this step, you need to implement hook_form_alter function. In this function we will add custom validation.
function yourmodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_login') {
    $form['#validate'][] = 'yourmodule_external_validate';
  }
}
  1. After that, add the following code to define the custom validation in which only administrator can login.
roles);
    if ($access_granted) {
      return TRUE;
    }
    else {
      form_set_error('', t('Only site administrator can login.'));
      return FALSE;
    }
  }
}-->

Note: If you want that administrator can’t login through user login page then add following code to do this.

Add the mentioned code in function yourmodule_external_validate

roles);
    if ($user_access_granted) {
      form_set_error('', t('Only front user can login.'));
      return FALSE;
    }
    else {
      return TRUE;
    }
}-->

Complete code of function yourmodule_external_validate looks like:

roles);
    if ($access_granted) {
      return TRUE;
    }
    else {
      form_set_error('', t('Only site administrator can login.'));
      return FALSE;
    }
  }
  else {
    $user = user_load($form_state['uid']);
    $user_access_granted = in_array('administrator', $user->roles);
    if ($user_access_granted) {
      form_set_error('', t('Only front user can login.'));
      return FALSE;
    }
    else {
      return TRUE;
    }
  }
}-->

If you are searching over the web and can’t find anything to create separate admin login page in drupal, Please follow the mentioned above steps and you are done. Also create different layout for this  admin login page.

Versha Gupta

Written by Versha Gupta

She is a software enthusiast, an avid contributor, and a regular blog writer. She usually works with React, UI/UX, Javascript, Node.js, and DevOps.

0

LoginRadius CIAM Platform

Our Product Experts will show you the power of the LoginRadius CIAM platform, discuss use-cases, and prove out ROI for your business.

Book A Demo Today