UserRepository::updateOnLogin()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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