Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.24% covered (success)
95.24%
20 / 21
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AuthorizationConstraint
95.24% covered (success)
95.24%
20 / 21
66.67% covered (warning)
66.67%
2 / 3
11
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 checkConstraint
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 getLegacyStatus
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
6.02
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\EditPage\Constraint;
22
23use MediaWiki\Page\PageIdentity;
24use MediaWiki\Permissions\Authority;
25use MediaWiki\Permissions\PermissionStatus;
26use StatusValue;
27
28/**
29 * Verify authorization to edit the page (user rights, rate limits, blocks).
30 *
31 * @since 1.44
32 * @internal
33 */
34class AuthorizationConstraint implements IEditConstraint {
35
36    private PermissionStatus $status;
37
38    private Authority $performer;
39    private PageIdentity $target;
40    private bool $new;
41
42    public function __construct(
43        Authority $performer,
44        PageIdentity $target,
45        bool $new
46    ) {
47        $this->performer = $performer;
48        $this->target = $target;
49        $this->new = $new;
50    }
51
52    public function checkConstraint(): string {
53        $this->status = PermissionStatus::newEmpty();
54
55        if ( $this->new && !$this->performer->authorizeWrite( 'create', $this->target, $this->status ) ) {
56            return self::CONSTRAINT_FAILED;
57        }
58
59        if ( !$this->performer->authorizeWrite( 'edit', $this->target, $this->status ) ) {
60            return self::CONSTRAINT_FAILED;
61        }
62
63        return self::CONSTRAINT_PASSED;
64    }
65
66    public function getLegacyStatus(): StatusValue {
67        $statusValue = StatusValue::newGood();
68
69        if ( !$this->status->isGood() ) {
70            // Report the most specific errors first
71            if ( $this->status->isBlocked() ) {
72                $statusValue->setResult( false, self::AS_BLOCKED_PAGE_FOR_USER );
73            } elseif ( $this->status->isRateLimitExceeded() ) {
74                $statusValue->setResult( false, self::AS_RATE_LIMITED );
75            } elseif ( $this->status->getPermission() === 'create' ) {
76                $statusValue->setResult( false, self::AS_NO_CREATE_PERMISSION );
77            } elseif ( !$this->performer->isRegistered() ) {
78                $statusValue->setResult( false, self::AS_READ_ONLY_PAGE_ANON );
79            } else {
80                $statusValue->setResult( false, self::AS_READ_ONLY_PAGE_LOGGED );
81            }
82        }
83
84        // TODO: Use error messages from the PermissionStatus ($this->status) here - T384399
85        return $statusValue;
86    }
87
88}