Completed
Pull Request — master (#29)
by Robbie
03:55
created

MemberReportExtension::getPermissionsDescription()   C

Complexity

Conditions 8
Paths 40

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 21
nc 40
nop 0
1
<?php
2
namespace SilverStripe\SecurityReport;
3
4
use SilverStripe\ORM\DataExtension;
5
use SilverStripe\Security\Permission;
6
use SilverStripe\Subsites\Model\Subsite;
7
8
/**
9
 * Extends the {@see Member} class with additional descriptions for elements.
10
 * See {@see UserSecurityReport} for usage.
11
 */
12
class MemberReportExtension extends DataExtension
13
{
14
    
15
    /**
16
     * Set cast of additional fields
17
     *
18
     * @var array
19
     * @config
20
     */
21
    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...
22
        'GroupsDescription' => 'Text',
23
        'PermissionsDescription' => 'Text'
24
    );
25
26
    /**
27
     * Builds a comma separated list of member group names for a given Member.
28
     *
29
     * @return string
30
     */
31
    public function getGroupsDescription()
32
    {
33
        if (class_exists(Subsite::class)) {
34
            Subsite::disable_subsite_filter(true);
35
        }
36
        
37
        // Get the member's groups, if any
38
        $groups = $this->owner->Groups();
39
        if ($groups->Count()) {
40
            // Collect the group names
41
            $groupNames = array();
42
            foreach ($groups as $group) {
43
                $groupNames[] = html_entity_decode($group->getTreeTitle());
44
            }
45
            // return a csv string of the group names, sans-markup
46
            $result = preg_replace("#</?[^>]>#", '', implode(', ', $groupNames));
47
        } else {
48
            // If no groups then return a status label
49
            $result = _t(__CLASS__ . '.NOGROUPS', 'Not in a Security Group');
50
        }
51
        
52
        if (class_exists(Subsite::class)) {
53
            Subsite::disable_subsite_filter(false);
54
        }
55
        return $result;
56
    }
57
    
58
    /**
59
     * Builds a comma separated list of human-readbale permissions for a given Member.
60
     *
61
     * @return string
62
     */
63
    public function getPermissionsDescription()
64
    {
65
        if (class_exists(Subsite::class)) {
66
            Subsite::disable_subsite_filter(true);
67
        }
68
        
69
        $permissionsUsr = Permission::permissions_for_member($this->owner->ID);
70
        $permissionsSrc = Permission::get_codes(true);
71
        sort($permissionsUsr);
72
        
73
        $permissionNames = array();
74
        foreach ($permissionsUsr as $code) {
75
            $code = strtoupper($code);
76
            foreach ($permissionsSrc as $k => $v) {
77
                if (isset($v[$code])) {
78
                    $name = empty($v[$code]['name'])
79
                        ? _t(__CLASS__ . '.UNKNOWN', 'Unknown')
80
                        : $v[$code]['name'];
81
                    $permissionNames[] = $name;
82
                }
83
            }
84
        }
85
86
        $result = $permissionNames
87
            ? implode(', ', $permissionNames)
88
            : _t(__CLASS__ . '.NOPERMISSIONS', 'No Permissions');
89
        
90
        if (class_exists(Subsite::class)) {
91
            Subsite::disable_subsite_filter(false);
92
        }
93
        return $result;
94
    }
95
}
96