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    private User $performer;
39    private PermissionManager $permManager;
40    private Title $title;
41    private string $result;
42    private bool $new;
43
44    /**
45     * @param User $performer
46     * @param PermissionManager $permManager
47     * @param Title $title
48     * @param bool $new
49     */
50    public function __construct(
51        User $performer,
52        PermissionManager $permManager,
53        Title $title,
54        bool $new
55    ) {
56        $this->performer = $performer;
57        $this->permManager = $permManager;
58        $this->title = $title;
59        $this->new = $new;
60    }
61
62    public function checkConstraint(): string {
63        if ( $this->new ) {
64            // Check isn't simple enough to just repeat when getting the status
65            if ( !$this->performer->authorizeWrite( 'create', $this->title ) ) {
66                $this->result = (string)self::AS_NO_CREATE_PERMISSION;
67                return self::CONSTRAINT_FAILED;
68            }
69        }
70
71        // Check isn't simple enough to just repeat when getting the status
72        // Prior to 1.41 this checked if the user had edit rights in general
73        // instead of for the specific page in question.
74        if ( !$this->permManager->userCan(
75            'edit',
76            $this->performer,
77            $this->title
78        ) ) {
79            $this->result = self::CONSTRAINT_FAILED;
80            return self::CONSTRAINT_FAILED;
81        }
82
83        $this->result = self::CONSTRAINT_PASSED;
84        return self::CONSTRAINT_PASSED;
85    }
86
87    public function getLegacyStatus(): StatusValue {
88        $statusValue = StatusValue::newGood();
89
90        if ( $this->result === self::CONSTRAINT_FAILED ) {
91            if ( !$this->performer->isRegistered() ) {
92                $statusValue->setResult( false, self::AS_READ_ONLY_PAGE_ANON );
93            } else {
94                $statusValue->fatal( 'readonlytext' );
95                $statusValue->value = self::AS_READ_ONLY_PAGE_LOGGED;
96            }
97        } elseif ( $this->result === (string)self::AS_NO_CREATE_PERMISSION ) {
98            $statusValue->fatal( 'nocreatetext' );
99            $statusValue->value = self::AS_NO_CREATE_PERMISSION;
100        }
101
102        return $statusValue;
103    }
104
105}