Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UserService
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
2 / 2
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 findUsers
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
1 / 1
8
1<?php
2
3/**
4 * Nextcloud - cospend
5 *
6 * This file is licensed under the Affero General Public License version 3 or
7 * later. See the COPYING file.
8 *
9 * @author Julien Veyssier
10 * @copyright Julien Veyssier 2019
11 */
12
13namespace OCA\Cospend\Service;
14
15use OCP\DB\QueryBuilder\IQueryBuilder;
16use OCP\IGroupManager;
17use OCP\IDBConnection;
18
19use OCA\Cospend\AppInfo\Application;
20use OCA\Cospend\Db\ProjectMapper;
21
22class UserService {
23
24    /**
25     * @var ProjectMapper
26     */
27    private $projectMapper;
28    /**
29     * @var IGroupManager
30     */
31    private $groupManager;
32    /**
33     * @var IDBConnection
34     */
35    private $dbconnection;
36
37    public function __construct (ProjectMapper $projectMapper,
38                                IGroupManager $groupManager,
39                                IDBConnection $dbconnection) {
40        $this->projectMapper = $projectMapper;
41        $this->groupManager = $groupManager;
42        $this->dbconnection = $dbconnection;
43    }
44
45    public function findUsers($projectid): array {
46        $userIds = [];
47        // get owner with mapper
48        $proj = $this->projectMapper->find($projectid);
49        array_push($userIds, $proj->getUserid());
50
51        // get user shares from project id
52        $qb = $this->dbconnection->getQueryBuilder();
53        $qb->select('userid')
54            ->from('cospend_shares', 's')
55            ->where(
56                $qb->expr()->eq('type', $qb->createNamedParameter(Application::SHARE_TYPES['user'], IQueryBuilder::PARAM_STR))
57            )
58            ->andWhere(
59                $qb->expr()->eq('projectid', $qb->createNamedParameter($projectid, IQueryBuilder::PARAM_STR))
60            );
61        $req = $qb->executeQuery();
62        while ($row = $req->fetch()) {
63            if (!in_array($row['userid'], $userIds)) {
64                array_push($userIds, $row['userid']);
65            }
66        }
67        $req->closeCursor();
68        $qb = $qb->resetQueryParts();
69
70        // get group shares from project id
71        $qb->select('userid')
72            ->from('cospend_shares', 's')
73            ->where(
74                $qb->expr()->eq('type', $qb->createNamedParameter(Application::SHARE_TYPES['group'], IQueryBuilder::PARAM_STR))
75            )
76            ->andWhere(
77                $qb->expr()->eq('projectid', $qb->createNamedParameter($projectid, IQueryBuilder::PARAM_STR))
78            );
79        $req = $qb->executeQuery();
80        $groupIds = [];
81        while ($row = $req->fetch()) {
82            array_push($groupIds, $row['userid']);
83        }
84        $req->closeCursor();
85        $qb->resetQueryParts();
86        // get users of groups
87        foreach ($groupIds as $gid) {
88            $group = $this->groupManager->get($gid);
89            if ($group !== null) {
90                $groupUsers = $group->getUsers();
91                foreach ($groupUsers as $user) {
92                    $uid = $user->getUID();
93                    if (!in_array($uid, $userIds)) {
94                        array_push($userIds, $uid);
95                    }
96                }
97            }
98        }
99
100        return $userIds;
101    }
102
103}