GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

MediumPermission::getGroupName()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 25

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 25
loc 25
rs 9.52
c 0
b 0
f 0
1
<?php
2
3
namespace phpMyFAQ\Permission;
4
5
/**
6
 * The medium permission class provides group rights.
7
 *
8
 * 
9
 *
10
 * This Source Code Form is subject to the terms of the Mozilla Public License,
11
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
12
 * obtain one at http://mozilla.org/MPL/2.0/.
13
 *
14
 * @package phpMyFAQ
15
 * @author Lars Tiedemann <[email protected]>
16
 * @copyright 2005-2019 phpMyFAQ Team
17
 * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
18
 * @link https://www.phpmyfaq.de
19
 * @since 2005-09-17
20
 */
21
22
use phpMyFAQ\Configuration;
23
use phpMyFAQ\Db;
24
use phpMyFAQ\User\CurrentUser;
25
26
if (!defined('IS_VALID_PHPMYFAQ')) {
27
    exit();
28
}
29
30
/**
31
 * PMF_Perm_Medium.
32
 *
33
 * @package phpMyFAQ
34
 * @author Lars Tiedemann <[email protected]>
35
 * @copyright 2005-2019 phpMyFAQ Team
36
 * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
37
 * @link https://www.phpmyfaq.de
38
 * @since 2005-09-17
39
 */
40
class MediumPermission extends BasicPermission
41
{
42
    /**
43
     * Default data for new groups.
44
     *
45
     * @var array
46
     */
47
    public $defaultGroupData = [
48
        'name' => 'DEFAULT_GROUP',
49
        'description' => 'Short group description.',
50
        'auto_join' => false,
51
    ];
52
53
    /**
54
     * Constructor.
55
     *
56
     * @param Configuration $config
57
     */
58
    public function __construct(Configuration $config)
59
    {
60
        parent::__construct($config);
61
    }
62
63
    /**
64
     * Returns true if the group specified by $groupId owns the
65
     * right given by $rightId, otherwise false.
66
     *
67
     * @param int $groupId Group ID
68
     * @param int $rightId Right ID
69
     * 
70
     * @return bool
71
     */
72
    public function checkGroupRight($groupId, $rightId)
73
    {
74
        // check input
75
        if ($rightId <= 0 || $groupId <= 0 || !is_numeric($rightId) || !is_numeric($groupId)) {
76
            return false;
77
        }
78
79
        // check right
80
        $select = sprintf('
81
            SELECT
82
                fr.right_id AS right_id
83
            FROM
84
                %sfaqright fr,
85
                %sfaqgroup_right fgr,
86
                %sfaqgroup fg
87
            WHERE
88
                fr.right_id = %d AND
89
                fr.right_id = fgr.right_id AND
90
                fg.group_id = fgr.group_id AND
91
                fg.group_id = %d',
92
            Db::getTablePrefix(),
93
            Db::getTablePrefix(),
94
            Db::getTablePrefix(),
95
            $rightId,
96
            $groupId
97
        );
98
99
        $res = $this->config->getDb()->query($select);
100
        if ($this->config->getDb()->numRows($res) >= 1) {
101
            return true;
102
        }
103
104
        return false;
105
    }
106
107
    /**
108
     * Returns an array that contains the right-IDs of all
109
     * group-rights the group $groupId owns.
110
     *
111
     * @param int $groupId Group ID
112
     * 
113
     * @return array
114
     */
115 View Code Duplication
    public function getGroupRights($groupId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117
        if ($groupId <= 0 || !is_numeric($groupId)) {
118
            return [];
119
        }
120
        // check right
121
        $select = sprintf('
122
            SELECT
123
                fr.right_id AS right_id
124
            FROM
125
                %sfaqright fr,
126
                %sfaqgroup_right fgr,
127
                %sfaqgroup fg
128
            WHERE
129
                fg.group_id = %d AND
130
                fg.group_id = fgr.group_id AND
131
                fr.right_id = fgr.right_id',
132
            Db::getTablePrefix(),
133
            Db::getTablePrefix(),
134
            Db::getTablePrefix(),
135
            $groupId
136
        );
137
138
        $res = $this->config->getDb()->query($select);
139
        $result = [];
140
        while ($row = $this->config->getDb()->fetchArray($res)) {
141
            $result[] = $row['right_id'];
142
        }
143
144
        return $result;
145
    }
146
147
    /**
148
     * Returns true, if the user given by $userId owns the right
149
     * specified by $right. It does not matter if the user owns this
150
     * right as a user-right or because of a group-membership.
151
     * The parameter $right may be a right-ID (recommended for
152
     * performance) or a right-name.
153
     *
154
     * @param int   $userId Group ID
155
     * @param mixed $right  Rights
156
     *
157
     * @return bool
158
     */
159 View Code Duplication
    public function checkRight($userId, $right)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    {
161
        $user = new CurrentUser($this->config);
162
        $user->getUserById($userId);
163
164
        if ($user->isSuperAdmin()) {
165
            return true;
166
        }
167
168
        // get right id
169
        if (!is_numeric($right) && is_string($right)) {
170
            $right = $this->getRightId($right);
171
        }
172
173
        // check user right and group right
174
        if (
175
            $this->checkUserGroupRight($userId, $right) ||
176
            $this->checkUserRight($userId, $right)
177
        ) {
178
            return true;
179
        }
180
181
        return false;
182
    }
183
184
    /**
185
     * Grants the group given by $groupId the right specified by
186
     * $rightId.
187
     *
188
     * @param int $groupId Group ID
189
     * @param int $rightId Right ID
190
     * 
191
     * @return bool
192
     */
193 View Code Duplication
    public function grantGroupRight($groupId, $rightId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
194
    {
195
        // check input
196
        if ($rightId <= 0 || $groupId <= 0 || !is_numeric($rightId) || !is_numeric($groupId)) {
197
            return false;
198
        }
199
200
        // is right for users?
201
        $right_data = $this->getRightData($rightId);
202
        if (!$right_data['for_groups']) {
203
            return false;
204
        }
205
206
        // grant right
207
        $insert = sprintf('
208
            INSERT INTO
209
                %sfaqgroup_right
210
            (group_id, right_id)
211
                VALUES
212
            (%d, %d)',
213
            Db::getTablePrefix(),
214
            $groupId,
215
            $rightId
216
        );
217
218
        $res = $this->config->getDb()->query($insert);
219
        if (!$res) {
220
            return false;
221
        }
222
223
        return true;
224
    }
225
226
    /**
227
     * Refuses the group given by $groupId the right specified by
228
     * $rightId.
229
     *
230
     * @param int $groupId Group ID
231
     * @param int $rightId Right ID
232
     *
233
     * @return bool
234
     */
235 View Code Duplication
    public function refuseGroupRight($groupId, $rightId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
236
    {
237
        // check input
238
        if ($rightId <= 0 || $groupId <= 0 || !is_numeric($rightId) || !is_numeric($groupId)) {
239
            return false;
240
        }
241
242
        // grant right
243
        $delete = sprintf('
244
            DELETE FROM
245
                %sfaqgroup_right
246
            WHERE
247
                group_id = %d AND
248
                right_id = %d',
249
            Db::getTablePrefix(),
250
            $groupId,
251
            $rightId
252
        );
253
254
        $res = $this->config->getDb()->query($delete);
255
        if (!$res) {
256
            return false;
257
        }
258
259
        return true;
260
    }
261
262
    /**
263
     * Adds a new group to the database and returns the ID of the
264
     * new group. The associative array $groupData contains the
265
     * data for the new group.
266
     *
267
     * @param array $groupData Array of group data
268
     *
269
     * @return int
270
     */
271 View Code Duplication
    public function addGroup(Array $groupData)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
272
    {
273
        // check if group already exists
274
        if ($this->getGroupId($groupData['name']) > 0) {
275
            return 0;
276
        }
277
278
        $nextId = $this->config->getDb()->nextId(Db::getTablePrefix().'faqgroup', 'group_id');
279
        $groupData = $this->checkGroupData($groupData);
280
        $insert = sprintf("
281
            INSERT INTO
282
                %sfaqgroup
283
            (group_id, name, description, auto_join)
284
                VALUES
285
            (%d, '%s', '%s', '%s')",
286
            Db::getTablePrefix(),
287
            $nextId,
288
            $groupData['name'],
289
            $groupData['description'],
290
            (int)$groupData['auto_join']
291
        );
292
293
        $res = $this->config->getDb()->query($insert);
294
        if (!$res) {
295
            return 0;
296
        }
297
298
        return $nextId;
299
    }
300
301
    /**
302
     * Changes the group data of the given group.
303
     *
304
     * @param int   $groupId   Group ID
305
     * @param array $groupData Array of group data
306
     *
307
     * @return bool
308
     */
309 View Code Duplication
    public function changeGroup($groupId, Array $groupData)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
310
    {
311
        $checkedData = $this->checkGroupData($groupData);
312
        $set = '';
313
        $comma = '';
314
315
        foreach ($groupData as $key => $val) {
316
            $set  .= $comma.$key." = '".$this->config->getDb()->escape($checkedData[$key])."'";
317
            $comma = ",\n                ";
318
        }
319
320
        $update = sprintf('
321
            UPDATE
322
                %sfaqgroup
323
            SET
324
                %s
325
            WHERE
326
                group_id = %d',
327
            Db::getTablePrefix(),
328
            $set,
329
            $groupId
330
        );
331
332
        $res = $this->config->getDb()->query($update);
333
334
        if (!$res) {
335
            return false;
336
        }
337
338
        return true;
339
    }
340
341
    /**
342
     * Removes the group given by $groupId from the database.
343
     * Returns true on success, otherwise false.
344
     *
345
     * @param int $groupId Group ID
346
     *
347
     * @return bool
348
     */
349 View Code Duplication
    public function deleteGroup($groupId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
350
    {
351
        if ($groupId <= 0 || !is_numeric($groupId)) {
352
            return false;
353
        }
354
355
        $delete = sprintf('
356
            DELETE FROM
357
                %sfaqgroup
358
            WHERE
359
                group_id = %d',
360
            Db::getTablePrefix(),
361
            $groupId
362
        );
363
364
        $res = $this->config->getDb()->query($delete);
365
        if (!$res) {
366
            return false;
367
        }
368
369
        $delete = sprintf('
370
            DELETE FROM
371
                %sfaquser_group
372
            WHERE
373
                group_id = %d',
374
            Db::getTablePrefix(),
375
            $groupId
376
        );
377
378
        $res = $this->config->getDb()->query($delete);
379
        if (!$res) {
380
            return false;
381
        }
382
383
        $delete = sprintf('
384
            DELETE FROM
385
                %sfaqgroup_right
386
            WHERE
387
                group_id = %d',
388
            Db::getTablePrefix(),
389
            $groupId
390
        );
391
392
        $res = $this->config->getDb()->query($delete);
393
        if (!$res) {
394
            return false;
395
        }
396
397
        return true;
398
    }
399
400
    /**
401
     * Returns true if the user given by $userId is a member of
402
     * the group specified by $groupId, otherwise false.
403
     *
404
     * @param int $userId  User ID
405
     * @param int $groupId Group ID
406
     *
407
     * @return bool
408
     */
409 View Code Duplication
    public function isGroupMember($userId, $groupId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
410
    {
411
        if ($userId <= 0 || $groupId <= 0 || !is_numeric($userId) || !is_numeric($groupId)) {
412
            return false;
413
        }
414
415
        $select = sprintf('
416
            SELECT
417
                fu.user_id AS user_id
418
            FROM
419
                %sfaquser fu,
420
                %sfaquser_group fug,
421
                %sfaqgroup fg
422
            WHERE
423
                fu.user_id  = %d AND
424
                fu.user_id  = fug.user_id AND
425
                fg.group_id = fug.group_id AND
426
                fg.group_id = %d',
427
            Db::getTablePrefix(),
428
            Db::getTablePrefix(),
429
            Db::getTablePrefix(),
430
            $userId,
431
            $groupId
432
        );
433
434
        $res = $this->config->getDb()->query($select);
435
        if ($this->config->getDb()->numRows($res) == 1) {
436
            return true;
437
        }
438
439
        return false;
440
    }
441
442
    /**
443
     * Returns an array that contains the user-IDs of all members
444
     * of the group $groupId.
445
     *
446
     * @param int $groupId Group ID
447
     *
448
     * @return array
449
     */
450 View Code Duplication
    public function getGroupMembers($groupId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
451
    {
452
        if ($groupId <= 0 || !is_numeric($groupId)) {
453
            return [];
454
        }
455
456
        $select = sprintf('
457
            SELECT
458
                fu.user_id AS user_id
459
            FROM
460
                %sfaquser fu,
461
                %sfaquser_group fug,
462
                %sfaqgroup fg
463
            WHERE
464
                fg.group_id = %d AND
465
                fg.group_id = fug.group_id AND
466
                fu.user_id  = fug.user_id',
467
            Db::getTablePrefix(),
468
            Db::getTablePrefix(),
469
            Db::getTablePrefix(),
470
            $groupId
471
        );
472
473
        $res = $this->config->getDb()->query($select);
474
        $result = [];
475
        while ($row = $this->config->getDb()->fetchArray($res)) {
476
            $result[] = $row['user_id'];
477
        }
478
479
        return $result;
480
    }
481
482
    /**
483
     * Adds a new member $userId to the group $groupId.
484
     * Returns true on success, otherwise false.
485
     *
486
     * @param int $userId  User ID
487
     * @param int $groupId Group ID
488
     *
489
     * @return bool
490
     */
491 View Code Duplication
    public function addToGroup($userId, $groupId)
492
    {
493
        if ($userId <= 0 || $groupId <= 0 || !is_numeric($userId) || !is_numeric($groupId)) {
494
            return false;
495
        }
496
497
        if (!$this->getGroupData($groupId)) {
498
            return false;
499
        }
500
501
        // add user to group
502
        $insert = sprintf('
503
            INSERT INTO
504
                %sfaquser_group
505
            (user_id, group_id)
506
               VALUES
507
            (%d, %d)',
508
            Db::getTablePrefix(),
509
            $userId,
510
            $groupId
511
        );
512
513
        $res = $this->config->getDb()->query($insert);
514
        if (!$res) {
515
            return false;
516
        }
517
518
        return true;
519
    }
520
521
    /**
522
     * Removes a user $userId from the group $groupId.
523
     * Returns true on success, otherwise false.
524
     *
525
     * @param int $userId  User ID
526
     * @param int $groupId Group ID
527
     *
528
     * @return bool
529
     */
530 View Code Duplication
    public function removeFromGroup($userId, $groupId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
531
    {
532
        if ($userId <= 0 || $groupId <= 0 || !is_numeric($userId) || !is_numeric($groupId)) {
533
            return false;
534
        }
535
536
        $delete = sprintf('
537
            DELETE FROM
538
                %sfaquser_group
539
            WHERE
540
                user_id  = %d AND
541
                group_id = %d',
542
            Db::getTablePrefix(),
543
            $userId,
544
            $groupId);
545
546
        $res = $this->config->getDb()->query($delete);
547
        if (!$res) {
548
            return false;
549
        }
550
551
        return true;
552
    }
553
554
    /**
555
     * Returns the ID of the group that has the name $name. Returns
556
     * 0 if the group-name cannot be found.
557
     *
558
     * @param string $name Group name
559
     *
560
     * @return int
561
     */
562
    public function getGroupId($name)
563
    {
564
        $select = sprintf("
565
            SELECT
566
                group_id
567
            FROM
568
                %sfaqgroup
569
            WHERE
570
                name = '%s'",
571
            Db::getTablePrefix(),
572
            $this->config->getDb()->escape($name)
573
        );
574
575
        $res = $this->config->getDb()->query($select);
576
        if ($this->config->getDb()->numRows($res) != 1) {
577
            return 0;
578
        }
579
        $row = $this->config->getDb()->fetchArray($res);
580
581
        return $row['group_id'];
582
    }
583
584
    /**
585
     * Returns an associative array with the group-data of the group
586
     * $groupId.
587
     *
588
     * @param int $groupId Group ID
589
     *
590
     * @return array
591
     */
592
    public function getGroupData($groupId)
593
    {
594
        if ($groupId <= 0 || !is_numeric($groupId)) {
595
            return [];
596
        }
597
598
        $select = sprintf('
599
            SELECT
600
                group_id,
601
                name,
602
                description,
603
                auto_join
604
            FROM
605
                %sfaqgroup
606
            WHERE
607
                group_id = %d',
608
            Db::getTablePrefix(),
609
            $groupId
610
        );
611
612
        $res = $this->config->getDb()->query($select);
613
        if ($this->config->getDb()->numRows($res) != 1) {
614
            return [];
615
        }
616
617
        return $this->config->getDb()->fetchArray($res);
618
    }
619
620
    /**
621
     * Returns an array that contains the IDs of all groups in which
622
     * the user $userId is a member.
623
     *
624
     * @param int $userId User ID
625
     *
626
     * @return array
627
     */
628
    public function getUserGroups($userId)
629
    {
630
        if ($userId <= 0 || !is_numeric($userId)) {
631
            return [-1];
632
        }
633
634
        $select = sprintf('
635
            SELECT
636
                fg.group_id AS group_id
637
            FROM
638
                %sfaquser fu,
639
                %sfaquser_group fug,
640
                %sfaqgroup fg
641
            WHERE
642
                fu.user_id  = %d AND
643
                fu.user_id  = fug.user_id AND
644
                fg.group_id = fug.group_id',
645
            Db::getTablePrefix(),
646
            Db::getTablePrefix(),
647
            Db::getTablePrefix(),
648
            $userId
649
        );
650
651
        $res = $this->config->getDb()->query($select);
652
        $result = array(-1);
653
        while ($row = $this->config->getDb()->fetchArray($res)) {
654
            $result[] = $row['group_id'];
655
        }
656
657
        return $result;
658
    }
659
660
    /**
661
     * Returns an array with the IDs of all groups stored in the
662
     * database if no user ID is passed.
663
     * @param int userId 
664
     * @return array
665
     */
666
    public function getAllGroups($userId = 1)
667
    {
668
        $select = sprintf('
669
            SELECT
670
                group_id
671
            FROM
672
                %sfaqgroup',
673
            Db::getTablePrefix()
674
        );
675
        if ($userId != 1) {
676
            $select = sprintf('
677
                SELECT
678
                    fg.group_id
679
                FROM
680
                    %sfaqgroup fg
681
                LEFT JOIN
682
                    %sfaquser_group fug ON fg.group_id=fug.group_id
683
                WHERE
684
                    fug.user_id = %d',
685
                Db::getTablePrefix(),
686
                Db::getTablePrefix(),
687
                $userId
688
            );
689
        }
690
691
        $res = $this->config->getDb()->query($select);
692
        $result = [];
693
        while ($row = $this->config->getDb()->fetchArray($res)) {
694
            $result[] = $row['group_id'];
695
        }
696
697
        return $result;
698
    }
699
700
    /**
701
     * Get all groups in <option> tags.
702
     *
703
     * @todo Move into the Helper class
704
     *
705
     * @param array $groups Selected groups
706
     * @param integer $userId
707
     * @return string
708
     */
709
    public function getAllGroupsOptions(Array $groups, $userId = 1)
710
    {
711
        $options = '';
712
        $allGroups = $this->getAllGroups($userId);
713
714
        foreach ($allGroups as $groupId) {
715
            if (-1 != $groupId) {
716
                $options .= sprintf('<option value="%d"%s>%s</option>',
717
                    $groupId,
718
                    (in_array($groupId, $groups) ? ' selected' : ''),
719
                    $this->getGroupName($groupId)
720
                );
721
            }
722
        }
723
724
        return $options;
725
    }
726
727
    /**
728
     * Returns true if the user $userId owns the right $rightId
729
     * because of a group-membership, otherwise false.
730
     *
731
     * @param int $userId  User ID
732
     * @param int $rightId Right ID
733
     *
734
     * @return bool
735
     */
736 View Code Duplication
    public function checkUserGroupRight($userId, $rightId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
737
    {
738
        // check input
739
        if ($rightId <= 0 || $userId <= 0 || !is_numeric($rightId) || !is_numeric($userId)) {
740
            return false;
741
        }
742
743
        $select = sprintf('
744
            SELECT
745
                fr.right_id AS right_id
746
            FROM
747
                %sfaqright fr,
748
                %sfaqgroup_right fgr,
749
                %sfaqgroup fg,
750
                %sfaquser_group fug,
751
                %sfaquser fu
752
            WHERE
753
                fr.right_id = %d AND
754
                fr.right_id = fgr.right_id AND
755
                fg.group_id = fgr.group_id AND
756
                fg.group_id = fug.group_id AND
757
                fu.user_id  = fug.user_id AND
758
                fu.user_id  = %d',
759
            Db::getTablePrefix(),
760
            Db::getTablePrefix(),
761
            Db::getTablePrefix(),
762
            Db::getTablePrefix(),
763
            Db::getTablePrefix(),
764
            $rightId,
765
            $userId
766
        );
767
768
        $res = $this->config->getDb()->query($select);
769
        if ($this->config->getDb()->numRows($res) == 1) {
770
            return true;
771
        }
772
773
        return false;
774
    }
775
776
    /**
777
     * Checks the given associative array $groupData. If a
778
     * parameter is incorrect or is missing, it will be replaced
779
     * by the default values in $this->defaultGroupData.
780
     * Returns the corrected $groupData associative array.
781
     *
782
     * @param array $groupData Array of group data
783
     *
784
     * @return array
785
     */
786
    public function checkGroupData(Array $groupData)
787
    {
788 View Code Duplication
        if (!isset($groupData['name']) || !is_string($groupData['name'])) {
789
            $groupData['name'] = $this->defaultGroupData['name'];
790
        }
791 View Code Duplication
        if (!isset($groupData['description']) || !is_string($groupData['description'])) {
792
            $groupData['description'] = $this->defaultGroupData['description'];
793
        }
794
        if (!isset($groupData['auto_join'])) {
795
            $groupData['auto_join'] = $this->defaultGroupData['auto_join'];
796
        }
797
        $groupData['auto_join'] = (int)$groupData['auto_join'];
798
799
        return $groupData;
800
    }
801
802
    /**
803
     * Returns an array that contains the right-IDs of all rights
804
     * the user $userId owns. User-rights and the rights the user
805
     * owns because of a group-membership are taken into account.
806
     *
807
     * @param int $userId User ID
808
     *
809
     * @return array
810
     */
811
    public function getAllUserRights($userId)
812
    {
813
        if ($userId <= 0 || !is_numeric($userId)) {
814
            return [];
815
        }
816
        $userRights = $this->getUserRights($userId);
817
        $groupRights = $this->getUserGroupRights($userId);
818
819
        return array_unique(array_merge($userRights, $groupRights));
820
    }
821
822
    /**
823
     * Adds the user $userId to all groups with the auto_join
824
     * option. By using the auto_join option, user administration
825
     * can be much easier. For example by setting this option only
826
     * for a single group called 'All Users'. The autoJoin() method
827
     * then has to be called every time a new user registers.
828
     * Returns true on success, otherwise false.
829
     *
830
     * @param int $userId User ID
831
     *
832
     * @return bool
833
     */
834
    public function autoJoin($userId)
835
    {
836
        if ($userId <= 0 || !is_numeric($userId)) {
837
            return false;
838
        }
839
840
        $select = sprintf('
841
            SELECT
842
                group_id
843
            FROM
844
                %sfaqgroup
845
            WHERE
846
                auto_join = 1',
847
            Db::getTablePrefix()
848
        );
849
850
        $res = $this->config->getDb()->query($select);
851
        if (!$res) {
852
            return false;
853
        }
854
855
        $auto_join = [];
856
        while ($row = $this->config->getDb()->fetchArray($res)) {
857
            $auto_join[] = $row['group_id'];
858
        }
859
860
        // add to groups
861
        foreach ($auto_join as $groupId) {
862
            $this->addToGroup($userId, $groupId);
863
        }
864
865
        return true;
866
    }
867
868
    /**
869
     * Removes the user $userId from all groups.
870
     * Returns true on success, otherwise false.
871
     *
872
     * @param int $userId User ID
873
     *
874
     * @return bool
875
     */
876 View Code Duplication
    public function removeFromAllGroups($userId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
877
    {
878
        if ($userId <= 0 || !is_numeric($userId)) {
879
            return false;
880
        }
881
882
        $delete = sprintf('
883
            DELETE FROM
884
                %sfaquser_group
885
            WHERE
886
                user_id  = %d',
887
            Db::getTablePrefix(),
888
            $userId);
889
890
        $res = $this->config->getDb()->query($delete);
891
        if (!$res) {
892
            return false;
893
        }
894
895
        return true;
896
    }
897
898
    /**
899
     * Returns an array that contains the IDs of all rights the user
900
     * $userId owns because of a group-membership.
901
     *
902
     * @param int $userId User ID
903
     *
904
     * @return array
905
     */
906 View Code Duplication
    public function getUserGroupRights($userId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
907
    {
908
        if ($userId <= 0 || !is_numeric($userId)) {
909
            return [];
910
        }
911
912
        $select = sprintf('
913
            SELECT
914
                fr.right_id AS right_id
915
            FROM
916
                %sfaqright fr,
917
                %sfaqgroup_right fgr,
918
                %sfaqgroup fg,
919
                %sfaquser_group fug,
920
                %sfaquser fu
921
            WHERE
922
                fu.user_id  = %d AND
923
                fu.user_id  = fug.user_id AND
924
                fg.group_id = fug.group_id AND
925
                fg.group_id = fgr.group_id AND
926
                fr.right_id = fgr.right_id',
927
            Db::getTablePrefix(),
928
            Db::getTablePrefix(),
929
            Db::getTablePrefix(),
930
            Db::getTablePrefix(),
931
            Db::getTablePrefix(),
932
            $userId);
933
934
        $res = $this->config->getDb()->query($select);
935
        $result = [];
936
        while ($row = $this->config->getDb()->fetchArray($res)) {
937
            $result[] = $row['right_id'];
938
        }
939
940
        return $result;
941
    }
942
943
    /**
944
     * Refuses all group rights.
945
     * Returns true on success, otherwise false.
946
     *
947
     * @param int $groupId Group ID
948
     *
949
     * @return bool
950
     */
951 View Code Duplication
    public function refuseAllGroupRights($groupId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
952
    {
953
        if ($groupId <= 0 || !is_numeric($groupId)) {
954
            return false;
955
        }
956
957
        $delete = sprintf('
958
            DELETE FROM
959
                %sfaqgroup_right
960
            WHERE
961
                group_id  = %d',
962
            Db::getTablePrefix(),
963
            $groupId);
964
965
        $res = $this->config->getDb()->query($delete);
966
        if (!$res) {
967
            return false;
968
        }
969
970
        return true;
971
    }
972
973
    /**
974
     * Returns the name of the group $groupId.
975
     *
976
     * @param int $groupId Group ID
977
     *
978
     * @return string
979
     */
980 View Code Duplication
    public function getGroupName($groupId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
981
    {
982
        if ($groupId <= 0 || !is_numeric($groupId)) {
983
            return '-';
984
        }
985
986
        $select = sprintf('
987
            SELECT
988
                name
989
            FROM
990
                %sfaqgroup
991
            WHERE
992
                group_id = %d',
993
            Db::getTablePrefix(),
994
            $groupId
995
        );
996
997
        $res = $this->config->getDb()->query($select);
998
        if ($this->config->getDb()->numRows($res) != 1) {
999
            return '-';
1000
        }
1001
        $row = $this->config->getDb()->fetchArray($res);
1002
1003
        return $row['name'];
1004
    }
1005
1006
    /**
1007
     * Removes all users from the group $groupId.
1008
     * Returns true on success, otherwise false.
1009
     *
1010
     * @param int $groupId Group ID
1011
     *
1012
     * @return bool
1013
     */
1014 View Code Duplication
    public function removeAllUsersFromGroup($groupId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1015
    {
1016
        if ($groupId <= 0 or !is_numeric($groupId)) {
1017
            return false;
1018
        }
1019
1020
        // remove all user from group
1021
        $delete = sprintf('
1022
            DELETE FROM
1023
                %sfaquser_group
1024
            WHERE
1025
                group_id = %d',
1026
            Db::getTablePrefix(),
1027
            $groupId);
1028
1029
        $res = $this->config->getDb()->query($delete);
1030
        if (!$res) {
1031
            return false;
1032
        }
1033
1034
        return true;
1035
    }
1036
}
1037