BoardMapper::findAllByGroups()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 21

Duplication

Lines 6
Ratio 28.57 %

Importance

Changes 0
Metric Value
dl 6
loc 21
rs 8.9617
c 0
b 0
f 0
cc 6
nc 7
nop 4
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\ILogger;
30
use OCP\IUserManager;
31
use OCP\IGroupManager;
32
33
class BoardMapper extends DeckMapper implements IPermissionMapper {
34
35
	private $labelMapper;
36
	private $aclMapper;
37
	private $stackMapper;
38
	private $userManager;
39
	private $groupManager;
40
41
	private $circlesEnabled;
42
43
	public function __construct(
44
		IDBConnection $db,
45
		LabelMapper $labelMapper,
46
		AclMapper $aclMapper,
47
		StackMapper $stackMapper,
48
		IUserManager $userManager,
49
		IGroupManager $groupManager
50
	) {
51
		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...
52
		$this->labelMapper = $labelMapper;
53
		$this->aclMapper = $aclMapper;
54
		$this->stackMapper = $stackMapper;
55
		$this->userManager = $userManager;
56
		$this->groupManager = $groupManager;
57
58
		$this->circlesEnabled = \OC::$server->getAppManager()->isEnabledForUser('circles');
59
	}
60
61
62
	/**
63
	 * @param $id
64
	 * @param bool $withLabels
65
	 * @param bool $withAcl
66
	 * @return \OCP\AppFramework\Db\Entity
67
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
68
	 * @throws DoesNotExistException
69
	 */
70
	public function find($id, $withLabels = false, $withAcl = false) {
71
		$sql = 'SELECT id, title, owner, color, archived, deleted_at, last_modified FROM `*PREFIX*deck_boards` ' .
72
			'WHERE `id` = ?';
73
		$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...
74
75
		// Add labels
76
		if ($withLabels) {
77
			$labels = $this->labelMapper->findAll($id);
78
			$board->setLabels($labels);
79
		}
80
81
		// Add acl
82
		if ($withAcl) {
83
			$acl = $this->aclMapper->findAll($id);
84
			$board->setAcl($acl);
85
		}
86
87
		return $board;
88
	}
89
90
	/**
91
	 * Find all boards for a given user
92
	 *
93
	 * @param $userId
94
	 * @param null $limit
95
	 * @param null $offset
96
	 * @return array
97
	 */
98
	public function findAllByUser($userId, $limit = null, $offset = null, $since = -1) {
99
		$sql = 'SELECT id, title, owner, color, archived, deleted_at, 0 as shared, last_modified FROM `*PREFIX*deck_boards` WHERE owner = ? AND last_modified > ? UNION ' .
100
			'SELECT boards.id, title, owner, color, archived, deleted_at, 1 as shared, last_modified FROM `*PREFIX*deck_boards` as boards ' .
101
			'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 > ?';
102
		$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...
103
		/* @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...
104
		foreach ($entries as $entry) {
105
			$acl = $this->aclMapper->findAll($entry->id);
106
			$entry->setAcl($acl);
107
		}
108
		return $entries;
109
	}
110
111
	/**
112
	 * Find all boards for a given user
113
	 *
114
	 * @param $userId
115
	 * @param $groups
116
	 * @param null $limit
117
	 * @param null $offset
118
	 * @return array
119
	 */
120
	public function findAllByGroups($userId, $groups, $limit = null, $offset = null) {
121
		if (count($groups) <= 0) {
122
			return [];
123
		}
124
		$sql = 'SELECT boards.id, title, owner, color, archived, deleted_at, 2 as shared, last_modified FROM `*PREFIX*deck_boards` as boards ' .
125
			'INNER JOIN `*PREFIX*deck_board_acl` as acl ON boards.id=acl.board_id WHERE owner != ? AND type=? AND (';
126 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...
127
			$sql .= 'acl.participant = ? ';
128
			if (count($groups) > 1 && $i < count($groups) - 1) {
129
				$sql .= ' OR ';
130
			}
131
		}
132
		$sql .= ');';
133
		$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...
134
		/* @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...
135
		foreach ($entries as $entry) {
136
			$acl = $this->aclMapper->findAll($entry->id);
137
			$entry->setAcl($acl);
138
		}
139
		return $entries;
140
	}
141
142
	public function findAllByCircles($userId, $limit = null, $offset = null) {
143
		if (!$this->circlesEnabled) {
144
			return [];
145
		}
146
		$circles = array_map(function($circle) {
147
			return $circle->getUniqueId();
148
		}, \OCA\Circles\Api\v1\Circles::joinedCircles('', true));
149
		if (count($circles) === 0) {
150
			return [];
151
		}
152
153
		$sql = 'SELECT boards.id, title, owner, color, archived, deleted_at, 2 as shared, last_modified FROM `*PREFIX*deck_boards` as boards ' .
154
			'INNER JOIN `*PREFIX*deck_board_acl` as acl ON boards.id=acl.board_id WHERE owner != ? AND type=? AND (';
155 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...
156
			$sql .= 'acl.participant = ? ';
157
			if (count($circles) > 1 && $i < count($circles) - 1) {
158
				$sql .= ' OR ';
159
			}
160
		}
161
		$sql .= ');';
162
		$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...
163
		/* @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...
164
		foreach ($entries as $entry) {
165
			$acl = $this->aclMapper->findAll($entry->id);
166
			$entry->setAcl($acl);
167
		}
168
		return $entries;
169
	}
170
171
	public function findAll() {
172
		$sql = 'SELECT id from *PREFIX*deck_boards;';
173
		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...
174
	}
175
176
	public function findToDelete() {
177
		// add buffer of 5 min
178
		$timeLimit = time() - (60 * 5);
179
		$sql = 'SELECT id, title, owner, color, archived, deleted_at, last_modified FROM `*PREFIX*deck_boards` ' .
180
			'WHERE `deleted_at` > 0 AND `deleted_at` < ?';
181
		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...
182
	}
183
184
	public function delete(/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
185
		\OCP\AppFramework\Db\Entity $entity) {
186
		// delete acl
187
		$acl = $this->aclMapper->findAll($entity->getId());
188
		foreach ($acl as $item) {
189
			$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...
190
		}
191
192
		// delete stacks ( includes cards, assigned labels)
193
		$stacks = $this->stackMapper->findAll($entity->getId());
194
		foreach ($stacks as $stack) {
195
			$this->stackMapper->delete($stack);
196
		}
197
		// delete labels
198
		$labels = $this->labelMapper->findAll($entity->getId());
199
		foreach ($labels as $label) {
200
			$this->labelMapper->delete($label);
201
		}
202
203
		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...
204
	}
205
206
	public function isOwner($userId, $boardId) {
207
		$board = $this->find($boardId);
208
		return ($board->getOwner() === $userId);
209
	}
210
211
	public function findBoardId($id) {
212
		return $id;
213
	}
214
215
	public function mapAcl(Acl &$acl) {
216
		$userManager = $this->userManager;
217
		$groupManager = $this->groupManager;
218
		$acl->resolveRelation('participant', function($participant) use (&$acl, &$userManager, &$groupManager) {
219 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...
220
				$user = $userManager->get($participant);
221
				if ($user !== null) {
222
					return new User($user);
223
				}
224
				\OC::$server->getLogger()->debug('User ' . $acl->getId() . ' not found when mapping acl ' . $acl->getParticipant());
225
				return null;
226
			}
227 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...
228
				$group = $groupManager->get($participant);
229
				if ($group !== null) {
230
					return new Group($group);
231
				}
232
				\OC::$server->getLogger()->debug('Group ' . $acl->getId() . ' not found when mapping acl ' . $acl->getParticipant());
233
				return null;
234
			}
235
			if ($acl->getType() === Acl::PERMISSION_TYPE_CIRCLE && $this->circlesEnabled) {
236
				try {
237
					$circle = \OCA\Circles\Api\v1\Circles::detailsCircle($acl->getParticipant(), true);
238
					if ($circle) {
239
						return new Circle($circle);
240
					}
241
				} catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
242
				}
243
				return null;
244
			}
245
			\OC::$server->getLogger()->log(ILogger::WARN, 'Unknown permission type for mapping acl ' . $acl->getId());
246
			return null;
247
		});
248
	}
249
250
	/**
251
	 * @param Board $board
252
	 */
253 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...
254
		$userManager = $this->userManager;
255
		$board->resolveRelation('owner', function($owner) use (&$userManager) {
256
			$user = $userManager->get($owner);
257
			if ($user !== null) {
258
				return new User($user);
259
			}
260
			return null;
261
		});
262
	}
263
264
265
}
266