Issues (2)

src/Extensions/ElementalCanViewExtension.php (2 issues)

1
<?php
2
3
namespace Sunnysideup\ElementalCanView\Extensions;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\OptionsetField;
7
use SilverStripe\Forms\TreeMultiselectField;
8
use SilverStripe\ORM\DataExtension;
9
use SilverStripe\Security\Group;
10
use SilverStripe\Security\InheritedPermissions;
11
use SilverStripe\Security\Permission;
12
use SilverStripe\Security\Security;
13
use Sunnysideup\ElementalCanView\Api\PermissionCanViewListMaker;
14
15
class ElementalCanViewExtension extends DataExtension
16
{
17
    /**
18
     * @var string
19
     */
20
    private const NOT_LOGGED_IN_USERS = 'NotLoggedInUsers';
21
22
    private static $db = [
23
        'CanViewType' => "Enum('" .
24
            InheritedPermissions::ANYONE . ', ' .
25
            self::NOT_LOGGED_IN_USERS . ', ' .
26
            InheritedPermissions::LOGGED_IN_USERS . ', ' .
27
            InheritedPermissions::ONLY_THESE_USERS . "', '" .
28
            InheritedPermissions::ANYONE .
29
        "')",
30
    ];
31
32
    private static $many_many = [
33
        'ViewerGroups' => Group::class,
34
    ];
35
36
    private static $defaults = [
37
        'CanViewType' => InheritedPermissions::ANYONE,
38
    ];
39
40
    public function canView($member, $content = [])
0 ignored issues
show
The parameter $content is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

40
    public function canView($member, /** @scrutinizer ignore-unused */ $content = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        $owner = $this->getOwner();
43
        if (! $member) {
44
            $member = Security::getCurrentUser();
45
        }
46
47
        // admin override
48
        if ($member && Permission::checkMember($member, ['ADMIN', 'SITETREE_VIEW_ALL'])) {
49
            return true;
50
        }
51
52
        // if there is no meaningfull response go back to actual element itself!
53
        if (! $owner->CanViewType || InheritedPermissions::ANYONE === $owner->CanViewType) {
54
            return null;
55
        }
56
57
        // check for any  NOT logged-in users
58
        if (self::NOT_LOGGED_IN_USERS === $owner->CanViewType) {
59
            if ($member && $member->ID) {
60
                return false;
61
            }
62
        }
63
64
        // check for any logged-in users
65
        if (InheritedPermissions::LOGGED_IN_USERS === $owner->CanViewType) {
66
            if (! ($member && $member->ID)) {
67
                return false;
68
            }
69
        }
70
71
        // check for specific groups
72
        if (InheritedPermissions::ONLY_THESE_USERS === $owner->CanViewType) {
73
            if (! ($member && $member->inGroups($owner->ViewerGroups()))) {
74
                return false;
75
            }
76
        }
77
78
        //important - return back to actual element
79
        return null;
80
    }
81
82
    public function updateCMSFields(FieldList $fields)
83
    {
84
        $owner = $this->getOwner();
85
        $viewAllGroupsMap = PermissionCanViewListMaker::get_list();
86
        $fields->removeFieldFromTab('Root', 'ViewerGroups');
87
        $fields->addFieldsToTab(
88
            'Root.Permissions',
89
            [
90
                $viewersOptionsField = (new OptionsetField(
91
                    'CanViewType',
92
                    _t(__CLASS__ . '.ACCESSHEADER', 'Who can view this elemental block?')
93
                ))
94
                    ->setDescription('
95
                        As an Administrator,
96
                        you can always see ALL blocks - no matter what - otherwise you could not edit them.'),
97
                $viewerGroupsField = TreeMultiselectField::create(
98
                    'ViewerGroups',
99
                    _t(__CLASS__ . '.VIEWERGROUPS', 'Viewer Groups'),
100
                    Group::class
101
                ),
102
            ]
103
        );
104
105
        $viewersOptionsSource = [
106
            InheritedPermissions::ANYONE => _t(__CLASS__ . '.ACCESSANYONEWITHPAGEACCESS', 'Anyone who can view the parent page'),
107
            self::NOT_LOGGED_IN_USERS => _t(__CLASS__ . '.ACCESSNOTLOGGEDIN', 'Logged-out users'),
108
            InheritedPermissions::LOGGED_IN_USERS => _t(__CLASS__ . '.ACCESSLOGGEDIN', 'Logged-in users'),
109
            InheritedPermissions::ONLY_THESE_USERS => _t(
110
                __CLASS__ . '.ACCESSONLYTHESE',
111
                'Only these groups (choose from list)'
112
            ),
113
        ];
114
        $viewersOptionsField->setSource($viewersOptionsSource);
115
116
        if ($viewAllGroupsMap) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $viewAllGroupsMap of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
117
            $viewerGroupsField->setDescription(_t(
118
                __CLASS__ . '.VIEWER_GROUPS_FIELD_DESC',
119
                'Groups with global view permissions: {groupList}',
120
                ['groupList' => implode(', ', array_values($viewAllGroupsMap))]
121
            ));
122
        }
123
124
        if (! Permission::check('SITETREE_GRANT_ACCESS')) {
125
            $fields->makeFieldReadonly($viewersOptionsField);
126
            if (InheritedPermissions::ONLY_THESE_USERS === $owner->CanEditType) {
127
                $fields->makeFieldReadonly($viewerGroupsField);
128
            } else {
129
                $fields->removeByName('ViewerGroups');
130
            }
131
        }
132
133
        return $fields;
134
    }
135
}
136