Skip to content

Commit

Permalink
feat: Block users by blocked emails
Browse files Browse the repository at this point in the history
  • Loading branch information
lens0021 committed May 25, 2024
1 parent 64e6724 commit 0770bee
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
9 changes: 9 additions & 0 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@
"DefaultOptions": {
"class": "MediaWiki\\Extension\\UnifiedExtensionForFemiwiki\\HookHandlers\\DefaultOptions",
"services": ["MainConfig", "UserOptionsManager"]
},
"SpamEmail": {
"class": "MediaWiki\\Extension\\UnifiedExtensionForFemiwiki\\HookHandlers\\SpamEmail",
"services": ["MainConfig", "DBLoadBalancer", "DatabaseBlockStore"]
}
},
"Hooks": {
"HtmlPageLinkRendererBegin": "main",
"isValidEmailAddr": "SpamEmail",
"LinkerMakeExternalLink": "main",
"OutputPageParserOutput": "RelatedArticles",
"SidebarBeforeOutput": "main",
Expand All @@ -63,6 +68,10 @@
"UnifiedExtensionForFemiwikiRelatedArticlesTargetNamespaces": {
"value": [],
"description": "An array of namespaces which can be a target for RelatedArticles. Empty array means allowing all namespaces."
},
"UnifiedExtensionForFemiwikiBlockByEmail": {
"value": true,
"description": "Block emails used by block users"
}
},
"TestAutoloadNamespaces": {
Expand Down
80 changes: 80 additions & 0 deletions includes/HookHandlers/SpamEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace MediaWiki\Extension\UnifiedExtensionForFemiwiki\HookHandlers;

use Config;
use MediaWiki\Block\DatabaseBlockStore;
use MediaWiki\Extension\SpamBlacklist\BaseBlacklist;
use MediaWiki\User\User;
use Wikimedia\AtEase\AtEase;
use Wikimedia\Rdbms\ILoadBalancer;

class SpamEmail implements
\MediaWiki\Hook\IsValidEmailAddrHook
{

/** @var Config */
private $config;

/** @var ILoadBalancer */
private $loadBalancer;

/** @var DatabaseBlockStore */
private $databaseBlockStore;

/**
* @param Config $config
* @param ILoadBalancer $loadBalancer
* @param DatabaseBlockStore $databaseBlockStore
*/
public function __construct(
Config $config,
ILoadBalancer $loadBalancer,
DatabaseBlockStore $databaseBlockStore
) {
$this->config = $config;
$this->loadBalancer = $loadBalancer;
$this->databaseBlockStore = $databaseBlockStore;
}

/** @inheritDoc */
public function onIsValidEmailAddr( $addr, &$result ) {
if ( !$this->config->get( 'UnifiedExtensionForFemiwikiBlockByEmail' ) ) {
return true;
}

// Check againt MediaWiki:Email-blacklist
$denylist = BaseBlacklist::getEmailBlacklist()->getBlacklists();
foreach ( $denylist as $regex ) {
AtEase::suppressWarnings();
$match = preg_match( $regex, $addr );
AtEase::restoreWarnings();
if ( $match ) {
return false;
}
}

// Check email addresses of block users
if ( version_compare( '1.42', MW_VERSION, '<=' ) ) {
$emails = array_filter( array_unique( array_map(
$this->databaseBlockStore->newListFromConds( [ 'bt_user IS NOT NULL' ] ),
static fn( $block ) => User::newFromIdentity( $block->getBlocker() )->getEmail()
) ) );
} else {
$dbr = $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
$emails = $dbr->newSelectQueryBuilder()
->fields( [ 'user_email' ] )
->tables( [ 'ipblocks' ] )
->join( 'user', 'user_id = ipb_user' )
->conds( [
'ipb_user IS NOT NULL',
"ipb_expiry > " . $dbr->addQuotes( $dbr->timestamp() ),
] )
->fetchFieldValues();
$emails = array_filter(array_unique($emails));
}
if ( in_array( $addr, $emails ) ) {
return false;
}
}
}

0 comments on commit 0770bee

Please sign in to comment.