MemberReportExtension   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 6
dl 0
loc 105
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLastLoggedIn() 0 15 2
A getGroupsDescription() 0 27 5
B getPermissionsDescription() 0 32 8
1
<?php
2
namespace SilverStripe\SecurityReport;
3
4
use SilverStripe\ORM\DataExtension;
5
use SilverStripe\ORM\FieldType\DBDatetime;
6
use SilverStripe\Security\Group;
7
use SilverStripe\Security\Permission;
8
use SilverStripe\Security\LoginAttempt;
9
use SilverStripe\Subsites\Model\Subsite;
10
11
/**
12
 * Extends the {@see Member} class with additional descriptions for elements.
13
 * See {@see UserSecurityReport} for usage.
14
 */
15
class MemberReportExtension extends DataExtension
16
{
17
    /**
18
     * Set cast of additional fields
19
     *
20
     * @var array
21
     * @config
22
     */
23
    private static $casting = array(
24
        'GroupsDescription' => 'Text',
25
        'PermissionsDescription' => 'Text'
26
    );
27
28
    /**
29
     * Retrieves the most recent successful LoginAttempt
30
     *
31
     * @return DBDatetime|string
32
     */
33
    public function getLastLoggedIn()
34
    {
35
        $lastTime = LoginAttempt::get()
36
            ->filter([
37
                'MemberID' => $this->owner->ID,
38
                'Status' => 'Success',
39
            ])
40
            ->sort('Created', 'DESC')
41
            ->first();
42
43
        if ($lastTime) {
44
            return $lastTime->dbObject('Created')->format(DBDatetime::ISO_DATETIME);
45
        }
46
        return _t(__CLASS__ . '.NEVER', 'Never');
47
    }
48
49
    /**
50
     * Builds a comma separated list of member group names for a given Member.
51
     *
52
     * @return string
53
     */
54
    public function getGroupsDescription()
55
    {
56
        if (class_exists(Subsite::class)) {
57
            Subsite::disable_subsite_filter(true);
58
        }
59
60
        // Get the member's groups, if any
61
        $groups = $this->owner->Groups();
62
        if ($groups->Count()) {
63
            // Collect the group names
64
            $groupNames = array();
65
            foreach ($groups as $group) {
66
                /** @var Group $group */
67
                $groupNames[] = html_entity_decode($group->getTreeTitle());
68
            }
69
            // return a csv string of the group names, sans-markup
70
            $result = preg_replace("#</?[^>]>#", '', implode(', ', $groupNames));
71
        } else {
72
            // If no groups then return a status label
73
            $result = _t(__CLASS__ . '.NOGROUPS', 'Not in a Security Group');
74
        }
75
76
        if (class_exists(Subsite::class)) {
77
            Subsite::disable_subsite_filter(false);
78
        }
79
        return $result;
80
    }
81
82
    /**
83
     * Builds a comma separated list of human-readbale permissions for a given Member.
84
     *
85
     * @return string
86
     */
87
    public function getPermissionsDescription()
88
    {
89
        if (class_exists(Subsite::class)) {
90
            Subsite::disable_subsite_filter(true);
91
        }
92
93
        $permissionsUsr = Permission::permissions_for_member($this->owner->ID);
94
        $permissionsSrc = Permission::get_codes(true);
95
        sort($permissionsUsr);
96
97
        $permissionNames = array();
98
        foreach ($permissionsUsr as $code) {
99
            $code = strtoupper($code);
100
            foreach ($permissionsSrc as $k => $v) {
101
                if (isset($v[$code])) {
102
                    $name = empty($v[$code]['name'])
103
                        ? _t(__CLASS__ . '.UNKNOWN', 'Unknown')
104
                        : $v[$code]['name'];
105
                    $permissionNames[] = $name;
106
                }
107
            }
108
        }
109
110
        $result = $permissionNames
111
            ? implode(', ', $permissionNames)
112
            : _t(__CLASS__ . '.NOPERMISSIONS', 'No Permissions');
113
114
        if (class_exists(Subsite::class)) {
115
            Subsite::disable_subsite_filter(false);
116
        }
117
        return $result;
118
    }
119
}
120