Passed
Push — 3.0 ( 8cdcee...90ba03 )
by Rubén
07:01
created

UserRepository::checkInUse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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\User;
26
27
use SP\Core\Exceptions\ConstraintException;
28
use SP\Core\Exceptions\QueryException;
29
use SP\Core\Exceptions\SPException;
30
use SP\DataModel\ItemSearchData;
31
use SP\DataModel\UserData;
32
use SP\DataModel\UserPreferencesData;
33
use SP\Repositories\DuplicatedItemException;
34
use SP\Repositories\Repository;
35
use SP\Repositories\RepositoryItemInterface;
36
use SP\Repositories\RepositoryItemTrait;
37
use SP\Services\User\UpdatePassRequest;
38
use SP\Storage\Database\QueryData;
39
use SP\Storage\Database\QueryResult;
40
41
/**
42
 * Class UserRepository
43
 *
44
 * @package SP\Repositories\User
45
 */
46
final class UserRepository extends Repository implements RepositoryItemInterface
47
{
48
    use RepositoryItemTrait;
49
50
    /**
51
     * Updates an item
52
     *
53
     * @param UserData $itemData
54
     *
55
     * @return int
56
     * @throws ConstraintException
57
     * @throws QueryException
58
     * @throws DuplicatedItemException
59
     */
60
    public function update($itemData)
61
    {
62
        if ($this->checkDuplicatedOnUpdate($itemData)) {
63
            throw new DuplicatedItemException(__u('Duplicated user login/email'));
64
        }
65
66
        $query = /** @lang SQL */
67
            'UPDATE User SET
68
            `name` = ?,
69
            login = ?,
70
            ssoLogin = ?,
71
            email = ?,
72
            notes = ?,
73
            userGroupId = ?,
74
            userProfileId = ?,
75
            isAdminApp = ?,
76
            isAdminAcc = ?,
77
            isDisabled = ?,
78
            isChangePass = ?,
79
            isLdap = ?,
80
            lastUpdate = NOW()
81
            WHERE id = ? LIMIT 1';
82
83
        $queryData = new QueryData();
84
        $queryData->setQuery($query);
85
        $queryData->setParams([
86
            $itemData->getName(),
87
            $itemData->getLogin(),
88
            $itemData->getSsoLogin(),
89
            $itemData->getEmail(),
90
            $itemData->getNotes(),
91
            $itemData->getUserGroupId(),
92
            $itemData->getUserProfileId(),
93
            $itemData->isAdminApp(),
94
            $itemData->isAdminAcc(),
95
            $itemData->isDisabled(),
96
            $itemData->isChangePass(),
97
            $itemData->isLdap(),
98
            $itemData->getId()
99
        ]);
100
        $queryData->setOnErrorMessage(__u('Error while updating the user'));
101
102
        return $this->db->doQuery($queryData)->getAffectedNumRows();
103
    }
104
105
    /**
106
     * Checks whether the item is duplicated on updating
107
     *
108
     * @param UserData $itemData
109
     *
110
     * @return bool
111
     * @throws ConstraintException
112
     * @throws QueryException
113
     */
114
    public function checkDuplicatedOnUpdate($itemData)
115
    {
116
        $query = /** @lang SQL */
117
            'SELECT id
118
            FROM User
119
            WHERE id <> ? AND (UPPER(login) = UPPER(?) 
120
            OR (UPPER(?) = ssoLogin AND ssoLogin IS NOT NULL AND ssoLogin <> \'\')
121
            OR (UPPER(?) = email AND email IS NOT NULL AND email <> \'\'))';
122
123
        $queryData = new QueryData();
124
        $queryData->setQuery($query);
125
        $queryData->setParams([
126
            $itemData->getId(),
127
            $itemData->getLogin(),
128
            $itemData->getSsoLogin(),
129
            $itemData->getEmail()
130
        ]);
131
132
        return $this->db->doSelect($queryData)->getNumRows() > 0;
133
    }
134
135
    /**
136
     * Updates an user's pass
137
     *
138
     * @param int               $id
139
     * @param UpdatePassRequest $passRequest
140
     *
141
     * @return int
142
     * @throws ConstraintException
143
     * @throws QueryException
144
     */
145
    public function updatePassById($id, UpdatePassRequest $passRequest)
146
    {
147
        $query = /** @lang SQL */
148
            'UPDATE User SET
149
            pass = ?,
150
            hashSalt = \'\',
151
            isChangePass = ?,
152
            isChangedPass = ?,
153
            isMigrate = 0,
154
            lastUpdate = NOW()
155
            WHERE id = ? LIMIT 1';
156
157
        $queryData = new QueryData();
158
        $queryData->setQuery($query);
159
        $queryData->setParams([
160
            $passRequest->getPass(),
161
            $passRequest->getisChangePass(),
162
            $passRequest->getisChangedPass(),
163
            $id
164
        ]);
165
        $queryData->setOnErrorMessage(__u('Error while updating the password'));
166
167
        return $this->db->doQuery($queryData)->getAffectedNumRows();
168
    }
169
170
    /**
171
     * Deletes an item
172
     *
173
     * @param $id
174
     *
175
     * @return int
176
     * @throws ConstraintException
177
     * @throws QueryException
178
     */
179
    public function delete($id)
180
    {
181
        $queryData = new QueryData();
182
        $queryData->setQuery('DELETE FROM User WHERE id = ? LIMIT 1');
183
        $queryData->addParam($id);
184
        $queryData->setOnErrorMessage(__u('Error while deleting the user'));
185
186
        return $this->db->doQuery($queryData)->getAffectedNumRows();
187
    }
188
189
    /**
190
     * Returns the item for given id
191
     *
192
     * @param int $id
193
     *
194
     * @return QueryResult
195
     * @throws QueryException
196
     * @throws ConstraintException
197
     */
198
    public function getById($id)
199
    {
200
        $query = /** @lang SQL */
201
            'SELECT U.id,
202
            U.name,
203
            U.userGroupId,
204
            UG.name AS userGroupName,
205
            U.login,
206
            U.ssoLogin,
207
            U.email,
208
            U.notes,
209
            U.loginCount,
210
            U.userProfileId,
211
            U.lastLogin,
212
            U.lastUpdate,
213
            U.lastUpdateMPass,
214
            U.preferences,
215
            U.pass,
216
            U.hashSalt,
217
            U.mPass,
218
            U.mKey,            
219
            U.isAdminApp,
220
            U.isAdminAcc,
221
            U.isLdap,
222
            U.isDisabled,
223
            U.isChangePass,
224
            U.isChangedPass,
225
            U.isMigrate
226
            FROM User U
227
            INNER JOIN UserGroup UG ON U.userGroupId = UG.id
228
            WHERE U.id = ? LIMIT 1';
229
230
        $queryData = new QueryData();
231
        $queryData->setMapClassName(UserData::class);
232
        $queryData->setQuery($query);
233
        $queryData->addParam($id);
234
        $queryData->setOnErrorMessage(__u('Error while retrieving the user\'s data'));
235
236
        return $this->db->doSelect($queryData);
237
    }
238
239
    /**
240
     * Returns all the items
241
     *
242
     * @return UserData[]
243
     * @throws QueryException
244
     * @throws ConstraintException
245
     */
246
    public function getAll()
247
    {
248
        $query = /** @lang SQL */
249
            'SELECT U.id,
250
            U.name,
251
            U.userGroupId,
252
            U.login,
253
            U.ssoLogin,
254
            U.email,
255
            U.notes,
256
            U.loginCount,
257
            U.userProfileId,
258
            U.lastLogin,
259
            U.lastUpdate,
260
            U.lastUpdateMPass,
261
            U.preferences,
262
            U.pass,
263
            U.hashSalt,
264
            U.mPass,
265
            U.mKey,            
266
            U.isAdminApp,
267
            U.isAdminAcc,
268
            U.isLdap,
269
            U.isDisabled,
270
            U.isChangePass,
271
            U.isChangedPass,
272
            U.isMigrate
273
            FROM User U';
274
275
        $queryData = new QueryData();
276
        $queryData->setMapClassName(UserData::class);
277
        $queryData->setQuery($query);
278
279
        return $this->db->doSelect($queryData)->getDataAsArray();
280
    }
281
282
    /**
283
     * Returns all the items for given ids
284
     *
285
     * @param array $ids
286
     *
287
     * @return UserData[]
288
     * @throws QueryException
289
     * @throws ConstraintException
290
     */
291
    public function getByIdBatch(array $ids)
292
    {
293
        if (empty($ids)) {
294
            return [];
295
        }
296
297
        $query = /** @lang SQL */
298
            'SELECT U.id,
299
            U.name,
300
            U.userGroupId,
301
            UG.name AS userGroupName,
302
            U.login,
303
            U.ssoLogin,
304
            U.email,
305
            U.notes,
306
            U.loginCount,
307
            U.userProfileId,
308
            U.lastLogin,
309
            U.lastUpdate,
310
            U.lastUpdateMPass,
311
            U.preferences,
312
            U.pass,
313
            U.hashSalt,
314
            U.mPass,
315
            U.mKey,            
316
            U.isAdminApp,
317
            U.isAdminAcc,
318
            U.isLdap,
319
            U.isDisabled,
320
            U.isChangePass,
321
            U.isChangedPass,
322
            U.isMigrate
323
            FROM User U
324
            INNER JOIN UserGroup UG ON U.userGroupId = UG.id
325
            WHERE U.id IN (' . $this->getParamsFromArray($ids) . ')';
326
327
        $queryData = new QueryData();
328
        $queryData->setMapClassName(UserData::class);
329
        $queryData->setQuery($query);
330
        $queryData->setParams($ids);
331
332
        return $this->db->doSelect($queryData)->getDataAsArray();
333
    }
334
335
    /**
336
     * Deletes all the items for given ids
337
     *
338
     * @param array $ids
339
     *
340
     * @return int
341
     * @throws ConstraintException
342
     * @throws QueryException
343
     */
344
    public function deleteByIdBatch(array $ids)
345
    {
346
        if (empty($ids)) {
347
            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...
348
        }
349
350
        $queryData = new QueryData();
351
        $queryData->setQuery('DELETE FROM User WHERE id IN (' . $this->getParamsFromArray($ids) . ')');
352
        $queryData->setParams($ids);
353
        $queryData->setOnErrorMessage(__u('Error while deleting the users'));
354
355
        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...
356
    }
357
358
    /**
359
     * Checks whether the item is in use or not
360
     *
361
     * @param $id int
362
     *
363
     * @return void
364
     */
365
    public function checkInUse($id)
366
    {
367
        throw new \RuntimeException('Not implemented');
368
    }
369
370
    /**
371
     * Searches for items by a given filter
372
     *
373
     * @param ItemSearchData $itemSearchData
374
     *
375
     * @return QueryResult
376
     * @throws QueryException
377
     * @throws ConstraintException
378
     */
379
    public function search(ItemSearchData $itemSearchData)
380
    {
381
        $queryData = new QueryData();
382
        $queryData->setSelect('User.id,
383
            User.name, 
384
            User.login,
385
            UserProfile.name AS userProfileName,
386
            UserGroup.name AS userGroupName,
387
            User.isAdminApp,
388
            User.isAdminAcc,
389
            User.isLdap,
390
            User.isDisabled,
391
            User.isChangePass');
392
        $queryData->setFrom('User
393
        INNER JOIN UserProfile ON User.userProfileId = UserProfile.id 
394
        INNER JOIN UserGroup ON User.userGroupId = UserGroup.id');
395
        $queryData->setOrder('User.name');
396
397
        if ($itemSearchData->getSeachString() !== '') {
398
            if ($this->context->getUserData()->getIsAdminApp()) {
399
                $queryData->setWhere('User.name LIKE ? OR User.login LIKE ?');
400
            } else {
401
                $queryData->setWhere('User.name LIKE ? OR User.login LIKE ? AND User.isAdminApp = 0');
402
            }
403
404
            $search = '%' . $itemSearchData->getSeachString() . '%';
405
            $queryData->addParam($search);
406
            $queryData->addParam($search);
407
        } elseif (!$this->context->getUserData()->getIsAdminApp()) {
408
            $queryData->setWhere('User.isAdminApp = 0');
409
        }
410
411
        $queryData->setLimit(
412
            '?,?',
413
            [$itemSearchData->getLimitStart(), $itemSearchData->getLimitCount()]
414
        );
415
416
        return $this->db->doSelect($queryData, true);
417
    }
418
419
    /**
420
     * Creates an item
421
     *
422
     * @param UserData $itemData
423
     *
424
     * @return int
425
     * @throws SPException
426
     */
427
    public function create($itemData)
428
    {
429
        if ($this->checkDuplicatedOnAdd($itemData)) {
430
            throw new DuplicatedItemException(__u('Duplicated user login/email'));
431
        }
432
433
        $query = /** @lang SQL */
434
            'INSERT INTO User SET
435
            `name` = ?,
436
            login = ?,
437
            ssoLogin = ?,
438
            email = ?,
439
            notes = ?,
440
            userGroupId = ?,
441
            userProfileId = ?,
442
            mPass = ?,
443
            mKey = ?,
444
            lastUpdateMPass = ?,
445
            isAdminApp = ?,
446
            isAdminAcc = ?,
447
            isDisabled = ?,
448
            isChangePass = ?,
449
            isLdap = ?,
450
            pass = ?,
451
            hashSalt = \'\'';
452
453
        $queryData = new QueryData();
454
        $queryData->setQuery($query);
455
        $queryData->setParams([
456
            $itemData->getName(),
457
            $itemData->getLogin(),
458
            $itemData->getSsoLogin(),
459
            $itemData->getEmail(),
460
            $itemData->getNotes(),
461
            $itemData->getUserGroupId(),
462
            $itemData->getUserProfileId(),
463
            $itemData->getMPass(),
464
            $itemData->getMKey(),
465
            $itemData->getLastUpdateMPass(),
466
            $itemData->isAdminApp(),
467
            $itemData->isAdminAcc(),
468
            $itemData->isDisabled(),
469
            $itemData->isChangePass(),
470
            $itemData->isLdap(),
471
            $itemData->getPass()
472
473
        ]);
474
        $queryData->setOnErrorMessage(__u('Error while creating the user'));
475
476
        return $this->db->doQuery($queryData)->getLastId();
477
    }
478
479
    /**
480
     * Checks whether the item is duplicated on adding
481
     *
482
     * @param UserData $itemData
483
     *
484
     * @return bool
485
     * @throws ConstraintException
486
     * @throws QueryException
487
     */
488
    public function checkDuplicatedOnAdd($itemData)
489
    {
490
        $query = /** @lang SQL */
491
            'SELECT id
492
            FROM User
493
            WHERE UPPER(login) = UPPER(?) 
494
            OR (UPPER(?) = ssoLogin AND ssoLogin IS NOT NULL AND ssoLogin <> \'\')
495
            OR (UPPER(?) = email AND email IS NOT NULL AND email <> \'\')';
496
497
        $queryData = new QueryData();
498
        $queryData->setQuery($query);
499
        $queryData->setParams([
500
            $itemData->getLogin(),
501
            $itemData->getSsoLogin(),
502
            $itemData->getEmail()
503
        ]);
504
505
        return $this->db->doSelect($queryData)->getNumRows() > 0;
506
    }
507
508
    /**
509
     * @param $login string
510
     *
511
     * @return QueryResult
512
     * @throws ConstraintException
513
     * @throws QueryException
514
     */
515
    public function getByLogin($login)
516
    {
517
        $query = /** @lang SQL */
518
            'SELECT U.id,
519
            U.name,
520
            U.userGroupId,
521
            UG.name AS userGroupName,
522
            U.login,
523
            U.ssoLogin,
524
            U.email,
525
            U.notes,
526
            U.loginCount,
527
            U.userProfileId,
528
            U.lastLogin,
529
            U.lastUpdate,
530
            U.lastUpdateMPass,
531
            U.preferences,
532
            U.pass,
533
            U.hashSalt,
534
            U.mPass,
535
            U.mKey,            
536
            U.isAdminApp,
537
            U.isAdminAcc,
538
            U.isLdap,
539
            U.isDisabled,
540
            U.isChangePass,
541
            U.isChangedPass,
542
            U.isMigrate
543
            FROM User U
544
            INNER JOIN UserGroup UG ON U.userGroupId = UG.id
545
            WHERE U.login = ? OR U.ssoLogin = ? LIMIT 1';
546
547
        $queryData = new QueryData();
548
        $queryData->setMapClassName(UserData::class);
549
        $queryData->setQuery($query);
550
        $queryData->setParams([$login, $login]);
551
        $queryData->setOnErrorMessage(__u('Error while retrieving the user\'s data'));
552
553
        return $this->db->doSelect($queryData);
554
    }
555
556
    /**
557
     * Returns items' basic information
558
     *
559
     * @return QueryResult
560
     * @throws ConstraintException
561
     * @throws QueryException
562
     */
563
    public function getBasicInfo()
564
    {
565
        $query = /** @lang SQL */
566
            'SELECT U.id,
567
            U.name,
568
            U.login,
569
            U.email,
570
            U.userGroupId,
571
            U.userProfileId,
572
            U.isAdminApp,
573
            U.isAdminAcc,
574
            U.isLdap,
575
            U.isDisabled
576
            FROM User U';
577
578
        $queryData = new QueryData();
579
        $queryData->setMapClassName(UserData::class);
580
        $queryData->setQuery($query);
581
582
        return $this->db->doSelect($queryData);
583
    }
584
585
    /**
586
     * Updates user's master password
587
     *
588
     * @param $id
589
     * @param $pass
590
     * @param $key
591
     *
592
     * @return int
593
     * @throws ConstraintException
594
     * @throws QueryException
595
     */
596
    public function updateMasterPassById($id, $pass, $key)
597
    {
598
        $query = /** @lang SQL */
599
            'UPDATE User SET 
600
              mPass = ?,
601
              mKey = ?,
602
              lastUpdateMPass = UNIX_TIMESTAMP(),
603
              isMigrate = 0,
604
              isChangedPass = 0 
605
              WHERE id = ? LIMIT 1';
606
607
        $queryData = new QueryData();
608
        $queryData->setQuery($query);
609
        $queryData->setParams([$pass, $key, $id]);
610
611
        return $this->db->doQuery($queryData)->getAffectedNumRows();
612
    }
613
614
    /**
615
     * Actualiza el último inicio de sesión del usuario en la BBDD.
616
     *
617
     * @param $id int El id del usuario
618
     *
619
     * @return int
620
     * @throws QueryException
621
     * @throws ConstraintException
622
     */
623
    public function updateLastLoginById($id)
624
    {
625
        $queryData = new QueryData();
626
        $queryData->setQuery('UPDATE User SET lastLogin = NOW(), loginCount = loginCount + 1 WHERE id = ? LIMIT 1');
627
        $queryData->addParam($id);
628
629
        return $this->db->doQuery($queryData)->getAffectedNumRows();
630
    }
631
632
    /**
633
     * @param $login
634
     *
635
     * @return bool
636
     * @throws ConstraintException
637
     * @throws QueryException
638
     */
639
    public function checkExistsByLogin($login)
640
    {
641
        $queryData = new QueryData();
642
        $queryData->setQuery('SELECT id FROM User WHERE UPPER(login) = UPPER(?) OR UPPER(ssoLogin) = UPPER(?) LIMIT 1');
643
        $queryData->setParams([$login, $login]);
644
645
        return $this->db->doSelect($queryData)->getNumRows() > 0;
646
    }
647
648
    /**
649
     * @param UserData $itemData
650
     *
651
     * @return int
652
     * @throws ConstraintException
653
     * @throws QueryException
654
     */
655
    public function updateOnLogin(UserData $itemData)
656
    {
657
        $query = 'UPDATE User SET 
658
            pass = ?,
659
            hashSalt = \'\',
660
            `name` = ?,
661
            email = ?,
662
            lastUpdate = NOW(),
663
            lastLogin = NOW(),
664
            isLdap = ? 
665
            WHERE UPPER(login) = UPPER(?) OR UPPER(ssoLogin) = UPPER(?) LIMIT 1';
666
667
        $queryData = new QueryData();
668
        $queryData->setQuery($query);
669
        $queryData->setParams([
670
            $itemData->getPass(),
671
            $itemData->getName(),
672
            $itemData->getEmail(),
673
            $itemData->isLdap(),
674
            $itemData->getLogin(),
675
            $itemData->getLogin()
676
        ]);
677
        $queryData->setOnErrorMessage(__u('Error while updating the user'));
678
679
        return $this->db->doQuery($queryData)->getAffectedNumRows();
680
    }
681
682
    /**
683
     * Updates an user's pass
684
     *
685
     * @param int                 $id
686
     * @param UserPreferencesData $userPreferencesData
687
     *
688
     * @return int
689
     * @throws ConstraintException
690
     * @throws QueryException
691
     */
692
    public function updatePreferencesById($id, UserPreferencesData $userPreferencesData)
693
    {
694
        $queryData = new QueryData();
695
        $queryData->setQuery('UPDATE User SET preferences = ? WHERE id = ? LIMIT 1');
696
        $queryData->setParams([serialize($userPreferencesData), $id]);
697
        $queryData->setOnErrorMessage(__u('Error while updating the preferences'));
698
699
        return $this->db->doQuery($queryData)->getAffectedNumRows();
700
    }
701
702
    /**
703
     * Obtener el email de los usuarios de un grupo
704
     *
705
     * @param $groupId
706
     *
707
     * @return QueryResult
708
     * @throws ConstraintException
709
     * @throws QueryException
710
     */
711
    public function getUserEmailForGroup($groupId)
712
    {
713
        $query = /** @lang SQL */
714
            'SELECT U.id, U.login, U.name, U.email 
715
            FROM User U
716
            INNER JOIN UserGroup UG ON U.userGroupId = UG.id
717
            LEFT JOIN UserToUserGroup UUG ON U.id = UUG.userId
718
            WHERE U.email IS NOT NULL 
719
            AND U.userGroupId = ? OR UUG.userGroupId = ?
720
            AND U.isDisabled = 0
721
            ORDER BY U.login';
722
723
        $queryData = new QueryData();
724
        $queryData->setQuery($query);
725
        $queryData->setParams([$groupId, $groupId]);
726
727
        return $this->db->doSelect($queryData);
728
    }
729
730
    /**
731
     * Obtener el email de los usuarios
732
     *
733
     * @return QueryResult
734
     * @throws ConstraintException
735
     * @throws QueryException
736
     *
737
     * @TODO create unit test
738
     */
739
    public function getUserEmail()
740
    {
741
        $query = /** @lang SQL */
742
            'SELECT id, login, `name`, email 
743
            FROM User
744
            WHERE email IS NOT NULL 
745
            AND isDisabled = 0
746
            ORDER BY login';
747
748
        $queryData = new QueryData();
749
        $queryData->setQuery($query);
750
751
        return $this->db->doSelect($queryData);
752
    }
753
754
    /**
755
     * Returns the usage of the given user's id
756
     *
757
     * @param int $id
758
     *
759
     * @return QueryResult
760
     * @throws ConstraintException
761
     * @throws QueryException
762
     */
763
    public function getUsageForUser($id)
764
    {
765
        $query = 'SELECT * FROM (SELECT
766
                  A.id,
767
                  CONCAT(A.name, " (", C.name, ")") AS name,
768
                  \'Account\'                         AS ref
769
                FROM Account A
770
                  INNER JOIN Client C on A.clientId = C.id
771
                WHERE A.userId = ? OR A.userEditId = ?
772
                UNION ALL
773
                SELECT
774
                  AU.accountId                        AS id,
775
                  CONCAT(A.name, " (", C.name, ")") AS name,
776
                  \'Account\'                           AS ref
777
                FROM AccountToUser AU
778
                  INNER JOIN Account A on AU.accountId = A.id
779
                  INNER JOIN Client C on A.clientId = C.id
780
                WHERE AU.userId = ?
781
                UNION ALL
782
                SELECT
783
                  UUG.userGroupId AS id,
784
                  G.name,
785
                  \'UserGroup\'     AS ref
786
                FROM
787
                  UserToUserGroup UUG
788
                  INNER JOIN UserGroup G on UUG.userGroupId = G.id
789
                WHERE UUG.userId = ?
790
                UNION ALL
791
                SELECT
792
                  PL.id,
793
                  CONCAT(A.name, " (", C.name, ")") AS name,
794
                  \'PublicLink\' AS ref
795
                FROM
796
                  PublicLink PL
797
                  INNER JOIN Account A ON A.id = PL.itemId
798
                  INNER JOIN Client C on A.clientId = C.id
799
                WHERE PL.userId = ?) Items
800
                ORDER BY Items.ref';
801
802
        $queryData = new QueryData();
803
        $queryData->setQuery($query);
804
        $queryData->setParams(array_fill(0, 5, (int)$id));
805
806
        return $this->db->doSelect($queryData);
807
    }
808
}