Pagine private agli utenti loggati

← MediaWiki

Estensione Restrict access by category and group

https://www.mediawiki.org/wiki/Extension:Restrict_access_by_category_and_group

Abbiamo modificato questa estensione per:

  • permettere agli utenti loggati di vedere le pagine definite private
  • Impedire ai visitatori non loggati di vedere suddette pagine

Installazione

Abbiamo creato il file extensions/RestrictAccessByCategoryAndGroup/RestrictAccessByCategoryAndGroup.php con questo contenuto:

<?php

if ( !defined( 'MEDIAWIKI' ) ) {
	die( 'Not a valid entry point.' );
}

$wgExtensionCredits['parserhook'][] = array(
	'name' => 'Restrict access by category and group',
	'author' => 'Andrés Orencio Ramirez Perez',
	'url' => 'https://www.mediawiki.org/wiki/Extension:Restrict_access_by_category_and_group',
	'description' => 'Allows to restrict access to pages by users groups and page categories',
	'version' => '2.0.1'
);

$wgHooks['userCan'][] = 'restrictAccessByCategoryAndGroup';

function restrictAccessByCategoryAndGroup( $title, $user, $action, $result ) {
	global $wgGroupPermissions;
	global $wgWhitelistRead;
	global $wgLang;
	global $wgHooks;
	global $wgContLang;
	global $wgWhitelistRead;
	global $wgVersion;

	if ( $user->isLoggedIn() ) {
		return true;
	}
	
	//The Main Page, Login and Logout pages should always be accessible
	if ( $wgVersion >= '1.17' ) {
		$wgWhitelistRead[] = wfMessage( 'mainpage' )->plain();
	} else {
		$wgWhitelistRead[] = wfMsgForContent( 'mainpage' );
	}
        $wgWhitelistRead[] = SpecialPage::getTitleFor( 'Userlogin' )->getLocalUrl();
        $wgWhitelistRead[] = SpecialPage::getTitleFor( 'Userlogout' )->getLocalUrl();

	$validCategory = false;
	$groupExists = false;
	$pageHasCategories = false;
	$privateCategory = false;
	$privateCategoryTemp = false;
	$categoryNamespace = $wgLang->getNsText( NS_CATEGORY );
	$whitePage = true;

	//System categories
	$systemCategory = array();
	foreach ( array_change_key_case( $title->getParentCategories(), CASE_LOWER ) as $key => $value ) {
		$formatedKey = substr( $key, ( strpos( $key, ":" ) + 1 ) );
		$systemCategory[ $formatedKey ] = $value;
	}

	//Is this page a white page?
	if ( isset( $wgWhitelistRead[0] ) ) {
		$whitePage = in_array( $title, $wgWhitelistRead );
	}

	//If the page has no categories, it's public.
	if ( count( $title->getParentCategories() ) == 0 ) {
		$validCategory = true;
	} else {
		//For each system categories
		foreach ( $wgGroupPermissions as $key => $value ) {
			//If current system category is defined as private, then tmpCatP is true
			if ( isset( $wgGroupPermissions[ $key ]['private'] ) ) {
				$privateCategoryTemp = $wgGroupPermissions[ $key ]['private'];
			} else {
				$privateCategoryTemp = false;
			}
			//If current system category exist in the document category array ...
			if ( array_key_exists( strtolower( str_replace( " ", "_", $key ) ), $systemCategory ) ) {
				if ( $privateCategoryTemp and !$privateCategory ) {
					$privateCategory = true;
					$validCategory = false;
				}
				//We see that the user belongs to one of the groups (like of category)
				if ( in_array( $key, $user->getGroups() ) and ( !$privateCategory or ( $privateCategoryTemp and $privateCategory ) ) ) {
					$validCategory = true;
				}
				$groupExists = true;
			}
		}
		$pageHasCategories = count( $title->getParentCategories() ) > 0;
	}

	if ( !$pageHasCategories ) {
		return true;
	}
	if ( !$groupExists and !$whitePage ) {
		return true;
	}
	if ( ( $user->isLoggedIn() and $validCategory ) or $whitePage ) {
		return true;
	}
	return false;
}

Abbiamo importato l'estensione modificando il file LocalSettings.php aggiungendo le righe

// estensione protezione accesso alle pagine private
require_once "$IP/extensions/RestrictAccessByCategoryAndGroup/RestrictAccessByCategoryAndGroup.php";
// blocco delle pagine con categoria "Private" 
$wgGroupPermissions['Private']['private'] = true;

Ora basta inserire nella pagina la categoria inserendo il codice

[[Category:Private]]