|
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\IDBConnection; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
class BoardMapper extends DeckMapper implements IPermissionMapper { |
|
30
|
|
|
|
|
31
|
|
|
private $labelMapper; |
|
32
|
|
|
private $aclMapper; |
|
33
|
|
|
private $stackMapper; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(IDBConnection $db, LabelMapper $labelMapper, AclMapper $aclMapper, StackMapper $stackMapper) { |
|
36
|
|
|
parent::__construct($db, 'deck_boards', '\OCA\Deck\Db\Board'); |
|
37
|
|
|
$this->labelMapper = $labelMapper; |
|
38
|
|
|
$this->aclMapper = $aclMapper; |
|
39
|
|
|
$this->stackMapper = $stackMapper; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param $id |
|
45
|
|
|
* @param bool $withLabels |
|
46
|
|
|
* @param bool $withAcl |
|
47
|
|
|
* @return \OCP\AppFramework\Db\Entity if not found |
|
48
|
|
|
*/ |
|
49
|
|
|
public function find($id, $withLabels = false, $withAcl = false) { |
|
50
|
|
|
$sql = 'SELECT id, title, owner, color, archived FROM `*PREFIX*deck_boards` ' . |
|
51
|
|
|
'WHERE `id` = ?'; |
|
52
|
|
|
$board = $this->findEntity($sql, [$id]); |
|
53
|
|
|
|
|
54
|
|
|
// Add labels |
|
55
|
|
|
if ($withLabels) { |
|
56
|
|
|
$labels = $this->labelMapper->findAll($id); |
|
57
|
|
|
$board->setLabels($labels); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// Add acl |
|
61
|
|
|
if ($withAcl) { |
|
62
|
|
|
$acl = $this->aclMapper->findAll($id); |
|
63
|
|
|
$board->setAcl($acl); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $board; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Find all boards for a given user |
|
71
|
|
|
* |
|
72
|
|
|
* @param $userId |
|
73
|
|
|
* @param null $limit |
|
74
|
|
|
* @param null $offset |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
public function findAllByUser($userId, $limit = null, $offset = null) { |
|
78
|
|
|
$sql = 'SELECT id, title, owner, color, archived, 0 as shared FROM oc_deck_boards WHERE owner = ? UNION ' . |
|
79
|
|
|
'SELECT boards.id, title, owner, color, archived, 1 as shared FROM oc_deck_boards as boards ' . |
|
80
|
|
|
'JOIN oc_deck_board_acl as acl ON boards.id=acl.board_id WHERE acl.participant=? AND acl.type=? AND boards.owner != ?'; |
|
81
|
|
|
$entries = $this->findEntities($sql, [$userId, $userId, Acl::PERMISSION_TYPE_USER, $userId], $limit, $offset); |
|
82
|
|
|
/* @var Board $entry */ |
|
83
|
|
|
foreach ($entries as $entry) { |
|
84
|
|
|
$acl = $this->aclMapper->findAll($entry->id); |
|
85
|
|
|
$entry->setAcl($acl); |
|
86
|
|
|
} |
|
87
|
|
|
return $entries; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Find all boards for a given user |
|
92
|
|
|
* |
|
93
|
|
|
* @param $userId |
|
94
|
|
|
* @param $groups |
|
95
|
|
|
* @param null $limit |
|
96
|
|
|
* @param null $offset |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
public function findAllByGroups($userId, $groups, $limit = null, $offset = null) { |
|
100
|
|
|
if (count($groups) <= 0) { |
|
101
|
|
|
return []; |
|
102
|
|
|
} |
|
103
|
|
|
$sql = 'SELECT boards.id, title, owner, color, archived, 2 as shared FROM oc_deck_boards as boards ' . |
|
104
|
|
|
'INNER JOIN oc_deck_board_acl as acl ON boards.id=acl.board_id WHERE owner != ? AND type=? AND ('; |
|
105
|
|
|
for ($i = 0; $i < count($groups); $i++) { |
|
|
|
|
|
|
106
|
|
|
$sql .= 'acl.participant = ? '; |
|
107
|
|
|
if (count($groups) > 1 && $i < count($groups) - 1) { |
|
108
|
|
|
$sql .= ' OR '; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
$sql .= ');'; |
|
112
|
|
|
$entries = $this->findEntities($sql, array_merge([$userId, Acl::PERMISSION_TYPE_GROUP], $groups), $limit, $offset); |
|
113
|
|
|
/* @var Board $entry */ |
|
114
|
|
|
foreach ($entries as $entry) { |
|
115
|
|
|
$acl = $this->aclMapper->findAll($entry->id); |
|
116
|
|
|
$entry->setAcl($acl); |
|
117
|
|
|
} |
|
118
|
|
|
return $entries; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function delete(/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ |
|
122
|
|
|
\OCP\AppFramework\Db\Entity $entity) { |
|
123
|
|
|
// delete acl |
|
124
|
|
|
$acl = $this->aclMapper->findAll($entity->getId()); |
|
125
|
|
|
foreach ($acl as $item) { |
|
126
|
|
|
$this->aclMapper->delete($item); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// delete stacks ( includes cards, assigned labels) |
|
130
|
|
|
$stacks = $this->stackMapper->findAll($entity->getId()); |
|
131
|
|
|
foreach ($stacks as $stack) { |
|
132
|
|
|
$this->stackMapper->delete($stack); |
|
133
|
|
|
} |
|
134
|
|
|
// delete labels |
|
135
|
|
|
$labels = $this->labelMapper->findAll($entity->getId()); |
|
136
|
|
|
foreach ($labels as $label) { |
|
137
|
|
|
$this->labelMapper->delete($label); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return parent::delete($entity); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function isOwner($userId, $boardId) { |
|
144
|
|
|
$board = $this->find($boardId); |
|
145
|
|
|
return ($board->getOwner() === $userId); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function findBoardId($id) { |
|
149
|
|
|
return $id; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
} |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: