Completed
Push — master ( c9a3ff...e781c7 )
by Robbie
12s queued 10s
created

SiteTreeExtension::canView()   C

Complexity

Conditions 13
Paths 10

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 10
nop 1
dl 0
loc 36
rs 6.6166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverStripe\RealMe\Extension;
4
5
use SilverStripe\RealMe\RealMeService;
6
use SilverStripe\Security\Member;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\ORM\DataExtension;
9
10
class SiteTreeExtension extends DataExtension
11
{
12
    private static $dependencies = array(
0 ignored issues
show
introduced by
The private property $dependencies is not used, and could be removed.
Loading history...
13
        'service' => '%$' . RealMeService::class
14
    );
15
16
    /**
17
     * @var RealMeService
18
     */
19
    public $service;
20
21
    /**
22
     * This function is an extension of the default SiteTree canView(), and allows viewing permissions for a SiteTree
23
     * object which has allowed a page to be presented to logged in users. With RealMe a logged in user is a user
24
     * which has authenticated with the identity provider, and we have stored a FLT in session.
25
     *
26
     * Return true, if the CanViewType is LoggedInUsers, and we have a valid RealMe Session authenticated.
27
     *
28
     * @param Member|int $member
29
     *
30
     * @return bool True if the current user can view this page
31
     */
32
    public function canView($member)
33
    {
34
        switch ($this->owner->CanViewType) {
35
            case 'Anyone':
36
                return true;
37
38
            case 'Inherit':
39
                if ($this->owner->ParentID) {
40
                    return $this->owner->Parent()->canView($member);
41
                }
42
                return $this->owner->getSiteConfig()->canViewPages($member);
43
44
            case 'LoggedInUsers':
45
                // check for any logged-in RealMe Sessions
46
                $data = $this->service->getUserData();
47
                if (!is_null($data)) {
48
                    return true;
49
                }
50
51
                if ($member && is_numeric($member)) {
52
                    return true;
53
                }
54
55
                return false;
56
57
            case 'OnlyTheseUsers':
58
                if ($member && is_numeric($member)) {
59
                    $member = DataObject::get_by_id(Member::class, $member);
60
61
                    /** @var Member $member */
62
                    if ($member && $member->inGroups($this->owner->ViewerGroups())) {
63
                        return true;
64
                    }
65
                }
66
        }
67
        return false;
68
    }
69
}
70