Completed
Push — master ( 24a71d...4147fa )
by Robbie
9s
created
src/UserSecurityReport.php 1 patch
Indentation   +116 added lines, -116 removed lines patch added patch discarded remove patch
@@ -22,130 +22,130 @@
 block discarded – undo
22 22
 class UserSecurityReport extends Report
23 23
 {
24 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
-    );
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 41
     
42
-    protected $dataClass = Member::class;
42
+	protected $dataClass = Member::class;
43 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
-    }
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 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
-    }
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 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
-    }
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 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
-    }
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 91
 
92
-    /**
93
-     * @return array
94
-     */
95
-    public function summaryFields()
96
-    {
97
-        return $this->columns();
98
-    }
92
+	/**
93
+	 * @return array
94
+	 */
95
+	public function summaryFields()
96
+	{
97
+		return $this->columns();
98
+	}
99 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
-    }
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 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
-    }
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 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
-    }
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 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
-    }
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 151
 }
Please login to merge, or discard this patch.
tests/UserSecurityReportTest.php 1 patch
Indentation   +76 added lines, -76 removed lines patch added patch discarded remove patch
@@ -20,80 +20,80 @@
 block discarded – undo
20 20
 class UserSecurityReportTest extends SapphireTest
21 21
 {
22 22
 
23
-    protected static $fixture_file = 'UserSecurityReportTest.yml';
24
-
25
-    protected $records;
26
-    protected $report;
27
-
28
-    protected static $required_extensions = [
29
-        Member::class => [
30
-            MemberReportExtension::class,
31
-        ],
32
-    ];
33
-
34
-    protected static $illegal_extensions = [
35
-        Group::class => [
36
-            GroupSubsites::class,
37
-        ],
38
-    ];
39
-
40
-    /**
41
-     * Utility method for all tests to use.
42
-     *
43
-     * @return \ArrayList
44
-     * @todo pre-fill the report with fixture-defined users
45
-     */
46
-    protected function setUp()
47
-    {
48
-        parent::setUp();
49
-        $reports = Report::get_reports();
50
-        $report = $reports[UserSecurityReport::class];
51
-        $this->report = $report;
52
-        $this->records = $report->sourceRecords()->toArray();
53
-    }
54
-
55
-    public function testSourceRecords()
56
-    {
57
-        $this->assertNotEmpty($this->records);
58
-    }
59
-
60
-    public function testGetMemberGroups()
61
-    {
62
-        //getMemberGroups(&$member) returns string
63
-        $member = $this->objFromFixture(Member::class, 'member-has-0-groups');
64
-        $groups = $member->GroupsDescription;
65
-        $this->assertEquals('Not in a Security Group', $groups);
66
-
67
-        $member = $this->objFromFixture(Member::class, 'member-has-1-groups');
68
-        $groups = $member->GroupsDescription;
69
-        $this->assertEquals('Group Test 01', $groups);
70
-    }
71
-
72
-    public function testGetMemberPermissions()
73
-    {
74
-        $member = $this->objFromFixture(Member::class, 'member-has-0-permissions');
75
-        $perms = $member->PermissionsDescription;
76
-        $this->assertEquals('No Permissions', $perms);
77
-
78
-        $member = $this->objFromFixture(Member::class, 'member-has-1-permissions');
79
-        $perms = $member->PermissionsDescription;
80
-        $this->assertEquals('Full administrative rights', $perms);
81
-
82
-        $member = $this->objFromFixture(Member::class, 'member-has-n-permissions');
83
-        $perms = $member->PermissionsDescription;
84
-        $this->assertEquals('Full administrative rights, Edit any page', $perms);
85
-    }
86
-
87
-    public function testLoginLoggingColumnShowsOnlyWhenApplicable()
88
-    {
89
-        $original = Config::inst()->get(Security::class, 'login_recording');
90
-
91
-        Config::modify()->set(Security::class, 'login_recording', true);
92
-        $this->assertContains('LastLoggedIn', array_keys($this->report->columns()));
93
-
94
-        Config::modify()->set(Security::class, 'login_recording', false);
95
-        $this->assertNotContains('LastLoggedIn', array_keys($this->report->columns()));
96
-
97
-        Config::modify()->set(Security::class, 'login_recording', $original);
98
-    }
23
+	protected static $fixture_file = 'UserSecurityReportTest.yml';
24
+
25
+	protected $records;
26
+	protected $report;
27
+
28
+	protected static $required_extensions = [
29
+		Member::class => [
30
+			MemberReportExtension::class,
31
+		],
32
+	];
33
+
34
+	protected static $illegal_extensions = [
35
+		Group::class => [
36
+			GroupSubsites::class,
37
+		],
38
+	];
39
+
40
+	/**
41
+	 * Utility method for all tests to use.
42
+	 *
43
+	 * @return \ArrayList
44
+	 * @todo pre-fill the report with fixture-defined users
45
+	 */
46
+	protected function setUp()
47
+	{
48
+		parent::setUp();
49
+		$reports = Report::get_reports();
50
+		$report = $reports[UserSecurityReport::class];
51
+		$this->report = $report;
52
+		$this->records = $report->sourceRecords()->toArray();
53
+	}
54
+
55
+	public function testSourceRecords()
56
+	{
57
+		$this->assertNotEmpty($this->records);
58
+	}
59
+
60
+	public function testGetMemberGroups()
61
+	{
62
+		//getMemberGroups(&$member) returns string
63
+		$member = $this->objFromFixture(Member::class, 'member-has-0-groups');
64
+		$groups = $member->GroupsDescription;
65
+		$this->assertEquals('Not in a Security Group', $groups);
66
+
67
+		$member = $this->objFromFixture(Member::class, 'member-has-1-groups');
68
+		$groups = $member->GroupsDescription;
69
+		$this->assertEquals('Group Test 01', $groups);
70
+	}
71
+
72
+	public function testGetMemberPermissions()
73
+	{
74
+		$member = $this->objFromFixture(Member::class, 'member-has-0-permissions');
75
+		$perms = $member->PermissionsDescription;
76
+		$this->assertEquals('No Permissions', $perms);
77
+
78
+		$member = $this->objFromFixture(Member::class, 'member-has-1-permissions');
79
+		$perms = $member->PermissionsDescription;
80
+		$this->assertEquals('Full administrative rights', $perms);
81
+
82
+		$member = $this->objFromFixture(Member::class, 'member-has-n-permissions');
83
+		$perms = $member->PermissionsDescription;
84
+		$this->assertEquals('Full administrative rights, Edit any page', $perms);
85
+	}
86
+
87
+	public function testLoginLoggingColumnShowsOnlyWhenApplicable()
88
+	{
89
+		$original = Config::inst()->get(Security::class, 'login_recording');
90
+
91
+		Config::modify()->set(Security::class, 'login_recording', true);
92
+		$this->assertContains('LastLoggedIn', array_keys($this->report->columns()));
93
+
94
+		Config::modify()->set(Security::class, 'login_recording', false);
95
+		$this->assertNotContains('LastLoggedIn', array_keys($this->report->columns()));
96
+
97
+		Config::modify()->set(Security::class, 'login_recording', $original);
98
+	}
99 99
 }
Please login to merge, or discard this patch.
src/MemberReportExtension.php 1 patch
Indentation   +98 added lines, -98 removed lines patch added patch discarded remove patch
@@ -13,113 +13,113 @@
 block discarded – undo
13 13
 class MemberReportExtension extends DataExtension
14 14
 {
15 15
 
16
-    /**
17
-     * Connect the link to LoginAttempt.
18
-     * This relationship is always defined (whether enabled or not),
19
-     * although only normally accessible from the `LoginAttempt` side.
20
-     * This is adding the reflection, as that it is also accessible
21
-     * from the `Member` side.
22
-     *
23
-     * @var array
24
-     * @config
25
-     */
26
-    private static $has_many = [
27
-        'LoginAttempts' => LoginAttempt::class
28
-    ];
16
+	/**
17
+	 * Connect the link to LoginAttempt.
18
+	 * This relationship is always defined (whether enabled or not),
19
+	 * although only normally accessible from the `LoginAttempt` side.
20
+	 * This is adding the reflection, as that it is also accessible
21
+	 * from the `Member` side.
22
+	 *
23
+	 * @var array
24
+	 * @config
25
+	 */
26
+	private static $has_many = [
27
+		'LoginAttempts' => LoginAttempt::class
28
+	];
29 29
     
30
-    /**
31
-     * Set cast of additional fields
32
-     *
33
-     * @var array
34
-     * @config
35
-     */
36
-    private static $casting = array(
37
-        'GroupsDescription' => 'Text',
38
-        'PermissionsDescription' => 'Text'
39
-    );
30
+	/**
31
+	 * Set cast of additional fields
32
+	 *
33
+	 * @var array
34
+	 * @config
35
+	 */
36
+	private static $casting = array(
37
+		'GroupsDescription' => 'Text',
38
+		'PermissionsDescription' => 'Text'
39
+	);
40 40
 
41
-    /**
42
-     * Retrieves the most recent successful LoginAttempt
43
-     *
44
-     * @return DBDatetime|string
45
-     */
46
-    public function getLastLoggedIn()
47
-    {
48
-        $lastTime = $this->owner->LoginAttempts()
49
-            ->filter('Status', 'Success')
50
-            ->sort('Created', 'DESC')
51
-            ->first();
41
+	/**
42
+	 * Retrieves the most recent successful LoginAttempt
43
+	 *
44
+	 * @return DBDatetime|string
45
+	 */
46
+	public function getLastLoggedIn()
47
+	{
48
+		$lastTime = $this->owner->LoginAttempts()
49
+			->filter('Status', 'Success')
50
+			->sort('Created', 'DESC')
51
+			->first();
52 52
 
53
-        return $lastTime ? $lastTime->dbObject('Created') : _t(__CLASS__ . '.NEVER', 'Never');
54
-    }
53
+		return $lastTime ? $lastTime->dbObject('Created') : _t(__CLASS__ . '.NEVER', 'Never');
54
+	}
55 55
 
56
-    /**
57
-     * Builds a comma separated list of member group names for a given Member.
58
-     *
59
-     * @return string
60
-     */
61
-    public function getGroupsDescription()
62
-    {
63
-        if (class_exists(Subsite::class)) {
64
-            Subsite::disable_subsite_filter(true);
65
-        }
56
+	/**
57
+	 * Builds a comma separated list of member group names for a given Member.
58
+	 *
59
+	 * @return string
60
+	 */
61
+	public function getGroupsDescription()
62
+	{
63
+		if (class_exists(Subsite::class)) {
64
+			Subsite::disable_subsite_filter(true);
65
+		}
66 66
         
67
-        // Get the member's groups, if any
68
-        $groups = $this->owner->Groups();
69
-        if ($groups->Count()) {
70
-            // Collect the group names
71
-            $groupNames = array();
72
-            foreach ($groups as $group) {
73
-                $groupNames[] = html_entity_decode($group->getTreeTitle());
74
-            }
75
-            // return a csv string of the group names, sans-markup
76
-            $result = preg_replace("#</?[^>]>#", '', implode(', ', $groupNames));
77
-        } else {
78
-            // If no groups then return a status label
79
-            $result = _t(__CLASS__ . '.NOGROUPS', 'Not in a Security Group');
80
-        }
67
+		// Get the member's groups, if any
68
+		$groups = $this->owner->Groups();
69
+		if ($groups->Count()) {
70
+			// Collect the group names
71
+			$groupNames = array();
72
+			foreach ($groups as $group) {
73
+				$groupNames[] = html_entity_decode($group->getTreeTitle());
74
+			}
75
+			// return a csv string of the group names, sans-markup
76
+			$result = preg_replace("#</?[^>]>#", '', implode(', ', $groupNames));
77
+		} else {
78
+			// If no groups then return a status label
79
+			$result = _t(__CLASS__ . '.NOGROUPS', 'Not in a Security Group');
80
+		}
81 81
         
82
-        if (class_exists(Subsite::class)) {
83
-            Subsite::disable_subsite_filter(false);
84
-        }
85
-        return $result;
86
-    }
82
+		if (class_exists(Subsite::class)) {
83
+			Subsite::disable_subsite_filter(false);
84
+		}
85
+		return $result;
86
+	}
87 87
     
88
-    /**
89
-     * Builds a comma separated list of human-readbale permissions for a given Member.
90
-     *
91
-     * @return string
92
-     */
93
-    public function getPermissionsDescription()
94
-    {
95
-        if (class_exists(Subsite::class)) {
96
-            Subsite::disable_subsite_filter(true);
97
-        }
88
+	/**
89
+	 * Builds a comma separated list of human-readbale permissions for a given Member.
90
+	 *
91
+	 * @return string
92
+	 */
93
+	public function getPermissionsDescription()
94
+	{
95
+		if (class_exists(Subsite::class)) {
96
+			Subsite::disable_subsite_filter(true);
97
+		}
98 98
         
99
-        $permissionsUsr = Permission::permissions_for_member($this->owner->ID);
100
-        $permissionsSrc = Permission::get_codes(true);
101
-        sort($permissionsUsr);
99
+		$permissionsUsr = Permission::permissions_for_member($this->owner->ID);
100
+		$permissionsSrc = Permission::get_codes(true);
101
+		sort($permissionsUsr);
102 102
         
103
-        $permissionNames = array();
104
-        foreach ($permissionsUsr as $code) {
105
-            $code = strtoupper($code);
106
-            foreach ($permissionsSrc as $k => $v) {
107
-                if (isset($v[$code])) {
108
-                    $name = empty($v[$code]['name'])
109
-                        ? _t(__CLASS__ . '.UNKNOWN', 'Unknown')
110
-                        : $v[$code]['name'];
111
-                    $permissionNames[] = $name;
112
-                }
113
-            }
114
-        }
103
+		$permissionNames = array();
104
+		foreach ($permissionsUsr as $code) {
105
+			$code = strtoupper($code);
106
+			foreach ($permissionsSrc as $k => $v) {
107
+				if (isset($v[$code])) {
108
+					$name = empty($v[$code]['name'])
109
+						? _t(__CLASS__ . '.UNKNOWN', 'Unknown')
110
+						: $v[$code]['name'];
111
+					$permissionNames[] = $name;
112
+				}
113
+			}
114
+		}
115 115
 
116
-        $result = $permissionNames
117
-            ? implode(', ', $permissionNames)
118
-            : _t(__CLASS__ . '.NOPERMISSIONS', 'No Permissions');
116
+		$result = $permissionNames
117
+			? implode(', ', $permissionNames)
118
+			: _t(__CLASS__ . '.NOPERMISSIONS', 'No Permissions');
119 119
         
120
-        if (class_exists(Subsite::class)) {
121
-            Subsite::disable_subsite_filter(false);
122
-        }
123
-        return $result;
124
-    }
120
+		if (class_exists(Subsite::class)) {
121
+			Subsite::disable_subsite_filter(false);
122
+		}
123
+		return $result;
124
+	}
125 125
 }
Please login to merge, or discard this patch.