IP-based Frontend Accounts

Several days ago, I made reference to the amazing depth of of TYPO3.  Even after using the content management system day in a day out for more than 3 years, I’m still uncovering new features all the time. The latest to add to my list is the ability to automatically add visitors to a frontend usergroup and filter content based only on an IP address.

A client and friend of WEC runs a website that provides premium content behind a subscription.  Users can register for accounts to access this content, which works with the standard TYPO3 access controls for frontend users and groups. In addition to these individual subscribers who sign up on their own, libraries can subscribe to this content and make it available to anyone who logs on while at the library.

Our job was to come up with a way to transparently make this content available to libraries while also providing the TYPO3 administrators an easy way to add new libraries and provide content to them.

The first part of our task turned out to be yet another hidden gem inside TYPO3 as API-level support for IP-based Frontend Groups already existed.  By adding the following code to localconf.php, any visitor matching the IP mask (192.168.*, 127.*) will automatically be treated as part of the frontend group with a UID of 3.

 $GLOBALS['TYPO3_CONF_VARS']['FE']['IPmaskMountGroups'][] = array('192.168.*, 127.*,3);

From this static definition, its pretty clear to see how you could also write the value dynamically based on database records.  In our case, we used the TSFE’s initFEUser hook to get our code running early in the page rendering process.

// get where clause
$where = 'disabled = 0 AND deleted=0 AND (('.time().' > starttime AND '.time().' < endtime) OR (starttime = 0 AND endtime = 0) OR (starttime = 0 AND '.time().' < endtime) OR ('.time().' > starttime AND endtime = 0))';

// get all account records from the db
$res = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'tx_weciplogin_accounts', $where);

// loop through results
foreach( $res as $account ) {

	// explode the fe user groups into an array and iterate over them
	foreach( explode(',', $account['feusergroup']) as $groupid ) {
		// add masks to typo3 conf vars
		$GLOBALS['TYPO3_CONF_VARS']['FE']['IPmaskMountGroups'][] = array($account['ip'], $groupid);
	}
}

With the actual group assignment out the way, it was simply a matter of building a user interface so that the TYPO3 administrators could easily manage library subscriptions.  In the screenshot below, we see an IP-based frontend account that is active for one year. Any visitor matching the IP mask will automatically be treated as a member of the “Registered Users” group and see content specific to that group.

(Special thanks to Christoph for his help with this extension.  I know he loves it when I finally blog and its about something that he already knows all about)


About this entry