Completed
Push — master ( 24a71d...4147fa )
by Robbie
9s
created

MemberReportExtension::getLastLoggedIn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
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 = [
0 ignored issues
show
Unused Code introduced by
The property $has_many 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
        'LoginAttempts' => LoginAttempt::class
28
    ];
29
    
30
    /**
31
     * Set cast of additional fields
32
     *
33
     * @var array
34
     * @config
35
     */
36
    private static $casting = array(
0 ignored issues
show
Unused Code introduced by
The property $casting 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...
37
        'GroupsDescription' => 'Text',
38
        'PermissionsDescription' => 'Text'
39
    );
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();
52
53
        return $lastTime ? $lastTime->dbObject('Created') : _t(__CLASS__ . '.NEVER', 'Never');
54
    }
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
        }
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
        }
81
        
82
        if (class_exists(Subsite::class)) {
83
            Subsite::disable_subsite_filter(false);
84
        }
85
        return $result;
86
    }
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
        }
98
        
99
        $permissionsUsr = Permission::permissions_for_member($this->owner->ID);
100
        $permissionsSrc = Permission::get_codes(true);
101
        sort($permissionsUsr);
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
        }
115
116
        $result = $permissionNames
117
            ? implode(', ', $permissionNames)
118
            : _t(__CLASS__ . '.NOPERMISSIONS', 'No Permissions');
119
        
120
        if (class_exists(Subsite::class)) {
121
            Subsite::disable_subsite_filter(false);
122
        }
123
        return $result;
124
    }
125
}
126