Extension:On Campus

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
On Campus

Release status: experimental

Implementation User rights
Description Restrict a namespace from being viewed outside a subnet.
Last Version n/a (2006-05-11)
MediaWiki 1.5 and 1.6
License No license specified
Download see below

This is a nearly trivial extension for restricting read access of a particular namespace to a subnet, or to people who are logged in. It is NOT a production-quality extension; you'll need to alter the code to fit it to your own needs. It's plenty secure, just not very flexible, because it matches an IP address against a regular expression instead of a CIDR subnet mask. I'm just sharing it here to save you a few minutes of research. Usage:

1. Make a custom namespace with number 100, and make sure its Talk sibling is 101.

2. Plop this into your extensions/ folder, and call it OnCampus.php:

 <?php
 $wgHooks['userCan'][] = 'onCampus_Check';
 
 function onCampus_Check($title, $user, $action, $result)
 {
    if ($action == 'read' && ($title->getNamespace() == 100 || $title->getNamespace() == 101)) {
        if (preg_match("/^137\.165\./", $_SERVER['REMOTE_ADDR']) || $user->isLoggedIn())
        {
            $result = true;
        }
        else {
            $result = false;
        }
    }
    else
    {
          $result = false;
    }
 
    return $result;
 
 }
 ?>

3. Change the regular expression to test for your particular subnet. A real version of this extension would test the IP address against a subnet mask, but this suited my purposes just fine.

4. Add this to LocalSettings.php:

require( "extensions/OnCampus.php" );
Personal tools