Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
EditRightConstraint
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 checkConstraint
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
4
 getLegacyStatus
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
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\Permissions\PermissionManager;
24use MediaWiki\Title\Title;
25use MediaWiki\User\User;
26use StatusValue;
27
28/**
29 * Verify user permissions:
30 *    Must have edit rights
31 *
32 * @since 1.36
33 * @internal
34 * @author DannyS712
35 */
36class EditRightConstraint implements IEditConstraint {
37
38    /** @var User */
39    private $performer;
40
41    /** @var PermissionManager */
42    private $permManager;
43
44    /** @var Title */
45    private $title;
46
47    /** @var string|null */
48    private $result;
49
50    /** @var bool */
51    private $new;
52
53    /**
54     * @param User $performer
55     * @param PermissionManager $permManager
56     * @param Title $title
57     * @param bool $new
58     */
59    public function __construct(
60        User $performer,
61        PermissionManager $permManager,
62        Title $title,
63        bool $new
64    ) {
65        $this->performer = $performer;
66        $this->permManager = $permManager;
67        $this->title = $title;
68        $this->new = $new;
69    }
70
71    public function checkConstraint(): string {
72        if ( $this->new ) {
73            // Check isn't simple enough to just repeat when getting the status
74            if ( !$this->performer->authorizeWrite( 'create', $this->title ) ) {
75                $this->result = (string)self::AS_NO_CREATE_PERMISSION;
76                return self::CONSTRAINT_FAILED;
77            }
78        }
79
80        // Check isn't simple enough to just repeat when getting the status
81        // Prior to 1.41 this checked if the user had edit rights in general
82        // instead of for the specific page in question.
83        if ( $this->permManager->getPermissionErrors(
84            'edit',
85            $this->performer,
86            $this->title
87        ) ) {
88            $this->result = self::CONSTRAINT_FAILED;
89            return self::CONSTRAINT_FAILED;
90        }
91
92        $this->result = self::CONSTRAINT_PASSED;
93        return self::CONSTRAINT_PASSED;
94    }
95
96    public function getLegacyStatus(): StatusValue {
97        $statusValue = StatusValue::newGood();
98
99        if ( $this->result === self::CONSTRAINT_FAILED ) {
100            if ( !$this->performer->isRegistered() ) {
101                $statusValue->setResult( false, self::AS_READ_ONLY_PAGE_ANON );
102            } else {
103                $statusValue->fatal( 'readonlytext' );
104                $statusValue->value = self::AS_READ_ONLY_PAGE_LOGGED;
105            }
106        } elseif ( $this->result === (string)self::AS_NO_CREATE_PERMISSION ) {
107            $statusValue->fatal( 'nocreatetext' );
108            $statusValue->value = self::AS_NO_CREATE_PERMISSION;
109        }
110
111        return $statusValue;
112    }
113
114}