/
UserInfoRequest.php
98 lines (83 loc) · 1.83 KB
/
UserInfoRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace MediaWiki\Extension\LDAPProvider;
use Config;
use MWException;
class UserInfoRequest {
/**
*
* @var Client
*/
protected $ldapClient = null;
/**
*
* @var Config
*/
protected $config = null;
/**
*
* @var string
*/
protected $userBaseDN = '';
/**
*
* @var string
*/
protected $searchAttribute = '';
/**
* @param Client $ldapClient to use
* @param Config $config for retrieving config from
*/
public function __construct( Client $ldapClient, Config $config ) {
$this->ldapClient = $ldapClient;
$this->config = $config;
$this->userBaseDN = $config->get( ClientConfig::USER_BASE_DN );
$this->searchAttribute = $config->get(
ClientConfig::USER_DN_SEARCH_ATTR
);
}
/**
* @param string $username to get info for
* @return array
*/
public function getUserInfo( $username ) {
$escapedUserName = new EscapedString( $username );
// We need to do a subbase search for the entry
$filter = "({$this->searchAttribute}=$escapedUserName)";
// We explicitly put "memberof" here because it's an operational
// attribute in some servers.
$attributes = [ "*", "memberof" ];
$entry = $this->ldapClient->search(
$filter,
$this->userBaseDN,
$attributes
);
$count = $entry['count'];
if ( $count == 0 ) {
return [];
}
if ( $count > 1 ) {
throw new MWException(
wfMessage( "ldapprovider-more-than-one" )->params( $filter )->plain()
);
}
$res = [];
foreach ( $entry[0] as $key => $value ) {
if ( $key === 'dn' ) {
$res[$key] = $value;
} elseif ( !is_int( $key ) && $key !== "count" ) {
if ( $value['count'] === 1 ) {
$res[$key] = $value[0];
} else {
$res[$key] = array_filter(
$value,
function ( $thisKey ) {
return is_int( $thisKey );
},
ARRAY_FILTER_USE_KEY
);
}
}
}
return $res;
}
}