1
|
|
|
<?php |
2
|
|
|
namespace SilverStripe\SecurityReport; |
3
|
|
|
|
4
|
|
|
use SilverStripe\ORM\DataExtension; |
5
|
|
|
use SilverStripe\Security\Permission; |
6
|
|
|
use SilverStripe\Security\LoginAttempt; |
7
|
|
|
use SilverStripe\Subsites\Model\Subsite; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Extends the {@see Member} class with additional descriptions for elements. |
11
|
|
|
* See {@see UserSecurityReport} for usage. |
12
|
|
|
*/ |
13
|
|
|
class MemberReportExtension extends DataExtension |
14
|
|
|
{ |
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
|
|
|
]; |
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
|
|
|
); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Retrieves the most recent successful LoginAttempt |
43
|
|
|
*/ |
44
|
|
|
public function getLastLoggedIn() |
45
|
|
|
{ |
46
|
|
|
$lastTime = $this->owner->LoginAttempts() |
47
|
|
|
->filter('Status', 'Success') |
48
|
|
|
->sort('Created', 'DESC') |
49
|
|
|
->first(); |
50
|
|
|
|
51
|
|
|
return $lastTime ?: _t(__CLASS__ . '.NEVER', 'never'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Builds a comma separated list of member group names for a given Member. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getGroupsDescription() |
60
|
|
|
{ |
61
|
|
|
if (class_exists(Subsite::class)) { |
62
|
|
|
Subsite::disable_subsite_filter(true); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Get the member's groups, if any |
66
|
|
|
$groups = $this->owner->Groups(); |
67
|
|
|
if ($groups->Count()) { |
68
|
|
|
// Collect the group names |
69
|
|
|
$groupNames = array(); |
70
|
|
|
foreach ($groups as $group) { |
71
|
|
|
$groupNames[] = html_entity_decode($group->getTreeTitle()); |
72
|
|
|
} |
73
|
|
|
// return a csv string of the group names, sans-markup |
74
|
|
|
$result = preg_replace("#</?[^>]>#", '', implode(', ', $groupNames)); |
75
|
|
|
} else { |
76
|
|
|
// If no groups then return a status label |
77
|
|
|
$result = _t(__CLASS__ . '.NOGROUPS', 'Not in a Security Group'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (class_exists(Subsite::class)) { |
81
|
|
|
Subsite::disable_subsite_filter(false); |
82
|
|
|
} |
83
|
|
|
return $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Builds a comma separated list of human-readbale permissions for a given Member. |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getPermissionsDescription() |
92
|
|
|
{ |
93
|
|
|
if (class_exists(Subsite::class)) { |
94
|
|
|
Subsite::disable_subsite_filter(true); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$permissionsUsr = Permission::permissions_for_member($this->owner->ID); |
98
|
|
|
$permissionsSrc = Permission::get_codes(true); |
99
|
|
|
sort($permissionsUsr); |
100
|
|
|
|
101
|
|
|
$permissionNames = array(); |
102
|
|
|
foreach ($permissionsUsr as $code) { |
103
|
|
|
$code = strtoupper($code); |
104
|
|
|
foreach ($permissionsSrc as $k => $v) { |
105
|
|
|
if (isset($v[$code])) { |
106
|
|
|
$name = empty($v[$code]['name']) |
107
|
|
|
? _t(__CLASS__ . '.UNKNOWN', 'Unknown') |
108
|
|
|
: $v[$code]['name']; |
109
|
|
|
$permissionNames[] = $name; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$result = $permissionNames |
115
|
|
|
? implode(', ', $permissionNames) |
116
|
|
|
: _t(__CLASS__ . '.NOPERMISSIONS', 'No Permissions'); |
117
|
|
|
|
118
|
|
|
if (class_exists(Subsite::class)) { |
119
|
|
|
Subsite::disable_subsite_filter(false); |
120
|
|
|
} |
121
|
|
|
return $result; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.