summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/PermissionPthValidator.php
blob: f8a5e005ab79b61e0f733c5153d735f7b6848a5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php

namespace SMW;

use SMW\DataValues\AllowsPatternValue;
use SMW\Protection\ProtectionValidator;
use Title;
use User;

/**
 * @license GNU GPL v2+
 * @since 2.4
 *
 * @author mwjames
 */
class PermissionPthValidator {

	/**
	 * @var ProtectionValidator
	 */
	private $protectionValidator;

	/**
	 * @since 2.5
	 *
	 * @param ProtectionValidator $protectionValidator
	 */
	public function __construct( ProtectionValidator $protectionValidator ) {
		$this->protectionValidator = $protectionValidator;
	}

	/**
	 * @since 2.5
	 *
	 * @param Title &$title
	 * @param User $user
	 * @param string $action
	 * @param array &$errors
	 *
	 * @return boolean
	 */
	public function checkQuickPermission( Title &$title, User $user, $action, &$errors ) {
		return $this->hasUserPermission( $title, $user, $action, $errors );
	}

	/**
	 * @since 2.4
	 *
	 * @param Title &$title
	 * @param User $user
	 * @param string $action
	 * @param array &$errors
	 *
	 * @return boolean
	 */
	public function hasUserPermission( Title &$title, User $user, $action, &$errors ) {

		if ( $title->getNamespace() === SMW_NS_SCHEMA ) {
			return $this->checkSchemaNamespacePermission( $title, $user, $action, $errors );
		}

		if ( $action !== 'edit' && $action !== 'delete' && $action !== 'move' && $action !== 'upload' ) {
			return true;
		}

		if ( $title->getNamespace() === NS_MEDIAWIKI ) {
			return $this->checkMwNamespacePatternEditPermission( $title, $user, $action, $errors );
		}

		if ( $this->protectionValidator->getCreateProtectionRight() && $title->getNamespace() === SMW_NS_PROPERTY ) {
			return $this->checkPropertyNamespaceCreatePermission( $title, $user, $action, $errors );
		}

		if ( $title->getNamespace() === NS_CATEGORY ) {
			return $this->checkChangePropagationProtection( $title, $user, $action, $errors );
		}

		if ( !$title->exists() ) {
			return true;
		}

		if ( $title->getNamespace() === SMW_NS_PROPERTY ) {
			return $this->checkPropertyNamespaceEditPermission( $title, $user, $action, $errors );
		}

		if ( $this->protectionValidator->hasEditProtectionOnNamespace( $title ) ) {
			return $this->checkEditPermissionOn( $title, $user, $action, $errors );
		}

		return true;
	}

	private function checkMwNamespacePatternEditPermission( Title &$title, User $user, $action, &$errors ) {

		// @see https://www.semantic-mediawiki.org/wiki/Help:Special_property_Allows_pattern
		if ( $title->getDBKey() !== AllowsPatternValue::REFERENCE_PAGE_ID || $user->isAllowed( 'smw-patternedit' ) ) {
			return true;
		}

		$errors[] = [ 'smw-patternedit-protection', 'smw-patternedit' ];

		return false;
	}

	private function checkSchemaNamespacePermission( Title &$title, User $user, $action, &$errors ) {

		if ( !$user->isAllowed( 'smw-schemaedit' ) ) {
			$errors[] = [ 'smw-schema-namespace-edit-protection', 'smw-schemaedit' ];
			return false;
		}

		// Disallow to change the content model
		if ( $action === 'editcontentmodel' ) {
			$errors[] = [ 'smw-schema-namespace-editcontentmodel-disallowed' ];
			return false;
		}

		return true;
	}

	private function checkPropertyNamespaceCreatePermission( Title &$title, User $user, $action, &$errors ) {

		$createProtectionRight = $this->protectionValidator->getCreateProtectionRight();

		if ( $user->isAllowed( $createProtectionRight ) ) {
			return $this->checkPropertyNamespaceEditPermission( $title, $user, $action, $errors );;
		}

		$msg = 'smw-create-protection';

		if ( $title->exists() ) {
			$msg = 'smw-create-protection-exists';
		}

		$errors[] = [ $msg, $title->getText(), $createProtectionRight ];

		return false;
	}

	private function checkPropertyNamespaceEditPermission( Title &$title, User $user, $action, &$errors ) {

		// This renders full protection until the ChangePropagationDispatchJob was run
		if ( !$this->protectionValidator->hasChangePropagationProtection( $title ) ) {
			return $this->checkEditPermissionOn( $title, $user, $action, $errors );
		}

		$errors[] = [ 'smw-change-propagation-protection' ];

		return false;
	}

	private function checkChangePropagationProtection( Title &$title, User $user, $action, &$errors ) {

		// This renders full protection until the ChangePropagationDispatchJob was run
		if ( !$this->protectionValidator->hasChangePropagationProtection( $title ) ) {
			return true;
		}

		$errors[] = [ 'smw-change-propagation-protection' ];

		return false;
	}

	private function checkEditPermissionOn( Title &$title, User $user, $action, &$errors ) {

		$editProtectionRight = $this->protectionValidator->getEditProtectionRight();

		// @see https://www.semantic-mediawiki.org/wiki/Help:Special_property_Is_edit_protected
		if ( !$this->protectionValidator->hasProtection( $title ) || $user->isAllowed( $editProtectionRight ) ) {
			return true;
		}

		$errors[] = [ 'smw-edit-protection', $editProtectionRight ];

		return false;
	}

}