Refactor AbuseFilterView::canEdit* functions

Don't use global state in here, centralize
the logic for global filters and avoid static
functions.

This also makes the UI for global filters nicer
in case the user can't edit them (as all fields
are disabled then).

Change-Id: Ica4e77536d315d8ef39a45666c6b8834315bee77
This commit is contained in:
Marius Hoch 2013-07-09 15:35:06 +02:00 committed by CSteipp
parent bc20e22752
commit 5ed5230a49
2 changed files with 23 additions and 27 deletions

View file

@ -22,33 +22,31 @@ abstract class AbuseFilterView extends ContextSource {
abstract function show();
/**
* @static
* @return bool
*/
static function canEdit() {
global $wgUser;
static $canEdit = null;
if ( is_null( $canEdit ) ) {
$canEdit = $wgUser->isAllowed( 'abusefilter-modify' );
}
return $canEdit;
public function canEdit() {
return $this->getUser()->isAllowed( 'abusefilter-modify' );
}
/**
* @static
* @return bool
*/
static function canEditGlobal() {
global $wgUser;
static $canEditGlobal = null;
public function canEditGlobal() {
return $this->getUser()->isAllowed( 'abusefilter-modify-global' );
}
if ( is_null( $canEditGlobal ) ) {
$canEditGlobal = $wgUser->isAllowed( 'abusefilter-modify-global' );
}
return $canEditGlobal;
/**
* Whether the user can edit the given filter.
*
* @param object $row Filter row
*
* @return bool
*/
public function canEditFilter( $row ) {
return (
$this->canEdit() &&
!( isset( $row->af_global ) && $row->af_global == 1 && !$this->canEditGlobal() )
);
}
/**
@ -60,7 +58,7 @@ abstract class AbuseFilterView extends ContextSource {
static $canView = null;
if ( is_null( $canView ) ) {
$canView = self::canEdit() || $wgUser->isAllowed( 'abusefilter-view-private' );
$canView = $wgUser->isAllowedAny( 'abusefilter-modify', 'abusefilter-view-private' );
}
return $canView;

View file

@ -59,9 +59,7 @@ class AbuseFilterViewEdit extends AbuseFilterView {
// Don't allow adding a new global rule, or updating a
// rule that is currently global, without permissions.
if ( ( $newRow->af_global == 1 || $newRow->mOriginalRow->af_global == 1 )
&& !$this->canEditGlobal()
) {
if ( !$this->canEditFilter( $newRow ) || !$this->canEditFilter( $newRow->mOriginalRow ) ) {
$out->addWikiMsg( 'abusefilter-edit-notallowed-global' );
return;
}
@ -335,7 +333,7 @@ class AbuseFilterViewEdit extends AbuseFilterView {
$readOnlyAttrib = array();
$cbReadOnlyAttrib = array(); // For checkboxes
if ( !$this->canEdit() || ( isset( $row->af_global ) && $row->af_global == 1 && !$this->canEditGlobal() ) ) {
if ( !$this->canEditFilter( $row ) ) {
$readOnlyAttrib['readonly'] = 'readonly';
$cbReadOnlyAttrib['disabled'] = 'disabled';
}
@ -405,7 +403,7 @@ class AbuseFilterViewEdit extends AbuseFilterView {
$row->af_pattern,
'wpFilterRules',
true,
$this->canEdit()
$this->canEditFilter( $row )
);
$fields['abusefilter-edit-notes'] = Xml::textarea(
'wpFilterNotes',
@ -519,7 +517,7 @@ class AbuseFilterViewEdit extends AbuseFilterView {
$this->buildConsequenceEditor( $row, $actions )
);
if ( $this->canEdit() ) {
if ( $this->canEditFilter( $row ) ) {
$form .= Xml::submitButton(
$this->msg( 'abusefilter-edit-save' )->text(),
array( 'accesskey' => 's' )
@ -585,7 +583,7 @@ class AbuseFilterViewEdit extends AbuseFilterView {
$readOnlyAttrib = array();
$cbReadOnlyAttrib = array(); // For checkboxes
if ( !$this->canEdit() ) {
if ( !$this->canEditFilter( $row ) ) {
$readOnlyAttrib['readonly'] = 'readonly';
$cbReadOnlyAttrib['disabled'] = 'disabled';
}