1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* sysPass |
4
|
|
|
* |
5
|
|
|
* @author nuxsmin |
6
|
|
|
* @link https://syspass.org |
7
|
|
|
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org |
8
|
|
|
* |
9
|
|
|
* This file is part of sysPass. |
10
|
|
|
* |
11
|
|
|
* sysPass is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* sysPass is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU General Public License |
22
|
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace SP\Repositories\ItemPreset; |
26
|
|
|
|
27
|
|
|
use SP\DataModel\ItemPresetData; |
28
|
|
|
use SP\DataModel\ItemSearchData; |
29
|
|
|
use SP\Repositories\Repository; |
30
|
|
|
use SP\Repositories\RepositoryItemInterface; |
31
|
|
|
use SP\Repositories\RepositoryItemTrait; |
32
|
|
|
use SP\Storage\Database\QueryData; |
33
|
|
|
use SP\Storage\Database\QueryResult; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Class AccountDefaultPermissionRepository |
37
|
|
|
* |
38
|
|
|
* @package SP\Repositories\Account |
39
|
|
|
*/ |
40
|
|
|
class ItemPresetRepository extends Repository implements RepositoryItemInterface |
41
|
|
|
{ |
42
|
|
|
use RepositoryItemTrait; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Creates an item |
46
|
|
|
* |
47
|
|
|
* @param ItemPresetData $itemData |
48
|
|
|
* |
49
|
|
|
* @return int |
50
|
|
|
* @throws \SP\Core\Exceptions\ConstraintException |
51
|
|
|
* @throws \SP\Core\Exceptions\QueryException |
52
|
|
|
*/ |
53
|
|
|
public function create($itemData) |
54
|
|
|
{ |
55
|
|
|
$queryData = new QueryData(); |
56
|
|
|
$queryData->setQuery( |
57
|
|
|
'INSERT INTO ItemPreset |
58
|
|
|
SET type = ?, |
59
|
|
|
userId = ?, |
60
|
|
|
userGroupId = ?, |
61
|
|
|
userProfileId = ?, |
62
|
|
|
`fixed` = ?, |
63
|
|
|
priority = ?, |
64
|
|
|
`data` = ?, |
65
|
|
|
`hash` = ?'); |
66
|
|
|
$queryData->setParams([ |
67
|
|
|
$itemData->getType(), |
68
|
|
|
$itemData->getUserId(), |
69
|
|
|
$itemData->getUserGroupId(), |
70
|
|
|
$itemData->getUserProfileId(), |
71
|
|
|
$itemData->getFixed(), |
72
|
|
|
$itemData->getPriority(), |
73
|
|
|
$itemData->getData(), |
74
|
|
|
$itemData->getHash() |
75
|
|
|
]); |
76
|
|
|
$queryData->setOnErrorMessage(__u('Error al crear permiso')); |
77
|
|
|
|
78
|
|
|
return $this->db->doQuery($queryData)->getLastId(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Updates an item |
83
|
|
|
* |
84
|
|
|
* @param ItemPresetData $itemData |
85
|
|
|
* |
86
|
|
|
* @return int |
87
|
|
|
* @throws \SP\Core\Exceptions\ConstraintException |
88
|
|
|
* @throws \SP\Core\Exceptions\QueryException |
89
|
|
|
*/ |
90
|
|
|
public function update($itemData) |
91
|
|
|
{ |
92
|
|
|
$queryData = new QueryData(); |
93
|
|
|
$queryData->setQuery( |
94
|
|
|
'UPDATE ItemPreset |
95
|
|
|
SET type = ?, |
96
|
|
|
userId = ?, |
97
|
|
|
userGroupId = ?, |
98
|
|
|
userProfileId = ?, |
99
|
|
|
`fixed` = ?, |
100
|
|
|
priority = ?, |
101
|
|
|
`data` = ?, |
102
|
|
|
`hash` = ? |
103
|
|
|
WHERE id = ? LIMIT 1'); |
104
|
|
|
$queryData->setParams([ |
105
|
|
|
$itemData->getType(), |
106
|
|
|
$itemData->getUserId(), |
107
|
|
|
$itemData->getUserGroupId(), |
108
|
|
|
$itemData->getUserProfileId(), |
109
|
|
|
$itemData->getFixed(), |
110
|
|
|
$itemData->getPriority(), |
111
|
|
|
$itemData->getData(), |
112
|
|
|
$itemData->getHash(), |
113
|
|
|
$itemData->getId() |
114
|
|
|
]); |
115
|
|
|
$queryData->setOnErrorMessage(__u('Error al actualizar permiso')); |
116
|
|
|
|
117
|
|
|
return $this->db->doQuery($queryData)->getAffectedNumRows(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Deletes an item |
122
|
|
|
* |
123
|
|
|
* @param $id |
124
|
|
|
* |
125
|
|
|
* @return int |
126
|
|
|
* @throws \SP\Core\Exceptions\ConstraintException |
127
|
|
|
* @throws \SP\Core\Exceptions\QueryException |
128
|
|
|
*/ |
129
|
|
|
public function delete($id) |
130
|
|
|
{ |
131
|
|
|
$queryData = new QueryData(); |
132
|
|
|
$queryData->setQuery('DELETE FROM ItemPreset WHERE id = ? LIMIT 1'); |
133
|
|
|
$queryData->setParams([$id]); |
134
|
|
|
$queryData->setOnErrorMessage(__u('Error al eliminar permiso')); |
135
|
|
|
|
136
|
|
|
return $this->db->doQuery($queryData)->getAffectedNumRows(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns the item for given id |
141
|
|
|
* |
142
|
|
|
* @param int $id |
143
|
|
|
* |
144
|
|
|
* @return QueryResult |
145
|
|
|
* @throws \SP\Core\Exceptions\ConstraintException |
146
|
|
|
* @throws \SP\Core\Exceptions\QueryException |
147
|
|
|
*/ |
148
|
|
|
public function getById($id) |
149
|
|
|
{ |
150
|
|
|
$queryData = new QueryData(); |
151
|
|
|
$queryData->setMapClassName(ItemPresetData::class); |
152
|
|
|
$queryData->setQuery( |
153
|
|
|
'SELECT id, type, userId, userGroupId, userProfileId, `fixed`, priority, `data` |
154
|
|
|
FROM ItemPreset WHERE id = ? LIMIT 1'); |
155
|
|
|
$queryData->setParams([$id]); |
156
|
|
|
|
157
|
|
|
return $this->db->doSelect($queryData); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns the item for given id |
162
|
|
|
* |
163
|
|
|
* @param string $type |
164
|
|
|
* @param int $userId |
165
|
|
|
* @param int $userGroupId |
166
|
|
|
* @param int $userProfileId |
167
|
|
|
* |
168
|
|
|
* @return QueryResult |
169
|
|
|
* @throws \SP\Core\Exceptions\ConstraintException |
170
|
|
|
* @throws \SP\Core\Exceptions\QueryException |
171
|
|
|
*/ |
172
|
|
|
public function getByFilter(string $type, int $userId, int $userGroupId, int $userProfileId) |
173
|
|
|
{ |
174
|
|
|
$queryData = new QueryData(); |
175
|
|
|
$queryData->setMapClassName(ItemPresetData::class); |
176
|
|
|
$queryData->setQuery( |
177
|
|
|
'SELECT id, type, userId, userGroupId, userProfileId, `fixed`, priority, `data` |
178
|
|
|
FROM ItemPreset |
179
|
|
|
WHERE type = ? AND (userId = ? OR userGroupId = ? OR userProfileId = ?) |
180
|
|
|
ORDER BY priority DESC, userId DESC, userProfileId DESC, userGroupId DESC |
181
|
|
|
LIMIT 1'); |
182
|
|
|
|
183
|
|
|
$queryData->setParams([$type, $userId, $userGroupId, $userProfileId]); |
184
|
|
|
|
185
|
|
|
return $this->db->doSelect($queryData); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Returns all the items |
190
|
|
|
* |
191
|
|
|
* @return QueryResult |
192
|
|
|
* @throws \SP\Core\Exceptions\ConstraintException |
193
|
|
|
* @throws \SP\Core\Exceptions\QueryException |
194
|
|
|
*/ |
195
|
|
|
public function getAll() |
196
|
|
|
{ |
197
|
|
|
$queryData = new QueryData(); |
198
|
|
|
$queryData->setMapClassName(ItemPresetData::class); |
199
|
|
|
$queryData->setQuery( |
200
|
|
|
'SELECT id, type, userId, userGroupId, userProfileId, `fixed`, priority, `data` |
201
|
|
|
FROM ItemPreset'); |
202
|
|
|
|
203
|
|
|
return $this->db->doSelect($queryData); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Returns all the items for given ids |
208
|
|
|
* |
209
|
|
|
* @param array $ids |
210
|
|
|
* |
211
|
|
|
* @return QueryResult |
212
|
|
|
* @throws \SP\Core\Exceptions\ConstraintException |
213
|
|
|
* @throws \SP\Core\Exceptions\QueryException |
214
|
|
|
*/ |
215
|
|
|
public function getByIdBatch(array $ids) |
216
|
|
|
{ |
217
|
|
|
if (empty($ids)) { |
218
|
|
|
return new QueryResult(); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$queryData = new QueryData(); |
222
|
|
|
$queryData->setMapClassName(ItemPresetData::class); |
223
|
|
|
$queryData->setQuery( |
224
|
|
|
'SELECT type, userId, userGroupId, userProfileId, `fixed`, priority, `data` |
225
|
|
|
FROM ItemPreset WHERE id IN (' . $this->getParamsFromArray($ids) . ')'); |
226
|
|
|
$queryData->setParams($ids); |
227
|
|
|
|
228
|
|
|
return $this->db->doSelect($queryData); |
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Deletes all the items for given ids |
233
|
|
|
* |
234
|
|
|
* @param array $ids |
235
|
|
|
* |
236
|
|
|
* @return int |
237
|
|
|
* @throws \SP\Core\Exceptions\ConstraintException |
238
|
|
|
* @throws \SP\Core\Exceptions\QueryException |
239
|
|
|
*/ |
240
|
|
|
public function deleteByIdBatch(array $ids) |
241
|
|
|
{ |
242
|
|
|
if (empty($ids)) { |
243
|
|
|
return 0; |
|
|
|
|
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
$queryData = new QueryData(); |
247
|
|
|
$queryData->setQuery('DELETE FROM ItemPreset WHERE id IN (' . $this->getParamsFromArray($ids) . ')'); |
248
|
|
|
$queryData->setParams($ids); |
249
|
|
|
$queryData->setOnErrorMessage(__u('Error al eliminar los permisos')); |
250
|
|
|
|
251
|
|
|
return $this->db->doQuery($queryData)->getAffectedNumRows(); |
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Checks whether the item is in use or not |
256
|
|
|
* |
257
|
|
|
* @param $id int |
258
|
|
|
*/ |
259
|
|
|
public function checkInUse($id) |
260
|
|
|
{ |
261
|
|
|
throw new \RuntimeException('Not implemented'); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Checks whether the item is duplicated on updating |
266
|
|
|
* |
267
|
|
|
* @param mixed $itemData |
268
|
|
|
*/ |
269
|
|
|
public function checkDuplicatedOnUpdate($itemData) |
270
|
|
|
{ |
271
|
|
|
throw new \RuntimeException('Not implemented'); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Checks whether the item is duplicated on adding |
276
|
|
|
* |
277
|
|
|
* @param mixed $itemData |
278
|
|
|
*/ |
279
|
|
|
public function checkDuplicatedOnAdd($itemData) |
280
|
|
|
{ |
281
|
|
|
throw new \RuntimeException('Not implemented'); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Searches for items by a given filter |
286
|
|
|
* |
287
|
|
|
* @param ItemSearchData $itemSearchData |
288
|
|
|
* |
289
|
|
|
* @return QueryResult |
290
|
|
|
* @throws \SP\Core\Exceptions\ConstraintException |
291
|
|
|
* @throws \SP\Core\Exceptions\QueryException |
292
|
|
|
*/ |
293
|
|
|
public function search(ItemSearchData $itemSearchData) |
294
|
|
|
{ |
295
|
|
|
$queryData = new QueryData(); |
296
|
|
|
$queryData->setSelect( |
297
|
|
|
'IP.id, |
298
|
|
|
IP.type, |
299
|
|
|
IP.userId, |
300
|
|
|
IP.userGroupId, |
301
|
|
|
IP.userProfileId, |
302
|
|
|
IP.`fixed`, |
303
|
|
|
IP.priority, |
304
|
|
|
IP.data, |
305
|
|
|
U.name AS userName, |
306
|
|
|
UP.name AS userProfileName, |
307
|
|
|
UG.name AS userGroupName'); |
308
|
|
|
$queryData->setFrom(' |
309
|
|
|
ItemPreset IP |
310
|
|
|
LEFT JOIN User U ON IP.userId = U.id |
311
|
|
|
LEFT JOIN UserProfile UP ON IP.userProfileId = UP.id |
312
|
|
|
LEFT JOIN UserGroup UG ON IP.userGroupId = UG.id'); |
313
|
|
|
$queryData->setOrder('IP.type, IP.priority DESC, IP.userId DESC, IP.userProfileId DESC, IP.userGroupId DESC'); |
314
|
|
|
|
315
|
|
|
if ($itemSearchData->getSeachString() !== '') { |
316
|
|
|
$queryData->setWhere('IP.type LIKE ? OR U.name LIKE ? OR UP.name LIKE ? OR UG.name LIKE ?'); |
317
|
|
|
|
318
|
|
|
$search = '%' . $itemSearchData->getSeachString() . '%'; |
319
|
|
|
$queryData->addParam($search); |
320
|
|
|
$queryData->addParam($search); |
321
|
|
|
$queryData->addParam($search); |
322
|
|
|
$queryData->addParam($search); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
$queryData->setLimit('?,?'); |
326
|
|
|
$queryData->addParam($itemSearchData->getLimitStart()); |
327
|
|
|
$queryData->addParam($itemSearchData->getLimitCount()); |
328
|
|
|
|
329
|
|
|
return $this->db->doSelect($queryData, true); |
330
|
|
|
} |
331
|
|
|
} |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: