jenkins-bot has submitted this change and it was merged.

Change subject: Avoid duplicate key error on /authorize page
......................................................................


Avoid duplicate key error on /authorize page

bug: 55580
Change-Id: Icfd366a9c8a6c4e33ec252dcef6e99287d49a12f
---
M backend/MWOAuthServer.php
M frontend/specialpages/SpecialMWOAuth.php
2 files changed, 18 insertions(+), 8 deletions(-)

Approvals:
  CSteipp: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/backend/MWOAuthServer.php b/backend/MWOAuthServer.php
index d5337e1..1293cdc 100644
--- a/backend/MWOAuthServer.php
+++ b/backend/MWOAuthServer.php
@@ -153,7 +153,7 @@
                $dbw = MWOAuthUtils::getCentralDB( DB_MASTER );
 
                // Check if this authorization exists
-               $cmra = $this->getCurrentAuthorization( $mwUser, $consumer );
+               $cmra = $this->getCurrentAuthorization( $mwUser, $consumer, 
wfWikiId() );
 
                if ( $update ) {
                        // This should be an update to an existing authorization
@@ -193,16 +193,24 @@
        }
 
        /**
-        * Attempts to get an authorization by this user, for this consumer. 
First attempts
-        * to fine an acceptance for the current wiki, when for '*' wikis. In 
theory, a user
-        * could authorize different grants on a particular wiki vs. all wikis, 
for a given
-        * consumer.
+        * Attempts to find an authorization by this user for this consumer. 
Since a user can
+        * accept a consumer multiple times (once for "*" and once for each 
specific wiki),
+        * there can several access tokens per-wiki (with varying grants) for a 
consumer.
+        * This will choose the most wiki-specific access token. The precedence 
is:
+        * a) The acceptance for wiki X if the consumer is applicable only to 
wiki X
+        * b) The acceptance for wiki $wikiId (if the consumer is applicable to 
it)
+        * c) The acceptance for wikis "*" (all wikis)
+        *
+        * Users might want more grants on some wikis than on "*". Note that 
the reverse would not
+        * make sense, since the consumer could just use the "*" acceptance if 
it has more grants.
+        *
         * @param User $mwUser (local wiki user) User who may or may not have 
authorizations
         * @param MWOAuthConsumer $consumer
+        * @param string $wikiId
         * @throws MWOAuthException
         * @return MWOAuthConsumerAcceptance
         */
-       public function getCurrentAuthorization( User $mwUser, $consumer ) {
+       public function getCurrentAuthorization( User $mwUser, $consumer, 
$wikiId ) {
                $dbr = MWOAuthUtils::getCentralDB( DB_SLAVE );
 
                $centralUserId = MWOAuthUtils::getCentralIdFromLocalUser( 
$mwUser );
@@ -210,11 +218,13 @@
                        throw new MWOAuthException( 
'mwoauthserver-invalid-user' );
                }
 
+               $checkWiki = $consumer->get( 'wiki' ) !== '*' ? $consumer->get( 
'wiki' ) : $wikiId;
+
                $cmra = MWOAuthConsumerAcceptance::newFromUserConsumerWiki(
                        $dbr,
                        $centralUserId,
                        $consumer,
-                       wfWikiID()
+                       $checkWiki
                );
                if ( !$cmra ) {
                        $cmra = 
MWOAuthConsumerAcceptance::newFromUserConsumerWiki(
diff --git a/frontend/specialpages/SpecialMWOAuth.php 
b/frontend/specialpages/SpecialMWOAuth.php
index 3faf390..6ddf3c4 100644
--- a/frontend/specialpages/SpecialMWOAuth.php
+++ b/frontend/specialpages/SpecialMWOAuth.php
@@ -160,7 +160,7 @@
                $this->getOutput()->addModules( 'ext.MWOAuth.AuthorizeDialog' );
 
                // Check if this user has authorized grants for this consumer 
previously
-               $existing = $oauthServer->getCurrentAuthorization( $user, 
$cmr->getDAO() );
+               $existing = $oauthServer->getCurrentAuthorization( $user, 
$cmr->getDAO(), wfWikiId() );
 
                $control = new MWOAuthConsumerAcceptanceSubmitControl( 
$this->getContext(), array(), $dbr );
                $form = new HTMLForm(

-- 
To view, visit https://gerrit.wikimedia.org/r/89107
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Icfd366a9c8a6c4e33ec252dcef6e99287d49a12f
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/OAuth
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <[email protected]>
Gerrit-Reviewer: Aaron Schulz <[email protected]>
Gerrit-Reviewer: Anomie <[email protected]>
Gerrit-Reviewer: CSteipp <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to