Passed
Push — master ( 52914c...5ecbc9 )
by Tim
03:03
created

SmartName   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 16

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 11 2
B getFullName() 0 36 11
A getLocalUser() 0 10 3
1
<?php
2
3
namespace SimpleSAML\Module\smartattributes\Auth\Process;
4
5
use Webmozart\Assert\Assert;
6
7
/**
8
 * Filter to set name in a smart way, based on available name attributes.
9
 *
10
 * @author Andreas Åkre Solberg, UNINETT AS.
11
 * @package SimpleSAMLphp
12
 */
13
class SmartName extends \SimpleSAML\Auth\ProcessingFilter
14
{
15
    /**
16
     * Attributes which should be added/appended.
17
     *
18
     * @var array Associative array of arrays.
19
     */
20
    private $attributes = [];
0 ignored issues
show
introduced by
The private property $attributes is not used, and could be removed.
Loading history...
21
22
23
    /**
24
     * @param array $attributes
25
     * @return string|null
26
     */
27
    private function getFullName($attributes)
28
    {
29
        if (isset($attributes['displayName'])) {
30
            return $attributes['displayName'][0];
31
        }
32
33
        if (isset($attributes['cn'])) {
34
            if (count(explode(' ', $attributes['cn'][0])) > 1) {
35
                return $attributes['cn'][0];
36
            }
37
        }
38
39
        if (isset($attributes['sn']) && isset($attributes['givenName'])) {
40
            return $attributes['givenName'][0].' '.$attributes['sn'][0];
41
        }
42
43
        if (isset($attributes['cn'])) {
44
            return $attributes['cn'][0];
45
        }
46
47
        if (isset($attributes['sn'])) {
48
            return $attributes['sn'][0];
49
        }
50
51
        if (isset($attributes['givenName'])) {
52
            return $attributes['givenName'][0];
53
        }
54
55
        if (isset($attributes['eduPersonPrincipalName'])) {
56
            $localname = $this->getLocalUser($attributes['eduPersonPrincipalName'][0]);
57
            if (isset($localname)) {
58
                return $localname;
59
            }
60
        }
61
62
        return null;
63
    }
64
65
66
    /**
67
     * @param string $userid
68
     * @return string|null
69
     */
70
    private function getLocalUser($userid)
71
    {
72
        if (strpos($userid, '@') === false) {
73
            return null;
74
        }
75
        $decomposed = explode('@', $userid);
76
        if (count($decomposed) === 2) {
77
            return $decomposed[0];
78
        }
79
        return null;
80
    }
81
82
83
    /**
84
     * Apply filter to add or replace attributes.
85
     *
86
     * Add or replace existing attributes with the configured values.
87
     *
88
     * @param array &$request  The current request
89
     * @return void
90
     */
91
    public function process(&$request)
92
    {
93
        Assert::isArray($request);
94
        Assert::keyExists($request, 'Attributes');
95
96
        $attributes = &$request['Attributes'];
97
98
        $fullname = $this->getFullName($attributes);
99
100
        if (isset($fullname)) {
101
            $request['Attributes']['smartname-fullname'] = [$fullname];
102
        }
103
    }
104
}
105