Foxtrott has uploaded a new change for review. https://gerrit.wikimedia.org/r/173525
Change subject: Refactor PermissionsHelper.php ...................................................................... Refactor PermissionsHelper.php Change-Id: I0aed1ef22cca1a92a2d45f9872a843dc61fe01ae --- M src/PermissionsHelper.php 1 file changed, 73 insertions(+), 47 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/skins/chameleon refs/changes/25/173525/1 diff --git a/src/PermissionsHelper.php b/src/PermissionsHelper.php index 9d8c023..8a5c7f4 100644 --- a/src/PermissionsHelper.php +++ b/src/PermissionsHelper.php @@ -21,18 +21,18 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. * * @file - * @ingroup Skins + * @ingroup Skins */ namespace Skins\Chameleon; + use DOMElement; -use Skin; /** * PermissionsHelper class * - * @author Stephan Gambke - * @since 1.1 + * @author Stephan Gambke + * @since 1.1 * @ingroup Skins */ class PermissionsHelper { @@ -43,84 +43,110 @@ /** * @param \SkinChameleon $skin - * @param DOMElement $domElement - * @param bool $default + * @param DOMElement $domElement + * @param bool $default */ - public function __construct ( \SkinChameleon $skin, DOMElement $domElement = null, $default = false ){ + public function __construct( \SkinChameleon $skin, DOMElement $domElement = null, $default = false ) { $this->skin = $skin; $this->domElement = $domElement; $this->default = $default; } /** - * @param string $attribute + * @since 1.1 + * + * @param string $attributeNameInDomElement + * * @return bool */ - public function userHasGroup( $attribute ) { + public function userHasGroup( $attributeNameInDomElement ) { - if ( !$this->hasAttribute( $attribute ) ) { - return $this->default; - } - - $expectedGroups = $this->getValueListFromAttribute( $attribute ); - $userGroups = $this->skin->getUser()->getEffectiveGroups(); - $effectiveUserGroups = array_intersect( $expectedGroups, $userGroups ); - - return !empty( $effectiveUserGroups ); + return $this->userHas( 'group', $attributeNameInDomElement ); } /** - * @param string $attribute + * @param string $attributeOfUser + * @param string $attributeNameInDomElement + * + * @throws \MWException * @return bool */ - public function userHasPermission( $attribute ) { + protected function userHas( $attributeOfUser, $attributeNameInDomElement ) { - if ( !$this->hasAttribute( $attribute ) ) { + $user = $this->skin->getUser(); + $attributeAccessors = array( + 'group' => array( $user, 'getEffectiveGroups' ), + 'permission' => array( $user, 'getRights' ), + ); + + if ( !array_key_exists( $attributeOfUser, $attributeAccessors ) ) { + throw new \MWException( sprintf( 'Unknown permission: %s', $attributeOfUser ) ); + } + + if ( !$this->hasAttribute( $attributeNameInDomElement ) ) { return $this->default; } - $expectedRights = $this->getValueListFromAttribute( $attribute ); - $userRights = $this->skin->getUser()->getRights(); - $effectiveUserRights = array_intersect( $expectedRights, $userRights ); + $expectedValues = $this->getValueListFromAttribute( $attributeNameInDomElement ); + $observedValues = call_user_func( $attributeAccessors[ $attributeOfUser ] ); + $effectiveValues = array_intersect( $expectedValues, $observedValues ); - return !empty( $effectiveUserRights ); + return !empty( $effectiveValues ); } /** - * @param string $attribute + * @param string $attributeNameInDomElement + * * @return bool */ - public function pageIsInNamespace( $attribute ) { + public function hasAttribute( $attributeNameInDomElement ) { + return $this->domElement !== null && $this->domElement->hasAttribute( $attributeNameInDomElement ); + } - if ( !$this->hasAttribute( $attribute ) ) { + /** + * @param string $attributeName + * + * @return string[] + */ + private function getValueListFromAttribute( $attributeName ) { + return $this->domElement === null ? array() : array_map( 'trim', explode( ',', $this->domElement->getAttribute( $attributeName ) ) ); + + } + + /** + * @since 1.1 + * + * @param string $attributeNameInDomElement + * + * @return bool + */ + public function userHasPermission( $attributeNameInDomElement ) { + + return $this->userHas( 'permission', $attributeNameInDomElement ); + } + + /** + * @since 1.1 + * + * @param string $attributeNameInDomElement + * + * @return bool + */ + public function pageIsInNamespace( $attributeNameInDomElement ) { + + if ( !$this->hasAttribute( $attributeNameInDomElement ) ) { return $this->default; } - $expectedNamespaces = array_map( array($this, 'getNamespaceNumberFromDefinedConstantName' ), $this->getValueListFromAttribute( $attribute ) ); + $expectedNamespaces = array_map( array( $this, 'getNamespaceNumberFromDefinedConstantName' ), $this->getValueListFromAttribute( $attributeNameInDomElement ) ); $pageNamespace = $this->skin->getTitle()->getNamespace(); return in_array( $pageNamespace, $expectedNamespaces ); } /** - * @param string $attributeName - * @return bool - */ - public function hasAttribute( $attributeName ) { - return $this->domElement !== null && $this->domElement->hasAttribute( $attributeName ); - } - - /** - * @param string $attributeName - * @return string[] - */ - private function getValueListFromAttribute( $attributeName ) { - return $this->domElement === null ? array () : array_map( 'trim', explode( ',', $this->domElement->getAttribute( $attributeName ) ) ); - - } - - /** - * @param $value + * @param null|string $value + * * @return int */ private function getNamespaceNumberFromDefinedConstantName( $value ) { -- To view, visit https://gerrit.wikimedia.org/r/173525 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I0aed1ef22cca1a92a2d45f9872a843dc61fe01ae Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/skins/chameleon Gerrit-Branch: master Gerrit-Owner: Foxtrott <s7ep...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits