SolrLog::providePermissions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.8333
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * class SolrLog|Firesphere\SolrSearch\Models\SolrLog Solr logging to be read from the CMS
4
 *
5
 * @package Firesphere\Solr\Search
6
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
7
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
8
 */
9
10
namespace Firesphere\SolrSearch\Models;
11
12
use SilverStripe\Control\Director;
13
use SilverStripe\ORM\DataObject;
14
use SilverStripe\ORM\FieldType\DBDatetime;
15
use SilverStripe\Security\Member;
16
use SilverStripe\Security\PermissionProvider;
17
18
/**
19
 * Class \Firesphere\SolrSearch\Models\SolrError
20
 *
21
 * @package Firesphere\Solr\Search
22
 * @property string $Timestamp
23
 * @property string $Index
24
 * @property string $Type
25
 * @property string $Level
26
 * @property string $Message
27
 */
28
class SolrLog extends DataObject implements PermissionProvider
29
{
30
    /**
31
     * @var array Used to give the Gridfield rows a corresponding colour
32
     */
33
    protected static $row_color = [
34
        'ERROR' => 'alert alert-danger',
35
        'WARN'  => 'alert alert-warning',
36
        'INFO'  => 'alert alert-info',
37
    ];
38
    /**
39
     * @var string Database table name
40
     */
41
    private static $table_name = 'Solr_SolrLog';
42
    /**
43
     * @var array Database columns
44
     */
45
    private static $db = [
46
        'Timestamp' => 'Datetime',
47
        'Index'     => 'Varchar(255)',
48
        'Type'      => 'Enum("Config,Index,Query")',
49
        'Level'     => 'Varchar(10)',
50
        'Message'   => 'Text',
51
    ];
52
    /**
53
     * @var array Summary fields
54
     */
55
    private static $summary_fields = [
56
        'Timestamp',
57
        'Index',
58
        'Type',
59
        'Level',
60
    ];
61
    /**
62
     * @var array Searchable fields
63
     */
64
    private static $searchable_fields = [
65
        'Created',
66
        'Timestamp',
67
        'Index',
68
        'Type',
69
        'Level',
70
    ];
71
    /**
72
     * @var array Timestamp is indexed
73
     */
74
    private static $indexes = [
75
        'Timestamp' => true,
76
    ];
77
    /**
78
     * @var string Default sort
79
     */
80
    private static $default_sort = 'Timestamp DESC';
81
82
    /**
83
     * Convert the Timestamp to a DBDatetime for compatibility
84
     */
85 1
    public function onBeforeWrite()
86
    {
87 1
        parent::onBeforeWrite();
88
89 1
        $this->Timestamp = DBDatetime::create()->setValue(strtotime($this->Timestamp));
90 1
    }
91
92
    /**
93
     * Return the first line of this log item error
94
     *
95
     * @return string
96
     */
97 1
    public function getLastErrorLine()
98
    {
99 1
        $lines = explode(PHP_EOL, $this->Message);
100
101 1
        return $lines[0];
102
    }
103
104
    /**
105
     * Not creatable by users
106
     *
107
     * @param null|Member $member
108
     * @param array $context
109
     * @return bool|mixed
110
     */
111 1
    public function canCreate($member = null, $context = [])
112
    {
113 1
        return false;
114
    }
115
116
    /**
117
     * Not editable by users
118
     *
119
     * @param null|Member $member
120
     * @return bool|mixed
121
     */
122 1
    public function canEdit($member = null)
123
    {
124 1
        return false;
125
    }
126
127
    /**
128
     * Member has view access?
129
     *
130
     * @param null|Member $member
131
     * @return bool|mixed
132
     */
133 2
    public function canView($member = null)
134
    {
135 2
        return parent::canView($member);
136
    }
137
138
    /**
139
     * Only deleteable by admins or when in dev mode to clean up
140
     *
141
     * @param null|Member $member
142
     * @return bool|mixed
143
     */
144 1
    public function canDelete($member = null)
145
    {
146 1
        if ($member) {
147 1
            return $member->inGroup('administrators') || Director::isDev();
148
        }
149
150 1
        return parent::canDelete($member) || Director::isDev();
151
    }
152
153
    /**
154
     * Get the extra classes to colour the gridfield rows
155
     *
156
     * @return mixed|string
157
     */
158 1
    public function getExtraClass()
159
    {
160 1
        $classMap = static::$row_color;
161
162 1
        return $classMap[$this->Level] ?? 'alert alert-info';
163
    }
164
165
166
    /**
167
     * Return a map of permission codes to add to the dropdown shown in the Security section of the CMS.
168
     * array(
169
     *   'VIEW_SITE' => 'View the site',
170
     * );
171
     *
172
     * @return array
173
     */
174 1
    public function providePermissions()
175
    {
176
        return [
177
            'DELETE_LOG' => [
178 1
                'name'     => _t(self::class . '.PERMISSION_DELETE_DESCRIPTION', 'Delete Solr logs'),
179 1
                'category' => _t('Permissions.LOGS_CATEGORIES', 'Solr logs permissions'),
180 1
                'help'     => _t(
181 1
                    self::class . '.PERMISSION_DELETE_HELP',
182 1
                    'Permission required to delete existing Solr logs.'
183
                ),
184
            ],
185
            'VIEW_LOG'   => [
186 1
                'name'     => _t(self::class . '.PERMISSION_VIEW_DESCRIPTION', 'View Solr logs'),
187 1
                'category' => _t('Permissions.LOGS_CATEGORIES', 'Solr logs permissions'),
188 1
                'help'     => _t(
189 1
                    self::class . '.PERMISSION_VIEW_HELP',
190 1
                    'Permission required to view existing Solr logs.'
191
                ),
192
            ],
193
        ];
194
    }
195
}
196