permissions::cannot_edit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2016 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\content\services;
11
12
class permissions
13
{
14
	/** @var \phpbb\auth\auth */
15
	protected $auth;
16
17
	/** @var \phpbb\config\db */
18
	protected $config;
19
20
	/** @var \phpbb\user */
21
	protected $user;
22
23
	/**
24
	 * Constructor
25
	 *
26
	 * @param \phpbb\auth\auth		$auth		Auth object
27
	 * @param \phpbb\config\db		$config		Config object
28
	 * @param \phpbb\user			$user		User object
29
	 */
30
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\db $config, \phpbb\user $user)
31
	{
32
		$this->auth = $auth;
33
		$this->config = $config;
34
		$this->user = $user;
35
	}
36
37
	/**
38
	 * @param array $post_data
39
	 * @return bool
40
	 */
41
	public function display_attachments_notice(array $post_data)
42
	{
43
		return (!$this->auth->acl_get('f_download', $post_data['forum_id']) && $post_data['post_attachment']);
44
	}
45
46
	/**
47
	 * @param array $post_data
48
	 * @return bool
49
	 */
50
	public function permanent_delete_allowed(array $post_data)
51
	{
52
		return (
53
			$this->auth->acl_get('m_delete', $post_data['forum_id']) ||
54
			($this->auth->acl_get('f_delete', $post_data['forum_id']) && $this->user_is_poster($post_data['poster_id']))
55
		);
56
	}
57
58
	/**
59
	 * @param int $poster_id
60
	 * @return bool
61
	 */
62
	public function user_is_poster($poster_id)
63
	{
64
		return ($poster_id == $this->user->data['user_id']);
65
	}
66
67
	/**
68
	 * @param int $forum_id
69
	 * @return bool
70
	 */
71
	public function can_report_post($forum_id)
72
	{
73
		return ($this->auth->acl_get('f_report', $forum_id));
74
	}
75
76
	/**
77
	 * @param array $topic_data
78
	 * @return bool
79
	 */
80
	public function topic_has_unapproved_posts(array $topic_data)
81
	{
82
		return ($topic_data['topic_visibility'] == ITEM_APPROVED && $topic_data['topic_posts_unapproved'] && $this->auth->acl_get('m_approve', $topic_data['forum_id']));
83
	}
84
85
	/**
86
	 * @param array $topic_data
87
	 * @return bool
88
	 */
89
	public function topic_is_reported(array $topic_data)
90
	{
91
		return ($topic_data['topic_reported'] && !$topic_data['topic_moved_id'] && $this->auth->acl_get('m_report', $topic_data['forum_id'])) ? true : false;
92
	}
93
94
	/**
95
	 * @param array $topic_data
96
	 * @return bool
97
	 */
98
	public function topic_is_locked(array $topic_data)
99
	{
100
		return ($topic_data['topic_status'] == ITEM_UNLOCKED && $topic_data['forum_status'] == ITEM_UNLOCKED) ? false : true;
101
	}
102
103
	/**
104
	 * @param array $post_data
105
	 * @return bool
106
	 */
107
	public function post_is_unapproved(array $post_data)
108
	{
109
		return (($post_data['post_visibility'] == ITEM_UNAPPROVED || $post_data['post_visibility'] == ITEM_REAPPROVE) && $this->auth->acl_get('m_approve', $post_data['forum_id'])) ? true : false;
110
	}
111
112
	/**
113
	 * @param array $post_data
114
	 * @param array $topic_data
115
	 * @return bool
116
	 */
117
	protected function edit_allowed(array $post_data, array $topic_data)
118
	{
119
		return $this->auth->acl_get('m_edit', $post_data['forum_id']) || !$this->user_cannot_modify_post($post_data, $topic_data, 'edit');
120
	}
121
122
	/**
123
	 * @param array $post_data
124
	 * @param array $topic_data
125
	 * @return bool
126
	 */
127
	protected function delete_allowed(array $post_data, array $topic_data)
128
	{
129
		return $this->moderator_can_delete($post_data, $topic_data) || (
130
			!$this->cannot_delete_lastpost($post_data, $topic_data) &&
131
			!$this->user_cannot_modify_post($post_data, $topic_data, 'delete')
132
		);
133
	}
134
135
	/**
136
	 * @param array $post_data
137
	 * @param array $topic_data
138
	 * @return bool
139
	 */
140
	protected function moderator_can_delete(array $post_data, array $topic_data)
141
	{
142
		return (
143
			$this->auth->acl_get('m_delete', $post_data['forum_id']) ||
144
			($this->auth->acl_get('m_softdelete', $post_data['forum_id']) && $post_data['post_visibility'] != ITEM_DELETED)
145
		);
146
	}
147
148
	/**
149
	 * @param array $post_data
150
	 * @param array $topic_data
151
	 * @param string $mode
152
	 * @return bool
153
	 */
154
	protected function user_cannot_modify_post(array $post_data, array $topic_data, $mode)
155
	{
156
		$callable = 'cannot_' . $mode;
157
		return $this->$callable($post_data)
158
			|| $this->cannot_modify_time($post_data, $mode)
159
			|| $this->cannot_modify_locked($post_data, $topic_data);
160
	}
161
162
	/**
163
	 * @param array $post_data
164
	 * @return bool
165
	 */
166
	protected function softdelete_allowed(array $post_data)
167
	{
168
		return (
169
			$this->auth->acl_get('m_softdelete', $post_data['forum_id']) ||
170
			($this->auth->acl_get('f_softdelete', $post_data['forum_id']) && $this->user_is_poster($post_data['poster_id']))
171
		) && $post_data['post_visibility'] != ITEM_DELETED;
172
	}
173
174
	/**
175
	 * @param array $post_data
176
	 * @param string $mode edit|delete
177
	 * @return bool
178
	 */
179
	protected function cannot_modify_time(array $post_data, $mode)
180
	{
181
		$mode .= '_time';
182
		return $this->config[$mode] && $post_data['post_time'] <= time() - ($this->config[$mode] * 60);
183
	}
184
185
	/**
186
	 * we do not want to allow removal of the last post if a moderator locked it!
187
	 * @param array $post_data
188
	 * @param array $topic_data
189
	 * @return bool
190
	 */
191
	protected function cannot_modify_locked(array $post_data, array $topic_data)
192
	{
193
		return $topic_data['topic_status'] == ITEM_LOCKED || $post_data['post_edit_locked'];
194
	}
195
196
	/**
197
	 * @param array $post_data
198
	 * @return bool
199
	 */
200
	protected function cannot_edit(array $post_data)
201
	{
202
		return !$this->user_is_poster($post_data['poster_id']) || !$this->auth->acl_get('f_edit', $post_data['forum_id']);
203
	}
204
205
	/**
206
	 * @param array $post_data
207
	 * @return bool
208
	 */
209
	protected function cannot_delete(array $post_data)
210
	{
211
		return !$this->user_is_poster($post_data['poster_id']) || (
212
			!$this->auth->acl_get('f_delete', $post_data['forum_id']) &&
213
			(!$this->auth->acl_get('f_softdelete', $post_data['forum_id']) || $post_data['post_visibility'] == ITEM_DELETED)
214
		);
215
	}
216
217
	/**
218
	 * @param array $post_data
219
	 * @param array $topic_data
220
	 * @return bool
221
	 */
222
	protected function cannot_delete_lastpost(array $post_data, array $topic_data)
223
	{
224
		return $topic_data['topic_last_post_id'] != $post_data['post_id'];
225
	}
226
227
	/**
228
	 * @param array $topic_data
229
	 * @return bool
230
	 */
231
	protected function quote_allowed(array $topic_data)
232
	{
233
		return $this->auth->acl_get('m_edit', $topic_data['forum_id']) || ($topic_data['topic_status'] != ITEM_LOCKED &&
234
			($this->user->data['user_id'] == ANONYMOUS || $this->auth->acl_get('f_reply', $topic_data['forum_id']))
235
		);
236
	}
237
238
	/**
239
	 * @param array $post_data
240
	 * @param array $topic_data
241
	 * @return bool
242
	 */
243
	protected function post_is_quotable(array $post_data, array $topic_data)
244
	{
245
		return ($post_data['post_visibility'] == ITEM_APPROVED && $topic_data['topic_first_post_id'] != $post_data['post_id']);
246
	}
247
}
248