Completed
Push — master ( 05d8f1...d6e5c2 )
by
unknown
01:53 queued 34s
created

LogEntry::providePermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace SilverLeague\LogViewer\Model;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\Forms\LiteralField;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\Security\Permission;
9
use SilverStripe\Security\PermissionProvider;
10
11
/**
12
 * A LogEntry is a set of data provided from Monolog via the DataObjectHandler
13
 *
14
 * @package silverstripe-logviewer
15
 * @author  Robbie Averill <[email protected]>
16
 */
17
class LogEntry extends DataObject implements PermissionProvider
18
{
19
    private static $table_name = 'LogEntry';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $table_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
20
21
    private static $db = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
        'Entry'    => 'Text',
23
        'Level'    => 'Varchar'
24
    ];
25
26
    private static $summary_fields = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $summary_fields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
27
        'Entry',
28
        'Created',
29
        'Level'
30
    ];
31
32
    /**
33
     * Whether the cron functionality should run. This does not affect use as a BuildTask.
34
     * Note: you need to configure silverstripe/crontask yourself.
35
     *
36
     * @config
37
     * @var bool
38
     */
39
    private static $cron_enabled = true;
0 ignored issues
show
Unused Code introduced by
The property $cron_enabled is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
40
41
    /**
42
     * How often the cron should run (default: 4am daily)
43
     *
44
     * @config
45
     * @var string
46
     */
47
    private static $cron_schedule = '0 4 * * *';
0 ignored issues
show
Unused Code introduced by
The property $cron_schedule is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
48
49
    /**
50
     * The maximum age in days for a LogEntry before it will be removed
51
     *
52
     * @config
53
     * @var int
54
     */
55
    private static $max_log_age = 30;
0 ignored issues
show
Unused Code introduced by
The property $max_log_age is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
56
57
    /**
58
     * Which Monolog\Logger levels (numeric) to start handling from (see class for examples)
59
     *
60
     * @config
61
     * @var integer
62
     */
63
    private static $minimum_log_level = 300;
0 ignored issues
show
Unused Code introduced by
The property $minimum_log_level is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
64
65
    /**
66
     * Permissions
67
     */
68
    public function providePermissions()
69
    {
70
        return [
71
            'DELETE_ENTRY' => [
72
                'name' => _t('LogEntry.PERMISSION_DELETE_DESCRIPTION', 'Delete log entries'),
73
                'category' => _t('Permissions.LOGENTRY_CATEGORY', 'Log entry permissions'),
74
                'help' => _t('LogEntry.PERMISSION_DELETE_HELP', 'Permission required to delete existing log entries.')
75
            ],
76
            'VIEW_ENTRY' => [
77
                'name' => _t('LogEntry.PERMISSION_VIEW_DESCRIPTION', 'View log entries'),
78
                'category' => _t('Permissions.LOGENTRY_CATEGORY', 'Log entry permissions'),
79
                'help' => _t('LogEntry.PERMISSION_VIEW_HELP', 'Permission required to view existing log entries.')
80
            ]
81
        ];
82
    }
83
84
    /**
85
     * Format the log entry as JSON
86
     *
87
     * {@inheritDoc}
88
     */
89
    public function getCMSFields()
90
    {
91
        $fields = parent::getCMSFields();
92
93
        $data = Convert::json2obj($this->getField('Entry'));
94
        $fields->addFieldToTab(
95
            'Root.Main',
96
            LiteralField::create(
97
                'Entry',
98
                '<pre class="logviewer-logentry-entry"><code>'
99
                . Convert::raw2json($data, JSON_PRETTY_PRINT)
100
                . '</code></pre>'
101
            )
102
        );
103
104
        return $fields;
105
    }
106
107
    /**
108
     * Log entries are created programmatically, they should never be created manually
109
     *
110
     * {@inheritDoc}
111
     */
112
    public function canCreate($member = null, $context = [])
113
    {
114
        return false;
115
    }
116
117
    /**
118
     * We should never edit log entries
119
     *
120
     * {@inheritDoc}
121
     */
122
    public function canEdit($member = null)
123
    {
124
        return false;
125
    }
126
127
    public function canDelete($member = null)
128
    {
129
        return Permission::checkMember($member, ['DELETE_ENTRY', 'CMS_ACCESS_LogViewerAdmin']);
130
    }
131
132
    public function canView($member = null)
133
    {
134
        return Permission::checkMember($member, ['VIEW_ENTRY', 'CMS_ACCESS_LogViewerAdmin']);
135
    }
136
}
137