QueryFilterTrait::getViewStatusFilter()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 8
nc 3
nop 0
dl 0
loc 17
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Firesphere\SearchBackend\Traits\QueryTraits;
4
5
use SilverStripe\ORM\DataList;
6
use SilverStripe\Security\Group;
7
use SilverStripe\Security\Security;
8
9
/**
10
 *
11
 */
12
trait QueryFilterTrait
13
{
14
    /**
15
     * Add filtering on view status
16
     */
17
    public function getViewStatusFilter(): array
18
    {
19
        // Filter by what the user is allowed to see
20
        $viewIDs = ['null']; // null is always an option as that means publicly visible
21
        $member = Security::getCurrentUser();
22
        if ($member && $member->exists()) {
23
            // Member is logged in, thus allowed to see these
24
            $viewIDs[] = 'LoggedIn';
25
26
            /** @var DataList|Group[] $groups */
27
            $groups = Security::getCurrentUser()->Groups();
28
            if ($groups->count()) {
29
                $viewIDs = array_merge($viewIDs, $groups->column('Code'));
30
            }
31
        }
32
33
        return $viewIDs;
34
    }
35
}
36