UserSecurityReport   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 11
dl 0
loc 130
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A title() 0 4 1
A description() 0 8 1
A columns() 0 8 2
A getColumns() 0 4 1
A summaryFields() 0 4 1
A sortColumns() 0 4 1
A sourceRecords() 0 5 1
A canView() 0 4 1
A getReportField() 0 13 1
1
<?php
2
namespace SilverStripe\SecurityReport;
3
4
use SilverStripe\Control\Director;
5
use SilverStripe\Forms\FormField;
6
use SilverStripe\Forms\GridField\GridField;
7
use SilverStripe\Forms\GridField\GridFieldExportButton;
8
use SilverStripe\Forms\GridField\GridFieldPrintButton;
9
use SilverStripe\ORM\DataList;
10
use SilverStripe\Reports\Report;
11
use SilverStripe\Security\Member;
12
use SilverStripe\Security\Permission;
13
use SilverStripe\Security\Security;
14
use SilverStripe\SecurityReport\Forms\GridFieldExportReportButton;
15
use SilverStripe\SecurityReport\Forms\GridFieldPrintReportButton;
16
17
/**
18
 * User Security Report
19
 *
20
 * @author Michael Parkhill <[email protected]>
21
 */
22
class UserSecurityReport extends Report
23
{
24
25
    /**
26
     * Columns in the report
27
     *
28
     * @var array
29
     * @config
30
     */
31
    private static $columns = array(
32
        'ID' => 'User ID',
33
        'FirstName' => 'First Name',
34
        'Surname' => 'Surname',
35
        'Email' => 'Email',
36
        'Created' => 'Date Created',
37
        'LastLoggedIn' => 'Last Logged In',
38
        'GroupsDescription' => 'Groups',
39
        'PermissionsDescription' => 'Permissions',
40
    );
41
    
42
    protected $dataClass = Member::class;
43
44
    /**
45
     * Returns the report title
46
     *
47
     * @return string
48
     */
49
    public function title()
50
    {
51
        return _t(__CLASS__ . '.REPORTTITLE', 'Users, Groups and Permissions');
52
    }
53
54
    /**
55
     * Builds a report description which is the current hostname with the current date and time
56
     *
57
     * @return string e.g. localhost/sitename - 21/12/2112
58
     */
59
    public function description()
60
    {
61
        return str_replace(
62
            array('http', 'https', '://'),
63
            '',
64
            Director::protocolAndHost() . ' - ' . date('d/m/Y H:i:s')
65
        );
66
    }
67
68
    /**
69
     * Returns the column names of the report
70
     *
71
     * @return array
72
     */
73
    public function columns()
74
    {
75
        $columns = self::config()->columns;
76
        if (!Security::config()->get('login_recording')) {
77
            unset($columns['LastLoggedIn']);
78
        }
79
        return $columns;
80
    }
81
82
    /**
83
     * Alias of columns(), to support the export to csv action
84
     * in {@link GridFieldExportButton} generateExportFileData method.
85
     * @return array
86
     */
87
    public function getColumns()
88
    {
89
        return $this->columns();
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    public function summaryFields()
96
    {
97
        return $this->columns();
98
    }
99
100
    /**
101
     * Defines the sortable columns on the report gridfield
102
     *
103
     * @return array
104
     */
105
    public function sortColumns()
106
    {
107
        return array_keys($this->columns());
108
    }
109
110
    /**
111
     * Get the source records for the report gridfield
112
     *
113
     * @return DataList
114
     */
115
    public function sourceRecords()
116
    {
117
        // Get members sorted by ID
118
        return Member::get()->sort('ID');
119
    }
120
121
    /**
122
     * Restrict access to this report to users with security admin access
123
     *
124
     * @param Member $member
125
     * @return boolean
126
     */
127
    public function canView($member = null)
128
    {
129
        return (bool)Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin");
130
    }
131
132
    /**
133
     * Return a field, such as a {@link GridField} that is
134
     * used to show and manipulate data relating to this report.
135
     *
136
     * @return FormField subclass
137
     */
138
    public function getReportField()
139
    {
140
        /** @var GridField $gridField */
141
        $gridField = parent::getReportField();
142
        $gridField->setModelClass(self::class);
143
        $gridConfig = $gridField->getConfig();
144
        $gridConfig->removeComponentsByType([GridFieldPrintButton::class, GridFieldExportButton::class]);
145
        $gridConfig->addComponents(
146
            new GridFieldPrintReportButton('buttons-before-left'),
147
            new GridFieldExportReportButton('buttons-before-left')
148
        );
149
        return $gridField;
150
    }
151
}
152