Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
EchoOAuthStageChangePresentationModel
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 12
462
0.00% covered (danger)
0.00%
0 / 1
 getDefinition
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 canRender
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getHeaderMessage
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getSubjectMessage
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getBodyMessage
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getIconType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPrimaryLink
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 getSecondaryLinks
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getConsumer
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getOwner
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getConsumerName
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getConsumerStage
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\OAuth\Frontend;
4
5use MediaWiki\Extension\Notifications\AttributeManager;
6use MediaWiki\Extension\Notifications\Formatters\EchoEventPresentationModel;
7use MediaWiki\Extension\OAuth\Backend\Consumer;
8use MediaWiki\Extension\OAuth\Backend\Utils;
9use MediaWiki\SpecialPage\SpecialPage;
10use MediaWiki\User\User;
11use MWException;
12
13class EchoOAuthStageChangePresentationModel extends EchoEventPresentationModel {
14    /** @var User[] OAuth admins who should be notified about additions to the review queue */
15    protected static $oauthAdmins;
16
17    /** @var Consumer|false */
18    protected $consumer;
19
20    /** @var User|false The owner of the OAuth consumer */
21    protected $owner;
22
23    /**
24     * Helper function for $wgEchoNotifications
25     * @param string $action One of the actions from ConsumerSubmitControl::$actions
26     * @return array
27     */
28    public static function getDefinition( $action ) {
29        if ( $action === 'propose' ) {
30            // notify admins
31            $category = 'oauth-admin';
32        } else {
33            // notify owner
34            $category = 'oauth-owner';
35        }
36
37        return [
38            AttributeManager::ATTR_LOCATORS => [
39                [ [ Utils::class, 'locateUsersToNotify' ] ]
40            ],
41            'category' => $category,
42            'presentation-model' => self::class,
43            'icon' => 'oauth',
44        ];
45    }
46
47    /**
48     * Hide notifications that were already handled by another OAuth admin
49     * @inheritDoc
50     */
51    public function canRender() {
52        $action = $this->event->getExtraParam( 'action' );
53        return !( $action === 'propose' && $this->getConsumerStage() !== Consumer::STAGE_PROPOSED );
54    }
55
56    public function getHeaderMessage() {
57        $action = $this->event->getExtraParam( 'action' );
58        return $this->msg( "notification-oauth-app-$action-title",
59            $this->event->getAgent(), $this->getConsumerName(), $this->getOwner() );
60    }
61
62    public function getSubjectMessage() {
63        $action = $this->event->getExtraParam( 'action' );
64        return $this->msg( "notification-oauth-app-$action-subject",
65            $this->event->getAgent(), $this->getConsumerName(), $this->getOwner() );
66    }
67
68    public function getBodyMessage() {
69        $comment = $this->event->getExtraParam( 'comment' );
70        return $comment ? $this->msg( 'notification-oauth-app-body', $comment ) : false;
71    }
72
73    public function getIconType() {
74        return 'oauth';
75    }
76
77    public function getPrimaryLink() {
78        $consumerKey = $this->event->getExtraParam( 'app-key' );
79        $action = $this->event->getExtraParam( 'action' );
80
81        if ( $action === 'propose' ) {
82            // show management interface
83            $page = SpecialPage::getSafeTitleFor( 'OAuthManageConsumers', $consumerKey );
84        } else {
85            // show public view
86            $page = SpecialPage::getSafeTitleFor( 'OAuthListConsumers', "view/$consumerKey" );
87        }
88        if ( $page === null ) {
89            throw new MWException( "Invalid app ID: $consumerKey" );
90        }
91
92        return [
93            'url' => $page->getLocalURL(),
94            'label' => $this->msg( "notification-oauth-app-$action-primary-link" )->text(),
95        ];
96    }
97
98    public function getSecondaryLinks() {
99        return [ $this->getAgentLink() ];
100    }
101
102    /**
103     * @return Consumer|false
104     */
105    protected function getConsumer() {
106        if ( $this->consumer === null ) {
107            $dbr = Utils::getCentralDB( DB_REPLICA );
108            $this->consumer =
109                Consumer::newFromKey( $dbr, $this->event->getExtraParam( 'app-key' ) );
110        }
111        return $this->consumer;
112    }
113
114    /**
115     * @return User|false
116     */
117    protected function getOwner() {
118        if ( $this->owner === null ) {
119            $this->owner = Utils::getLocalUserFromCentralId(
120                $this->event->getExtraParam( 'owner-id' ) );
121        }
122        return $this->owner;
123    }
124
125    /**
126     * @return string|false
127     */
128    protected function getConsumerName() {
129        $consumer = $this->getConsumer();
130        return $consumer ? $consumer->getName() : false;
131    }
132
133    /**
134     * @return int The stage, or -1 if the consumer doesn't exist
135     */
136    protected function getConsumerStage() {
137        $consumer = $this->getConsumer();
138        return $consumer ? $consumer->getStage() : -1;
139    }
140}