Passed
Push — master ( aa725e...8615fe )
by Julius
03:34 queued 18s
created

BoardMapper::mapAcl()   B

Complexity

Conditions 8
Paths 1

Size

Total Lines 33

Duplication

Lines 16
Ratio 48.48 %

Importance

Changes 0
Metric Value
dl 16
loc 33
rs 8.1475
c 0
b 0
f 0
cc 8
nc 1
nop 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Deck\Db;
25
26
use OCP\AppFramework\Db\DoesNotExistException;
27
use OCP\AppFramework\QueryException;
28
use OCP\IDBConnection;
29
use OCP\IUserManager;
30
use OCP\IGroupManager;
31
32
class BoardMapper extends DeckMapper implements IPermissionMapper {
33
34
	private $labelMapper;
35
	private $aclMapper;
36
	private $stackMapper;
37
	private $userManager;
38
	private $groupManager;
39
40
	private $circlesEnabled;
41
42
	public function __construct(
43
		IDBConnection $db,
44
		LabelMapper $labelMapper,
45
		AclMapper $aclMapper,
46
		StackMapper $stackMapper,
47
		IUserManager $userManager,
48
		IGroupManager $groupManager
49
	) {
50
		parent::__construct($db, 'deck_boards', Board::class);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::__construct() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
51
		$this->labelMapper = $labelMapper;
52
		$this->aclMapper = $aclMapper;
53
		$this->stackMapper = $stackMapper;
54
		$this->userManager = $userManager;
55
		$this->groupManager = $groupManager;
56
57
		$this->circlesEnabled = \OC::$server->getAppManager()->isEnabledForUser('circles');
58
	}
59
60
61
	/**
62
	 * @param $id
63
	 * @param bool $withLabels
64
	 * @param bool $withAcl
65
	 * @return \OCP\AppFramework\Db\Entity
66
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
67
	 * @throws DoesNotExistException
68
	 */
69
	public function find($id, $withLabels = false, $withAcl = false) {
70
		$sql = 'SELECT id, title, owner, color, archived, deleted_at, last_modified FROM `*PREFIX*deck_boards` ' .
71
			'WHERE `id` = ?';
72
		$board = $this->findEntity($sql, [$id]);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::findEntity() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
73
74
		// Add labels
75
		if ($withLabels) {
76
			$labels = $this->labelMapper->findAll($id);
77
			$board->setLabels($labels);
78
		}
79
80
		// Add acl
81
		if ($withAcl) {
82
			$acl = $this->aclMapper->findAll($id);
83
			$board->setAcl($acl);
84
		}
85
86
		return $board;
87
	}
88
89
	/**
90
	 * Find all boards for a given user
91
	 *
92
	 * @param $userId
93
	 * @param null $limit
94
	 * @param null $offset
95
	 * @return array
96
	 */
97
	public function findAllByUser($userId, $limit = null, $offset = null, $since = 0) {
98
		$sql = 'SELECT id, title, owner, color, archived, deleted_at, 0 as shared, last_modified FROM `*PREFIX*deck_boards` WHERE owner = ? AND last_modified > ?';
99
		$entries = $this->findEntities($sql, [$userId, $since], $limit, $offset);
0 ignored issues
show
Unused Code introduced by
$entries is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::findEntities() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
100
101
		$sql = 'SELECT id, title, owner, color, archived, deleted_at, 0 as shared, last_modified FROM `*PREFIX*deck_boards` WHERE owner = ? AND last_modified > ? UNION ' .
102
			'SELECT boards.id, title, owner, color, archived, deleted_at, 1 as shared, last_modified FROM `*PREFIX*deck_boards` as boards ' .
103
			'JOIN `*PREFIX*deck_board_acl` as acl ON boards.id=acl.board_id WHERE acl.participant=? AND acl.type=? AND boards.owner != ? AND last_modified > ?';
104
		$entries = $this->findEntities($sql, [$userId, $since, $userId, Acl::PERMISSION_TYPE_USER, $userId, $since], $limit, $offset);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::findEntities() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
105
		/* @var Board $entry */
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
106
		foreach ($entries as $entry) {
107
			$acl = $this->aclMapper->findAll($entry->id);
108
			$entry->setAcl($acl);
109
		}
110
		return $entries;
111
	}
112
113
	/**
114
	 * Find all boards for a given user
115
	 *
116
	 * @param $userId
117
	 * @param $groups
118
	 * @param null $limit
119
	 * @param null $offset
120
	 * @return array
121
	 */
122
	public function findAllByGroups($userId, $groups, $limit = null, $offset = null) {
123
		if (count($groups) <= 0) {
124
			return [];
125
		}
126
		$sql = 'SELECT boards.id, title, owner, color, archived, deleted_at, 2 as shared, last_modified FROM `*PREFIX*deck_boards` as boards ' .
127
			'INNER JOIN `*PREFIX*deck_board_acl` as acl ON boards.id=acl.board_id WHERE owner != ? AND type=? AND (';
128 View Code Duplication
		for ($i = 0, $iMax = count($groups); $i < $iMax; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
			$sql .= 'acl.participant = ? ';
130
			if (count($groups) > 1 && $i < count($groups) - 1) {
131
				$sql .= ' OR ';
132
			}
133
		}
134
		$sql .= ');';
135
		$entries = $this->findEntities($sql, array_merge([$userId, Acl::PERMISSION_TYPE_GROUP], $groups), $limit, $offset);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::findEntities() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
136
		/* @var Board $entry */
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
137
		foreach ($entries as $entry) {
138
			$acl = $this->aclMapper->findAll($entry->id);
139
			$entry->setAcl($acl);
140
		}
141
		return $entries;
142
	}
143
144
	public function findAllByCircles($userId, $limit = null, $offset = null) {
145
		if (!$this->circlesEnabled) {
146
			return [];
147
		}
148
		$circles = array_map(function($circle) {
149
			return $circle->getUniqueId();
150
		}, \OCA\Circles\Api\v1\Circles::joinedCircles('', true));
151
		if (count($circles) === 0) {
152
			return [];
153
		}
154
155
		$sql = 'SELECT boards.id, title, owner, color, archived, deleted_at, 2 as shared, last_modified FROM `*PREFIX*deck_boards` as boards ' .
156
			'INNER JOIN `*PREFIX*deck_board_acl` as acl ON boards.id=acl.board_id WHERE owner != ? AND type=? AND (';
157 View Code Duplication
		for ($i = 0, $iMax = count($circles); $i < $iMax; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
			$sql .= 'acl.participant = ? ';
159
			if (count($circles) > 1 && $i < count($circles) - 1) {
160
				$sql .= ' OR ';
161
			}
162
		}
163
		$sql .= ');';
164
		$entries = $this->findEntities($sql, array_merge([$userId, Acl::PERMISSION_TYPE_CIRCLE], $circles), $limit, $offset);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::findEntities() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
165
		/* @var Board $entry */
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
166
		foreach ($entries as $entry) {
167
			$acl = $this->aclMapper->findAll($entry->id);
168
			$entry->setAcl($acl);
169
		}
170
		return $entries;
171
	}
172
173
	public function findAll() {
174
		$sql = 'SELECT id from *PREFIX*deck_boards;';
175
		return $this->findEntities($sql);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::findEntities() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
176
	}
177
178
	public function findToDelete() {
179
		// add buffer of 5 min
180
		$timeLimit = time() - (60 * 5);
181
		$sql = 'SELECT id, title, owner, color, archived, deleted_at, last_modified FROM `*PREFIX*deck_boards` ' .
182
			'WHERE `deleted_at` > 0 AND `deleted_at` < ?';
183
		return $this->findEntities($sql, [$timeLimit]);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::findEntities() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
184
	}
185
186
	public function delete(/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
187
		\OCP\AppFramework\Db\Entity $entity) {
188
		// delete acl
189
		$acl = $this->aclMapper->findAll($entity->getId());
190
		foreach ($acl as $item) {
191
			$this->aclMapper->delete($item);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::delete() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
192
		}
193
194
		// delete stacks ( includes cards, assigned labels)
195
		$stacks = $this->stackMapper->findAll($entity->getId());
196
		foreach ($stacks as $stack) {
197
			$this->stackMapper->delete($stack);
198
		}
199
		// delete labels
200
		$labels = $this->labelMapper->findAll($entity->getId());
201
		foreach ($labels as $label) {
202
			$this->labelMapper->delete($label);
203
		}
204
205
		return parent::delete($entity);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::delete() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
206
	}
207
208
	public function isOwner($userId, $boardId) {
209
		$board = $this->find($boardId);
210
		return ($board->getOwner() === $userId);
211
	}
212
213
	public function findBoardId($id) {
214
		return $id;
215
	}
216
217
	public function mapAcl(Acl &$acl) {
218
		$userManager = $this->userManager;
219
		$groupManager = $this->groupManager;
220
		$acl->resolveRelation('participant', function($participant) use (&$acl, &$userManager, &$groupManager) {
221 View Code Duplication
			if ($acl->getType() === Acl::PERMISSION_TYPE_USER) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
222
				$user = $userManager->get($participant);
223
				if ($user !== null) {
224
					return new User($user);
225
				}
226
				\OC::$server->getLogger()->debug('User ' . $acl->getId() . ' not found when mapping acl ' . $acl->getParticipant());
227
				return null;
228
			}
229 View Code Duplication
			if ($acl->getType() === Acl::PERMISSION_TYPE_GROUP) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
230
				$group = $groupManager->get($participant);
231
				if ($group !== null) {
232
					return new Group($group);
233
				}
234
				\OC::$server->getLogger()->debug('Group ' . $acl->getId() . ' not found when mapping acl ' . $acl->getParticipant());
235
				return null;
236
			}
237
			if ($acl->getType() === Acl::PERMISSION_TYPE_CIRCLE) {
238
				try {
239
					$circle = \OCA\Circles\Api\v1\Circles::detailsCircle($acl->getParticipant(), true);
240
					if ($circle) {
241
						return new Circle($circle);
242
					}
243
				} catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
244
				}
245
				return null;
246
			}
247
			throw new \Exception('Unknown permission type for mapping Acl');
248
		});
249
	}
250
251
	/**
252
	 * @param Board $board
253
	 */
254 View Code Duplication
	public function mapOwner(Board &$board) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
255
		$userManager = $this->userManager;
256
		$board->resolveRelation('owner', function($owner) use (&$userManager) {
257
			$user = $userManager->get($owner);
258
			if ($user !== null) {
259
				return new User($user);
260
			}
261
			return null;
262
		});
263
	}
264
265
266
}
267