Passed
Push — devel-3.0 ( 543c48...5b8639 )
by Rubén
03:28
created

AccountToUserRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 123
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addByType() 0 25 3
A updateByType() 0 6 1
A getUsersByAccountId() 0 15 1
A deleteTypeByAccountId() 0 8 1
A deleteByAccountId() 0 8 1
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\ItemData;
28
use SP\Repositories\Repository;
29
use SP\Repositories\RepositoryItemTrait;
30
use SP\Services\Account\AccountRequest;
31
use SP\Storage\Database\QueryData;
32
33
/**
34
 * Class AccountToUserRepository
35
 *
36
 * @package SP\Repositories\Account
37
 */
38
final class AccountToUserRepository extends Repository
39
{
40
    use RepositoryItemTrait;
41
42
    /**
43
     * Actualizar la asociación de grupos con cuentas.
44
     *
45
     * @param AccountRequest $accountRequest
46
     *
47
     * @param bool           $isEdit
48
     *
49
     * @return bool
50
     * @throws \SP\Core\Exceptions\ConstraintException
51
     * @throws \SP\Core\Exceptions\QueryException
52
     */
53
    public function updateByType(AccountRequest $accountRequest, bool $isEdit)
54
    {
55
        $this->deleteTypeByAccountId($accountRequest->id, $isEdit);
56
        $this->addByType($accountRequest, $isEdit);
57
58
        return false;
59
    }
60
61
    /**
62
     * Eliminar la asociación de grupos con cuentas.
63
     *
64
     * @param int  $id con el Id de la cuenta
65
     * @param bool $isEdit
66
     *
67
     * @return int
68
     * @throws \SP\Core\Exceptions\ConstraintException
69
     * @throws \SP\Core\Exceptions\QueryException
70
     */
71
    public function deleteTypeByAccountId($id, bool $isEdit)
72
    {
73
        $queryData = new QueryData();
74
        $queryData->setQuery('DELETE FROM AccountToUser WHERE accountId = ? AND isEdit = ?');
75
        $queryData->setParams([$id, (int)$isEdit]);
76
        $queryData->setOnErrorMessage(__u('Error al eliminar usuarios asociados a la cuenta'));
77
78
        return $this->db->doQuery($queryData)->getAffectedNumRows();
79
    }
80
81
    /**
82
     * Crear asociación de usuarios con cuentas.
83
     *
84
     * @param AccountRequest $accountRequest
85
     * @param bool           $isEdit
86
     *
87
     * @return bool
88
     * @throws \SP\Core\Exceptions\ConstraintException
89
     * @throws \SP\Core\Exceptions\QueryException
90
     */
91
    public function addByType(AccountRequest $accountRequest, bool $isEdit)
92
    {
93
        $items = $isEdit ? $accountRequest->usersEdit : $accountRequest->usersView;
94
        $values = $this->getParamsFromArray($items, '(?,?,?)');
95
96
        $query = /** @lang SQL */
97
            'INSERT INTO AccountToUser (accountId, userId, isEdit) 
98
              VALUES ' . $values . '
99
              ON DUPLICATE KEY UPDATE isEdit = ' . (int)$isEdit;
100
101
        $queryData = new QueryData();
102
        $queryData->setQuery($query);
103
        $queryData->setOnErrorMessage(__u('Error al actualizar los usuarios de la cuenta'));
104
105
        $params = [];
106
107
        foreach ($items as $user) {
108
            $params[] = $accountRequest->id;
109
            $params[] = $user;
110
            $params[] = (int)$isEdit;
111
        }
112
113
        $queryData->setParams($params);
114
115
        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 documented return type boolean.
Loading history...
116
    }
117
118
    /**
119
     * Eliminar la asociación de grupos con cuentas.
120
     *
121
     * @param int $id con el Id de la cuenta
122
     *
123
     * @return int
124
     * @throws \SP\Core\Exceptions\ConstraintException
125
     * @throws \SP\Core\Exceptions\QueryException
126
     */
127
    public function deleteByAccountId($id)
128
    {
129
        $queryData = new QueryData();
130
        $queryData->setQuery('DELETE FROM AccountToUser WHERE accountId = ?');
131
        $queryData->addParam($id);
132
        $queryData->setOnErrorMessage(__u('Error al eliminar usuarios asociados a la cuenta'));
133
134
        return $this->db->doQuery($queryData)->getAffectedNumRows();
135
    }
136
137
    /**
138
     * Obtiene el listado de usuarios de una cuenta.
139
     *
140
     * @param int $id con el id de la cuenta
141
     *
142
     * @return \SP\Storage\Database\QueryResult
143
     * @throws \SP\Core\Exceptions\ConstraintException
144
     * @throws \SP\Core\Exceptions\QueryException
145
     */
146
    public function getUsersByAccountId($id)
147
    {
148
        $query = /** @lang SQL */
149
            'SELECT `User`.id, `User`.name, `User`.login, AccountToUser.isEdit
150
            FROM AccountToUser
151
            INNER JOIN `User` ON AccountToUser.userId = `User`.id
152
            WHERE AccountToUser.accountId = ?
153
            ORDER BY `User`.name';
154
155
        $queryData = new QueryData();
156
        $queryData->setQuery($query);
157
        $queryData->addParam($id);
158
        $queryData->setMapClassName(ItemData::class);
159
160
        return $this->db->doSelect($queryData);
161
    }
162
}