Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit 5e9dfea

Browse files
authored
Merge pull request #34 from pajavyskocil/Connectors
Rewrited Adapters and Connectors
2 parents c196bba + fbfe943 commit 5e9dfea

File tree

7 files changed

+149
-81
lines changed

7 files changed

+149
-81
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ All notable changes to this project will be documented in this file.
66
- Added badges to README
77
- Added new property to Facility model: description
88

9+
[Changed]
10+
- Connectors methods are not static for now.
11+
- Added constructors to Adapters, which allows specified config file for each connections.
12+
913
## [v1.0.0]
1014

1115
[Unreleased]: https://github.com/CESNET/perun-simplesamlphp-module/tree/master

lib/Adapter.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ abstract class sspmod_perun_Adapter
99
const RPC = 'rpc';
1010
const LDAP = 'ldap';
1111

12+
/**
13+
* @var sspmod_perun_RpcConnector | sspmod_perun_LdapConnector
14+
*/
15+
protected $connector;
16+
17+
public function getConnector(){
18+
return $this->connector;
19+
}
20+
1221
/**
1322
* @param string $interface code of interface. Check constants of this class.
1423
* @return sspmod_perun_Adapter instance of this class. note it is NOT singleton.

lib/AdapterLdap.php

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,46 @@
33
/**
44
* Class sspmod_perun_AdapterLdap
55
*
6+
* Configuration file should be placed in default config folder of SimpleSAMLphp.
7+
* Example of file is in config-template folder.
8+
*
69
* Perun adapter which uses Perun LDAP interface
10+
* @author Ondrej Velisek <[email protected]>
11+
* @author Michal Prochazka <[email protected]>
12+
* @author Pavel Vyskocil <[email protected]>
713
*/
814
class sspmod_perun_AdapterLdap extends sspmod_perun_Adapter
915
{
16+
const DEFAULT_CONFIG_FILE_NAME = 'module_perun.php';
17+
const LDAP_HOSTNAME = 'ldap.hostname';
18+
const LDAP_USER = 'ldap.username';
19+
const LDAP_PASSWORD = 'ldap.password';
20+
const LDAP_BASE = 'ldap.base';
1021

22+
private $ldapHostname;
23+
private $ldapUser;
24+
private $ldapPassword;
1125
private $ldapBase;
1226

13-
const CONFIG_FILE_NAME = 'module_perun.php';
14-
const LDAP_BASE = 'ldap.base';
27+
protected $connector;
1528

16-
public function __construct ()
29+
public function __construct ($configFileName = null)
1730
{
18-
$conf = SimpleSAML_Configuration::getConfig(self::CONFIG_FILE_NAME);
31+
if (is_null($configFileName)) {
32+
$configFileName = self::DEFAULT_CONFIG_FILE_NAME;
33+
}
34+
35+
$conf = SimpleSAML_Configuration::getConfig($configFileName);
36+
37+
$this->ldapHostname = $conf->getString(self::LDAP_HOSTNAME);
38+
$this->ldapUser = $conf->getString(self::LDAP_USER);
39+
$this->ldapPassword = $conf->getString(self::LDAP_PASSWORD);
1940
$this->ldapBase = $conf->getString(self::LDAP_BASE);
41+
42+
43+
$this->connector = new sspmod_perun_LdapConnector($this->ldapHostname, $this->ldapUser, $this->ldapPassword);
2044
}
45+
2146
public function getPerunUser($idpEntityId, $uids)
2247
{
2348
# Build a LDAP query, we are searching for the user who has at least one of the uid
@@ -30,7 +55,7 @@ public function getPerunUser($idpEntityId, $uids)
3055
return null;
3156
}
3257

33-
$user = sspmod_perun_LdapConnector::searchForEntity("ou=People," . $this->ldapBase,
58+
$user = $this->connector->searchForEntity("ou=People," . $this->ldapBase,
3459
"(|$query)",
3560
array("perunUserId", "displayName", "cn", "givenName", "sn", "preferredMail", "mail")
3661
);
@@ -52,7 +77,7 @@ public function getPerunUser($idpEntityId, $uids)
5277
public function getMemberGroups($user, $vo)
5378
{
5479
$userId = $user->getId();
55-
$userWithMembership = sspmod_perun_LdapConnector::searchForEntity("perunUserId=$userId,ou=People," . $this->ldapBase,
80+
$userWithMembership = $this->connector->searchForEntity("perunUserId=$userId,ou=People," . $this->ldapBase,
5681
"(objectClass=perunUser)",
5782
array("perunUserId", "memberOf")
5883
);
@@ -64,7 +89,7 @@ public function getMemberGroups($user, $vo)
6489
continue;
6590
}
6691

67-
$group = sspmod_perun_LdapConnector::searchForEntity($groupDn,
92+
$group = $this->connector->searchForEntity($groupDn,
6893
"(objectClass=perunGroup)",
6994
array("perunGroupId", "cn", "perunUniqueGroupName", "perunVoId", "description")
7095
);
@@ -77,15 +102,15 @@ public function getMemberGroups($user, $vo)
77102

78103
public function getSpGroups($spEntityId, $vo)
79104
{
80-
$resources = sspmod_perun_LdapConnector::searchForEntities($this->ldapBase,
105+
$resources = $this->connector->searchForEntities($this->ldapBase,
81106
"(&(objectClass=perunResource)(entityID=$spEntityId))",
82107
array("perunResourceId", "assignedGroupId", "perunVoId")
83108
);
84109

85110
$groups = array();
86111
foreach ($resources as $resource) {
87112
foreach ($resource['assignedGroupId'] as $groupId) {
88-
$group = sspmod_perun_LdapConnector::searchForEntity("perunGroupId=$groupId,perunVoId=" . $resource['perunVoId'][0] . "," . $this->ldapBase,
113+
$group = $this->connector->searchForEntity("perunGroupId=$groupId,perunVoId=" . $resource['perunVoId'][0] . "," . $this->ldapBase,
89114
"(objectClass=perunGroup)",
90115
array("perunGroupId", "cn", "perunUniqueGroupName", "perunVoId", "description")
91116
);
@@ -102,7 +127,7 @@ public function getSpGroups($spEntityId, $vo)
102127
public function getGroupByName($vo, $name)
103128
{
104129
$voId = $vo->getId();
105-
$group = sspmod_perun_LdapConnector::searchForEntity("perunVoId=$voId," . $this->ldapBase,
130+
$group = $this->connector->searchForEntity("perunVoId=$voId," . $this->ldapBase,
106131
"(&(objectClass=perunGroup)(perunUniqueGroupName=$name))",
107132
array("perunGroupId", "cn", "perunUniqueGroupName", "perunVoId", "description")
108133
);
@@ -116,7 +141,7 @@ public function getGroupByName($vo, $name)
116141

117142
public function getVoByShortName($voShortName)
118143
{
119-
$vo = sspmod_perun_LdapConnector::searchForEntity($this->ldapBase,
144+
$vo = $this->connector->searchForEntity($this->ldapBase,
120145
"(&(objectClass=perunVo)(o=$voShortName))",
121146
array("perunVoId", "o", "description")
122147
);
@@ -131,7 +156,7 @@ public function getVoByShortName($voShortName)
131156
public function getUserAttributes($user, $attrNames)
132157
{
133158
$userId = $user->getId();
134-
$attributes = sspmod_perun_LdapConnector::searchForEntity("perunUserId=$userId,ou=People," . $this->ldapBase,
159+
$attributes = $this->connector->searchForEntity("perunUserId=$userId,ou=People," . $this->ldapBase,
135160
"(objectClass=perunUser)",
136161
$attrNames
137162
);
@@ -165,7 +190,7 @@ public function getFacilityAttribute($facility, $attrName)
165190

166191
public function getUsersGroupsOnFacility($spEntityId, $userId)
167192
{
168-
$resources = sspmod_perun_LdapConnector::searchForEntities($this->ldapBase,
193+
$resources = $this->connector->searchForEntities($this->ldapBase,
169194
"(&(objectClass=perunResource)(entityID=$spEntityId))",
170195
array("perunResourceId")
171196
);
@@ -181,7 +206,7 @@ public function getUsersGroupsOnFacility($spEntityId, $userId)
181206
$resourcesString .= ")";
182207

183208
$resultGroups = array();
184-
$groups = sspmod_perun_LdapConnector::searchForEntities($this->ldapBase,
209+
$groups = $this->connector->searchForEntities($this->ldapBase,
185210
"(&(uniqueMember=perunUserId=".$userId.", ou=People," . $this->ldapBase. ")".$resourcesString.")",
186211
array("perunGroupId", "cn", "perunUniqueGroupName", "perunVoId", "description")
187212
);

lib/AdapterRpc.php

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,39 @@
99
*/
1010
class sspmod_perun_AdapterRpc extends sspmod_perun_Adapter
1111
{
12+
const DEFAULT_CONFIG_FILE_NAME = 'module_perun.php';
13+
const RPC_URL = 'rpc.url';
14+
const RPC_USER = 'rpc.username';
15+
const RPC_PASSWORD = 'rpc.password';
1216

17+
private $rpcUrl;
18+
private $rpcUser;
19+
private $rpcPassword;
1320

21+
protected $connector;
22+
23+
public function __construct ($configFileName = null)
24+
{
25+
if (is_null($configFileName)) {
26+
$configFileName = self::DEFAULT_CONFIG_FILE_NAME;
27+
}
28+
29+
$conf = SimpleSAML_Configuration::getConfig($configFileName);
30+
31+
$this->rpcUrl = $conf->getString(self::RPC_URL);
32+
$this->rpcUser = $conf->getString(self::RPC_USER);
33+
$this->rpcPassword = $conf->getString(self::RPC_PASSWORD);
34+
35+
$this->connector = new sspmod_perun_RpcConnector($this->rpcUrl, $this->rpcUser, $this->rpcPassword);
36+
}
37+
1438
public function getPerunUser($idpEntityId, $uids)
1539
{
1640
$user = null;
1741

1842
foreach ($uids as $uid) {
1943
try {
20-
$user = sspmod_perun_RpcConnector::get('usersManager', 'getUserByExtSourceNameAndExtLogin', array(
44+
$user = $this->connector->get('usersManager', 'getUserByExtSourceNameAndExtLogin', array(
2145
'extSourceName' => $idpEntityId,
2246
'extLogin' => $uid,
2347
));
@@ -49,13 +73,13 @@ public function getPerunUser($idpEntityId, $uids)
4973
public function getMemberGroups($user, $vo)
5074
{
5175
try {
52-
$member = sspmod_perun_RpcConnector::get('membersManager', 'getMemberByUser', array(
76+
$member = $this->connector->get('membersManager', 'getMemberByUser', array(
5377
'vo' => $vo->getId(),
5478
'user' => $user->getId(),
5579
));
5680

5781

58-
$memberGroups = sspmod_perun_RpcConnector::get('groupsManager', 'getAllMemberGroups', array(
82+
$memberGroups = $this->connector->get('groupsManager', 'getAllMemberGroups', array(
5983
'member' => $member['id'],
6084
));
6185
} catch (sspmod_perun_Exception $e) {
@@ -73,15 +97,15 @@ public function getMemberGroups($user, $vo)
7397

7498
public function getSpGroups($spEntityId, $vo)
7599
{
76-
$resources = sspmod_perun_RpcConnector::get('resourcesManager', 'getResources', array(
100+
$resources = $this->connector->get('resourcesManager', 'getResources', array(
77101
'vo' => $vo->getId(),
78102
));
79103

80104
$spFacilityIds = array();
81105
$spResources = array();
82106
foreach ($resources as $resource) {
83107
if (!array_key_exists($resource['facilityId'], $spFacilityIds)) {
84-
$attribute = sspmod_perun_RpcConnector::get('attributesManager', 'getAttribute', array(
108+
$attribute = $this->connector->get('attributesManager', 'getAttribute', array(
85109
'facility' => $resource['facilityId'],
86110
'attributeName' => 'urn:perun:facility:attribute-def:def:entityID',
87111
));
@@ -98,7 +122,7 @@ public function getSpGroups($spEntityId, $vo)
98122

99123
$spGroups = array();
100124
foreach ($spResources as $spResource) {
101-
$groups = sspmod_perun_RpcConnector::get('resourcesManager', 'getAssignedGroups', array(
125+
$groups = $this->connector->get('resourcesManager', 'getAssignedGroups', array(
102126
'resource' => $spResource['id'],
103127
));
104128
$convertedGroups = array();
@@ -116,7 +140,7 @@ public function getSpGroups($spEntityId, $vo)
116140

117141
public function getGroupByName($vo, $name)
118142
{
119-
$group = sspmod_perun_RpcConnector::get('groupsManager', 'getGroupByName', array(
143+
$group = $this->connector->get('groupsManager', 'getGroupByName', array(
120144
'vo' => $vo->getId(),
121145
'name' => $name,
122146
));
@@ -127,7 +151,7 @@ public function getGroupByName($vo, $name)
127151

128152
public function getVoByShortName($voShortName)
129153
{
130-
$vo = sspmod_perun_RpcConnector::get('vosManager', 'getVoByShortName', array(
154+
$vo = $this->connector->get('vosManager', 'getVoByShortName', array(
131155
'shortName' => $voShortName,
132156
));
133157

@@ -137,7 +161,7 @@ public function getVoByShortName($voShortName)
137161

138162
public function getUserAttributes($user, $attrNames)
139163
{
140-
$perunAttrs = sspmod_perun_RpcConnector::get('attributesManager', 'getAttributes', array(
164+
$perunAttrs = $this->connector->get('attributesManager', 'getAttributes', array(
141165
'user' => $user->getId(),
142166
'attrNames' => $attrNames,
143167
));
@@ -155,7 +179,7 @@ public function getUserAttributes($user, $attrNames)
155179

156180
public function getEntitylessAttribute($attrName)
157181
{
158-
$perunAttrs = sspmod_perun_RpcConnector::get('attributesManager', 'getEntitylessAttributes', array(
182+
$perunAttrs = $this->connector->get('attributesManager', 'getEntitylessAttributes', array(
159183
'attrName' => $attrName,
160184
));
161185

@@ -170,7 +194,7 @@ public function getEntitylessAttribute($attrName)
170194

171195
public function getVoAttributes($vo, $attrNames)
172196
{
173-
$perunAttrs = sspmod_perun_RpcConnector::get('attributesManager', 'getAttributes', array(
197+
$perunAttrs = $this->connector->get('attributesManager', 'getAttributes', array(
174198
'vo' => $vo->getId(),
175199
'attrNames' => $attrNames,
176200
));
@@ -188,7 +212,7 @@ public function getVoAttributes($vo, $attrNames)
188212

189213
public function getFacilityAttribute($facility, $attrName)
190214
{
191-
$perunAttr = sspmod_perun_RpcConnector::get('attributesManager', 'getAttribute', array(
215+
$perunAttr = $this->connector->get('attributesManager', 'getAttribute', array(
192216
'facility' => $facility->getId(),
193217
'attributeName' => $attrName,
194218
));
@@ -197,22 +221,22 @@ public function getFacilityAttribute($facility, $attrName)
197221
}
198222

199223

200-
public function getUsersGroupsOnFacility($spEntityId, $userId)
224+
public function getUsersGroupsOnFacility($spEntityId, $userId)
201225
{
202-
$facilities = sspmod_perun_RpcConnector::get('facilitiesManager', 'getFacilitiesByAttribute', array(
226+
$facilities = $this->connector->get('facilitiesManager', 'getFacilitiesByAttribute', array(
203227
'attributeName' => 'urn:perun:facility:attribute-def:def:entityID',
204228
'attributeValue' => $spEntityId,
205229
));
206230

207231
$allowedResources = array();
208232
foreach ($facilities as $facility) {
209-
$resources = sspmod_perun_RpcConnector::get('facilitiesManager', 'getAssignedResources', array(
233+
$resources = $this->connector->get('facilitiesManager', 'getAssignedResources', array(
210234
'facility' => $facility['id'],
211235
));
212236
$allowedResources = array_merge($allowedResources, $resources);
213237
}
214238

215-
$members = sspmod_perun_RpcConnector::get('membersManager', 'getMembersByUser', array(
239+
$members = $this->connector->get('membersManager', 'getMembersByUser', array(
216240
'user' => $userId,
217241
));
218242

@@ -226,7 +250,7 @@ public function getUsersGroupsOnFacility($spEntityId, $userId)
226250
$allGroups = array();
227251
foreach ($allowedResources as $resource) {
228252
foreach ($validMembers as $member) {
229-
$groups = sspmod_perun_RpcConnector::get('resourcesManager', 'getAssignedGroups', array(
253+
$groups = $this->connector->get('resourcesManager', 'getAssignedGroups', array(
230254
'resource' => $resource['id'],
231255
'member' => $member['id'],
232256
));
@@ -242,7 +266,7 @@ public function getUsersGroupsOnFacility($spEntityId, $userId)
242266

243267
public function getFacilitiesByEntityId($spEntityId)
244268
{
245-
$perunAttrs = sspmod_perun_RpcConnector::get('facilitiesManager', 'getFacilitiesByAttribute', array(
269+
$perunAttrs = $this->connector->get('facilitiesManager', 'getFacilitiesByAttribute', array(
246270
'attributeName' => 'urn:perun:facility:attribute-def:def:entityID',
247271
'attributeValue' => $spEntityId,
248272
));
@@ -252,5 +276,4 @@ public function getFacilitiesByEntityId($spEntityId)
252276
}
253277
return $facilities;
254278
}
255-
256279
}

0 commit comments

Comments
 (0)