jenkins-bot has submitted this change and it was merged.
Change subject: Re-implemented missing Zero Portal Lua function
......................................................................
Re-implemented missing Zero Portal Lua function
* getAccountsForUser()
* getUsername()
* getVal( name, default )
* isAdmin()
* jsonDecode( value )
* jsonEncode( value, escapeHtml )
* wasPosted()
Change-Id: I8ad0f6253d34ba93b3f6aa3bcbfd42129680be7e
---
M includes/LuaLibrary.lua
M includes/LuaLibrary.php
2 files changed, 163 insertions(+), 0 deletions(-)
Approvals:
Dr0ptp4kt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/includes/LuaLibrary.lua b/includes/LuaLibrary.lua
index 04be097..62c4433 100644
--- a/includes/LuaLibrary.lua
+++ b/includes/LuaLibrary.lua
@@ -17,8 +17,41 @@
-- Each of the following functions should be documented in the corresponding
LuaLibrary.php function
+function p.getAllowedAccountIds()
+ return php.getAllowedAccountIds()
+end
+
+function p.getUsername()
+ return php.getUsername()
+end
+
+function p.getRawParameter( name, default )
+ return php.getRawParameter( name, default )
+end
+
+function p.isAllowedToSee( xcs )
+ return php.isAllowedToSee( xcs )
+end
+
+function p.isSiteAdmin()
+ return php.isSiteAdmin()
+end
+
+function p.jsonDecode( value )
+ return php.jsonDecode( value )
+end
+
+function p.jsonEncode( value, escapeHtml )
+ return php.jsonEncode( value, escapeHtml )
+end
+
function p.setRawResult( result )
return php.setRawResult( result )
end
+function p.wasPosted()
+ return php.wasPosted()
+end
+
+
return p
diff --git a/includes/LuaLibrary.php b/includes/LuaLibrary.php
index d60ab96..116b312 100644
--- a/includes/LuaLibrary.php
+++ b/includes/LuaLibrary.php
@@ -2,9 +2,13 @@
namespace ZeroPortal;
+use FormatJson;
+use JsonConfig\JCSingleton;
use Scribunto_LuaError;
use Scribunto_LuaLibraryBase;
use IContextSource;
+use TitleValue;
+use ZeroBanner\ZeroConfig;
class LuaLibrary extends Scribunto_LuaLibraryBase {
@@ -81,13 +85,130 @@
if ( $title && $title->inNamespace( NS_SPECIAL ) &&
$title->getText() === 'ZeroPortal' ) {
$functions = array();
foreach ( array(
+ 'getAllowedAccountIds',
+ 'getUsername',
+ 'getRawParameter',
+ 'isAllowedToSee',
+ 'isSiteAdmin',
+ 'jsonDecode',
+ 'jsonEncode',
'setRawResult',
+ 'wasPosted',
) as $f ) {
$functions[$f] = array( $this, $f );
}
$moduleFileName = __DIR__ . DIRECTORY_SEPARATOR .
'LuaLibrary.lua';
$this->getEngine()->registerInterface( $moduleFileName,
$functions, array() );
}
+ }
+
+ /**
+ * Returns a list of Zero config titles (XCS) that this user has been
assigned to as an admin
+ * @return string[]|false[]
+ */
+ public function getAllowedAccountIds() {
+ $configs = array();
+ list( $name ) = $this->getUsername();
+ if ( $name ) {
+ ApiZeroPortal::iterateAllConfigs( false,
+ function ( ZeroConfig $content, $title ) use (
$name, &$configs ) {
+ if ( $name === true || in_array( $name,
$content->admins() ) ) {
+ $configs[] = $title;
+ }
+ return false;
+ } );
+ }
+ return array( $configs );
+ }
+
+ /**
+ * Return name of the logged in user, or false if anonymous
+ * @return string[]|bool[]
+ * @throws Scribunto_LuaError
+ */
+ public function getUsername() {
+ $user = self::getContext()->getUser();
+ if ( $user->isAnon() ) {
+ return array( false );
+ } else {
+ return array( $user->getName() );
+ }
+ }
+
+ /**
+ * Get a value from the request (GET or POST). Do not echo this value,
as it gets sent from the client
+ * @param string $name
+ * @param mixed $default
+ * @return mixed[]
+ * @throws Scribunto_LuaError
+ */
+ public function getRawParameter( $name = null, $default = null ) {
+ $funcName = self::luaNamespace . __FUNCTION__;
+ $this->checkType( $funcName, 1, $name, 'string' );
+ if ( substr( $name, 0, 2 ) !== 'zp' ) {
+ throw new Scribunto_LuaError( "bad argument #1 to
'$funcName' (name must start with 'zp')" );
+ }
+ return array( self::getContext()->getRequest()->getVal( $name,
$default ) );
+ }
+
+ /**
+ * Return true if current user has the right to view data for a
specific XCS
+ * @param string $xcs
+ * @throws Scribunto_LuaError
+ * @return bool[]
+ */
+ public function isAllowedToSee( $xcs = null ) {
+ // First validate parameter, than analyze
+ $this->checkType( self::luaNamespace . __FUNCTION__, 1, $xcs,
'string' );
+ $title = new TitleValue( NS_ZERO, $xcs );
+ if ( $this->isSiteAdmin() ) {
+ return true;
+ }
+ list( $name ) = $this->getUsername();
+ if ( !$name ) {
+ return false;
+ }
+ /** @var ZeroConfig $content */
+ $content = JCSingleton::getContent( $title );
+ return in_array( $name, $content->admins() );
+ }
+
+ /**
+ * Return true if this is an admin user (anyone belonging to the
zero-edit group)
+ * @return bool[]
+ * @throws Scribunto_LuaError
+ */
+ public function isSiteAdmin() {
+ return array( self::getContext()->getUser()->isAllowed(
'zero-edit' ) );
+ }
+
+ /**
+ * Decode value as a JSON string.
+ * NOTE: this function uses FormatJson::decode() for simplicity sake.
+ * TODO: remove this function once Scribunto gets its own json
encode/decode
+ * @param mixed $value
+ * @throws Scribunto_LuaError
+ * @return mixed[]
+ */
+ public function jsonDecode( $value = null ) {
+ $this->checkType( self::luaNamespace . __FUNCTION__, 1, $value,
'string' );
+ return array( FormatJson::decode( $value, true ) );
+ }
+
+ /**
+ * Encode value as a JSON string.
+ * TODO: remove this function once Scribunto gets its own json
encode/decode
+ * @param mixed $value
+ * @throws Scribunto_LuaError
+ * @return string
+ */
+ public function jsonEncode( $value = null ) {
+ $this->checkTypeOptional( self::luaNamespace . __FUNCTION__, 2,
$escapeHtml, 'boolean', false );
+ $result = FormatJson::encode( $value, false, FormatJson::ALL_OK
);
+ if ( $result === false ) {
+ throw new Scribunto_LuaError( 'Unable to encode value'
);
+ }
+ return array( $result );
}
/**
@@ -100,4 +221,13 @@
self::$result = $result;
}
+ /**
+ * Returns true if the present request was reached by a POST operation,
false otherwise
+ * @return bool[]
+ * @throws Scribunto_LuaError
+ */
+ public function wasPosted() {
+ return array( self::getContext()->getRequest()->wasPosted() );
+ }
+
}
--
To view, visit https://gerrit.wikimedia.org/r/171472
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I8ad0f6253d34ba93b3f6aa3bcbfd42129680be7e
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/ZeroPortal
Gerrit-Branch: master
Gerrit-Owner: Yurik <[email protected]>
Gerrit-Reviewer: Dr0ptp4kt <[email protected]>
Gerrit-Reviewer: Jackmcbarn <[email protected]>
Gerrit-Reviewer: Jhobs <[email protected]>
Gerrit-Reviewer: Yurik <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits