Tuesday, January 10, 2012

only allow certain users can login from a public IP address, other users can only login from company IP address

only allow certain users can login from a public IP address, other users can only login from company IP address.

If other users are trying to login outside of company, they will be denied.

<?php
/*
 * Implementation of hook_user()
 */
function myModule_user($op, &$edit, &$account, $category = NULL) {
  if ($op === 'login') {
    $ip_address = ip_address();

    if (!in_array($account->name, array('admin', 'admin2'))) {
      if (!in_array($ip_address, array('202.173.122.136'))) {
        if (!function_exists('user_logout')) {
          // include the file directly.
          //include_once(drupal_get_path('module', 'user') . '/user.pages.inc');

          // Proper way to load the include files
          module_load_include('inc', 'user', 'user.pages');
        }
        user_logout();
      }
    }
  }
}
?>

No comments: