Issues (50)

src/Extensions/DataObjectSearchExtension.php (4 issues)

1
<?php
2
3
namespace Firesphere\SearchBackend\Extensions;
4
5
use Firesphere\SearchBackend\Models\DirtyClass;
6
use SilverStripe\CMS\Model\SiteTree;
0 ignored issues
show
The type SilverStripe\CMS\Model\SiteTree was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use SilverStripe\ORM\DataExtension;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\ORM\ValidationException;
10
use SilverStripe\Security\InheritedPermissionsExtension;
11
use SilverStripe\SiteConfig\SiteConfig;
0 ignored issues
show
The type SilverStripe\SiteConfig\SiteConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
/**
14
 * Class \Firesphere\SearchBackend\Extensions\DataObjectSearchExtension
15
 *
16
 * @property DataObject|DataObjectSearchExtension $owner
17
 */
18
class DataObjectSearchExtension extends DataExtension
19
{
20
    /**
21
     * @var array Cached permission list
22
     */
23
    public static $cachedClasses;
24
    /**
25
     * @var SiteConfig Current siteconfig
26
     */
27
    protected static $siteConfig;
28
29
    /**
30
     * Get the view status for each member in this object
31
     *
32
     * @return array
33
     */
34
    public function getViewStatus(): array
35
    {
36
        // return as early as possible
37
        /** @var DataObject|SiteTree $owner */
38
        $owner = $this->owner;
39
        if (isset(static::$cachedClasses[$owner->ClassName])) {
40
            return static::$cachedClasses[$owner->ClassName];
41
        }
42
43
        // Make sure the siteconfig is loaded
44
        if (!static::$siteConfig) {
45
            static::$siteConfig = SiteConfig::current_site_config();
46
        }
47
        // Return false if it's not allowed to show in search
48
        // The setting needs to be explicitly false, to avoid any possible collision
49
        // with objects not having the setting, thus being `null`
50
        // Return immediately if the owner has ShowInSearch not being `null`
51
        if ($owner->ShowInSearch === false || $owner->ShowInSearch === 0) {
52
            return ['false'];
53
        }
54
55
        $permissions = $this->getGroupViewPermissions($owner);
56
57
        if (!$owner->hasExtension(InheritedPermissionsExtension::class)) {
58
            static::$cachedClasses[$owner->ClassName] = $permissions;
59
        }
60
61
        return $permissions;
62
    }
63
64
    /**
65
     * Determine the view permissions based on group settings
66
     *
67
     * @param DataObject|SiteTree|SiteConfig $owner
68
     * @return array
69
     */
70
    protected function getGroupViewPermissions($owner): array
71
    {
72
        // Switches are not ideal, but it's a lot more readable this way!
73
        switch ($owner->CanViewType) {
74
            case 'LoggedInUsers':
75
                $return = ['false', 'LoggedIn'];
76
                break;
77
            case 'OnlyTheseUsers':
78
                $return = ['false'];
79
                $return = array_merge($return, $owner->ViewerGroups()->column('Code'));
80
                break;
81
            case 'Inherit':
82
                $parent = !$owner->ParentID ? static::$siteConfig : $owner->Parent();
83
                $return = $this->getGroupViewPermissions($parent);
84
                break;
85
            case 'Anyone': // View is either not implemented, or it's "Anyone"
86
                $return = ['null'];
87
                break;
88
            default:
89
                // Default to "Anyone can view"
90
                $return = ['null'];
91
        }
92
93
        return $return;
94
    }
95
96
    /**
97
     * Find or create a new DirtyClass for recording dirty IDs
98
     *
99
     * @param string $type
100
     * @return DirtyClass
101
     * @throws ValidationException
102
     */
103
    public function getDirtyClass(string $type)
104
    {
105
        // Get the DirtyClass object for this item
106
        /** @var null|DirtyClass $record */
107
        $record = DirtyClass::get()->filter(['Class' => $this->owner->ClassName, 'Type' => $type])->first();
0 ignored issues
show
Bug Best Practice introduced by
The property ClassName does not exist on Firesphere\SearchBackend...taObjectSearchExtension. Did you maybe forget to declare it?
Loading history...
108
        if (!$record || !$record->exists()) {
0 ignored issues
show
$record is of type Firesphere\SearchBackend\Models\DirtyClass, thus it always evaluated to true.
Loading history...
109
            $record = DirtyClass::create([
110
                'Class' => $this->owner->ClassName,
111
                'Type'  => $type,
112
            ]);
113
            $record->write();
114
        }
115
116
        return $record;
117
    }
118
}
119