Passed
Push — devel-3.0 ( 5f7f30...cd1038 )
by Rubén
03:44
created

AccountDefaultPermissionRepository::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 1
dl 0
loc 24
rs 9.6
c 0
b 0
f 0
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\Account;
26
27
use SP\DataModel\AccountDefaultPermissionData;
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 AccountDefaultPermissionRepository extends Repository implements RepositoryItemInterface
41
{
42
    use RepositoryItemTrait;
43
44
    /**
45
     * Creates an item
46
     *
47
     * @param AccountDefaultPermissionData $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 AccountDefaultPermission 
58
                SET userId = ?, 
59
                userGroupId = ?, 
60
                userProfileId = ?, 
61
                `fixed` = ?, 
62
                priority = ?,
63
                permission = ?,
64
                `hash` = ?');
65
        $queryData->setParams([
66
            $itemData->getUserId(),
67
            $itemData->getUserGroupId(),
68
            $itemData->getUserProfileId(),
69
            $itemData->getFixed(),
70
            $itemData->getPriority(),
71
            $itemData->getPermission(),
72
            $itemData->getHash()
73
        ]);
74
        $queryData->setOnErrorMessage(__u('Error al crear permiso'));
75
76
        return $this->db->doQuery($queryData)->getLastId();
77
    }
78
79
    /**
80
     * Updates an item
81
     *
82
     * @param AccountDefaultPermissionData $itemData
83
     *
84
     * @return int
85
     * @throws \SP\Core\Exceptions\ConstraintException
86
     * @throws \SP\Core\Exceptions\QueryException
87
     */
88
    public function update($itemData)
89
    {
90
        $queryData = new QueryData();
91
        $queryData->setQuery(
92
            'UPDATE AccountDefaultPermission 
93
                SET userId = ?, 
94
                userGroupId = ?, 
95
                userProfileId = ?, 
96
                `fixed` = ?, 
97
                priority = ?,
98
                permission = ?,
99
                `hash` = ?
100
                WHERE id = ? LIMIT 1');
101
        $queryData->setParams([
102
            $itemData->getUserId(),
103
            $itemData->getUserGroupId(),
104
            $itemData->getUserProfileId(),
105
            $itemData->getFixed(),
106
            $itemData->getPriority(),
107
            $itemData->getPermission(),
108
            $itemData->getHash(),
109
            $itemData->getId()
110
        ]);
111
        $queryData->setOnErrorMessage(__u('Error al actualizar permiso'));
112
113
        return $this->db->doQuery($queryData)->getAffectedNumRows();
114
    }
115
116
    /**
117
     * Deletes an item
118
     *
119
     * @param $id
120
     *
121
     * @return int
122
     * @throws \SP\Core\Exceptions\ConstraintException
123
     * @throws \SP\Core\Exceptions\QueryException
124
     */
125
    public function delete($id)
126
    {
127
        $queryData = new QueryData();
128
        $queryData->setQuery('DELETE FROM AccountDefaultPermission WHERE id = ? LIMIT 1');
129
        $queryData->setParams([$id]);
130
        $queryData->setOnErrorMessage(__u('Error al eliminar permiso'));
131
132
        return $this->db->doQuery($queryData)->getAffectedNumRows();
133
    }
134
135
    /**
136
     * Returns the item for given id
137
     *
138
     * @param int $id
139
     *
140
     * @return QueryResult
141
     * @throws \SP\Core\Exceptions\ConstraintException
142
     * @throws \SP\Core\Exceptions\QueryException
143
     */
144
    public function getById($id)
145
    {
146
        $queryData = new QueryData();
147
        $queryData->setMapClassName(AccountDefaultPermissionData::class);
148
        $queryData->setQuery(
149
            'SELECT id, userId, userGroupId, userProfileId, `fixed`, priority, permission 
150
        FROM AccountDefaultPermission WHERE id = ? LIMIT 1');
151
        $queryData->setParams([$id]);
152
153
        return $this->db->doSelect($queryData);
154
    }
155
156
    /**
157
     * Returns the item for given id
158
     *
159
     * @param int $userId
160
     * @param int $userGroupId
161
     * @param int $userProfileId
162
     *
163
     * @return QueryResult
164
     * @throws \SP\Core\Exceptions\ConstraintException
165
     * @throws \SP\Core\Exceptions\QueryException
166
     */
167
    public function getByFilter(int $userId, int $userGroupId, int $userProfileId)
168
    {
169
        $queryData = new QueryData();
170
        $queryData->setMapClassName(AccountDefaultPermissionData::class);
171
        $queryData->setQuery(
172
            'SELECT id, userId, userGroupId, userProfileId, `fixed`, priority, permission 
173
        FROM AccountDefaultPermission 
174
        WHERE userId = ? OR userGroupId = ? OR userProfileId = ?
175
        ORDER BY priority DESC, userId DESC, userProfileId DESC, userGroupId DESC
176
        LIMIT 1');
177
178
        $queryData->setParams([$userId, $userGroupId, $userProfileId]);
179
180
        return $this->db->doSelect($queryData);
181
    }
182
183
    /**
184
     * Returns all the items
185
     *
186
     * @return QueryResult
187
     * @throws \SP\Core\Exceptions\ConstraintException
188
     * @throws \SP\Core\Exceptions\QueryException
189
     */
190
    public function getAll()
191
    {
192
        $queryData = new QueryData();
193
        $queryData->setMapClassName(AccountDefaultPermissionData::class);
194
        $queryData->setQuery(
195
            'SELECT id, userId, userGroupId, userProfileId, `fixed`, priority, permission 
196
        FROM AccountDefaultPermission');
197
198
        return $this->db->doSelect($queryData);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->db->doSelect($queryData) returns the type SP\Storage\Database\QueryResult which is incompatible with the return type mandated by SP\Repositories\RepositoryItemInterface::getAll() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
199
    }
200
201
    /**
202
     * Returns all the items for given ids
203
     *
204
     * @param array $ids
205
     *
206
     * @return QueryResult
207
     * @throws \SP\Core\Exceptions\ConstraintException
208
     * @throws \SP\Core\Exceptions\QueryException
209
     */
210
    public function getByIdBatch(array $ids)
211
    {
212
        if (empty($ids)) {
213
            return new QueryResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return new SP\Storage\Database\QueryResult() returns the type SP\Storage\Database\QueryResult which is incompatible with the return type mandated by SP\Repositories\Reposito...terface::getByIdBatch() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
214
        }
215
216
        $queryData = new QueryData();
217
        $queryData->setMapClassName(AccountDefaultPermissionData::class);
218
        $queryData->setQuery(
219
            'SELECT userId, userGroupId, userProfileId, `fixed`, priority, permission
220
            FROM AccountDefaultPermission WHERE id IN (' . $this->getParamsFromArray($ids) . ')');
221
        $queryData->setParams($ids);
222
223
        return $this->db->doSelect($queryData);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->db->doSelect($queryData) returns the type SP\Storage\Database\QueryResult which is incompatible with the return type mandated by SP\Repositories\Reposito...terface::getByIdBatch() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
224
    }
225
226
    /**
227
     * Deletes all the items for given ids
228
     *
229
     * @param array $ids
230
     *
231
     * @return int
232
     * @throws \SP\Core\Exceptions\ConstraintException
233
     * @throws \SP\Core\Exceptions\QueryException
234
     */
235
    public function deleteByIdBatch(array $ids)
236
    {
237
        if (empty($ids)) {
238
            return 0;
0 ignored issues
show
Bug Best Practice introduced by
The expression return 0 returns the type integer which is incompatible with the return type mandated by SP\Repositories\Reposito...face::deleteByIdBatch() of SP\Repositories\RepositoryItemInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
239
        }
240
241
        $queryData = new QueryData();
242
        $queryData->setQuery('DELETE FROM AccountDefaultPermission WHERE id IN (' . $this->getParamsFromArray($ids) . ')');
243
        $queryData->setParams($ids);
244
        $queryData->setOnErrorMessage(__u('Error al eliminar los permisos'));
245
246
        return $this->db->doQuery($queryData)->getAffectedNumRows();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->db->doQuer...)->getAffectedNumRows() returns the type integer which is incompatible with the return type mandated by SP\Repositories\Reposito...face::deleteByIdBatch() of SP\Repositories\RepositoryItemInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
247
    }
248
249
    /**
250
     * Checks whether the item is in use or not
251
     *
252
     * @param $id int
253
     */
254
    public function checkInUse($id)
255
    {
256
        throw new \RuntimeException('Not implemented');
257
    }
258
259
    /**
260
     * Checks whether the item is duplicated on updating
261
     *
262
     * @param mixed $itemData
263
     */
264
    public function checkDuplicatedOnUpdate($itemData)
265
    {
266
        throw new \RuntimeException('Not implemented');
267
    }
268
269
    /**
270
     * Checks whether the item is duplicated on adding
271
     *
272
     * @param mixed $itemData
273
     */
274
    public function checkDuplicatedOnAdd($itemData)
275
    {
276
        throw new \RuntimeException('Not implemented');
277
    }
278
279
    /**
280
     * Searches for items by a given filter
281
     *
282
     * @param ItemSearchData $itemSearchData
283
     *
284
     * @return QueryResult
285
     * @throws \SP\Core\Exceptions\ConstraintException
286
     * @throws \SP\Core\Exceptions\QueryException
287
     */
288
    public function search(ItemSearchData $itemSearchData)
289
    {
290
        $queryData = new QueryData();
291
        $queryData->setSelect(
292
            'ADP.id,
293
            ADP.userId,
294
            ADP.userGroupId,
295
            ADP.userProfileId,
296
            ADP.`fixed`,
297
            ADP.priority,
298
            ADP.permission,
299
            U.name AS userName,
300
            UP.name AS userProfileName,
301
            UG.name AS userGroupName');
302
        $queryData->setFrom('
303
            AccountDefaultPermission ADP 
304
            LEFT JOIN User U ON ADP.userId = U.id 
305
            LEFT JOIN UserProfile UP ON ADP.userProfileId = UP.id 
306
            LEFT JOIN UserGroup UG ON ADP.userGroupId = UG.id');
307
        $queryData->setOrder('id');
308
309
        if ($itemSearchData->getSeachString() !== '') {
310
            $queryData->setWhere('U.name LIKE ? OR UP.name LIKE ? OR UG.name LIKE ?');
311
312
            $search = '%' . $itemSearchData->getSeachString() . '%';
313
            $queryData->addParam($search);
314
            $queryData->addParam($search);
315
            $queryData->addParam($search);
316
        }
317
318
        $queryData->setLimit('?,?');
319
        $queryData->addParam($itemSearchData->getLimitStart());
320
        $queryData->addParam($itemSearchData->getLimitCount());
321
322
        return $this->db->doSelect($queryData, true);
323
    }
324
325
    /**
326
     * @param AccountDefaultPermissionData $data
327
     *
328
     * @return string
329
     */
330
    private function getHash(AccountDefaultPermissionData $data)
331
    {
332
        return sha1((int)$data->getUserId() . (int)$data->getUserGroupId() . (int)$data->getUserProfileId() . (int)$data->getPriority());
333
    }
334
}