Skip to content

Commit 8fe267a

Browse files
committed
searching in all users in uniba's LDAP (not just employees)
1 parent d120abb commit 8fe267a

File tree

3 files changed

+25
-57
lines changed

3 files changed

+25
-57
lines changed

src/Legislator/LegislatorBundle/Integration/LDAPTeacherSearch.php renamed to src/Legislator/LegislatorBundle/Integration/LDAPUserSearch.php

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,30 @@
11
<?php
2-
/**
3-
* @copyright Copyright (c) 2013 The FMFI Anketa authors (see AUTHORS).
4-
* Use of this source code is governed by a license that can be
5-
* found in the LICENSE file in the project root directory.
6-
*
7-
* @package Anketa
8-
* @subpackage Integration
9-
* @author Martin Kralik <[email protected]>
10-
*/
112

123
namespace Legislator\LegislatorBundle\Integration;
134
use Legislator\LegislatorBundle\Integration\LDAPRetriever;
145

156
/**
16-
* Searches LDAP for teachers.
7+
* Searches LDAP for any users (teachers or students).
178
*
18-
* @author Martin Kralik <[email protected]>
199
*/
20-
class LDAPTeacherSearch {
10+
class LDAPUserSearch {
2111

2212
private $ldap;
2313
private $orgUnit;
2414
const GROUP_REGEXP = '@^pouzivatelia_(?P<orgUnits>[a-zA-Z]+)(?<!interni|externi)$@';
2515

26-
public function __construct(LDAPRetriever $ldap, $orgUnit) {
16+
public function __construct(LDAPRetriever $ldap, $orgUnit)
17+
{
2718
$this->ldap = $ldap;
2819
$this->ldap->loginIfNotAlready();
2920
$this->orgUnit = $orgUnit;
3021
}
3122

32-
public function __destruct() {
23+
public function __destruct()
24+
{
3325
$this->ldap->logoutIfNotAlready();
3426
}
3527

36-
/**
37-
* Trims and transliterate string with accents into ASCII.
38-
*
39-
* @param string $string
40-
* @return string
41-
*/
42-
private function removeAccents($string) {
43-
44-
if (function_exists('iconv')) {
45-
$string = iconv('utf-8', 'us-ascii//TRANSLIT', trim($string));
46-
}
47-
return $string;
48-
}
49-
50-
/**
51-
* Searches LDAP for users by substring of their full name (without accents).
52-
* In addition, users must be either teachers on any faculty or PhD students
53-
* on faculty provided in class constructor.
54-
*
55-
* @param string $name Substring of name
56-
* @return array @see executeSeachAndProcessData for docs
57-
*/
58-
public function byFullName($name) {
59-
$safeName = $this->removeAccents($this->ldap->escape($name));
60-
$safeOrgUnit = $this->ldap->escape($this->orgUnit);
61-
$filter = '(&(cn=*'.$safeName.'*)(|(group=zamestnanci)(group=doktorandi_'.$safeOrgUnit.')))';
62-
63-
return $this->executeSeachAndProcessData($filter);
64-
}
65-
6628
/**
6729
* Searches LDAP for user(s) based on a full login.
6830
* In addition, users must be either teachers on any faculty or PhD students
@@ -71,10 +33,15 @@ public function byFullName($name) {
7133
* @param string $login full login
7234
* @return array @see executeSeachAndProcessData for docs
7335
*/
74-
public function byLogin($login) {
36+
public function byLogin($login, $only_from_orgunit=TRUE)
37+
{
7538
$safeLogin = $this->ldap->escape($login);
76-
$safeOrgUnit = $this->ldap->escape($this->orgUnit);
77-
$filter = '(&(uid='.$safeLogin.')(|(group=zamestnanci)(group=doktorandi_'.$safeOrgUnit.')))';
39+
$filter_org = '';
40+
if ($only_from_orgunit) {
41+
$safeOrgUnit = $this->ldap->escape($this->orgUnit);
42+
$filter_org = '_'.$safeOrgUnit;
43+
}
44+
$filter = '(&(uid='.$safeLogin.')(group=pouzivatelia'.$filter_org.'))';
7845

7946
return $this->executeSeachAndProcessData($filter);
8047
}
@@ -107,25 +74,26 @@ public function byLogin($login) {
10774
* @param string $filter
10875
* @return array
10976
*/
110-
private function executeSeachAndProcessData($filter) {
77+
private function executeSeachAndProcessData($filter)
78+
{
11179
$result = $this->ldap->searchAll($filter,
11280
array('displayName', 'uid', 'group', 'givenNameU8', 'snU8'));
11381

114-
$teachers = array();
82+
$users = array();
11583
foreach ($result as $record) {
116-
$teachers[$record['uid'][0]]['name'] = $record['displayName'][0];
117-
$teachers[$record['uid'][0]]['givenName'] = $record['givenNameU8'][0];
118-
$teachers[$record['uid'][0]]['familyName'] = $record['snU8'][0];
84+
$users[$record['uid'][0]]['name'] = $record['displayName'][0];
85+
$users[$record['uid'][0]]['givenName'] = $record['givenNameU8'][0];
86+
$users[$record['uid'][0]]['familyName'] = $record['snU8'][0];
11987
$orgUnits = array();
12088
foreach ($record['group'] as $group) {
12189
$match = array();
12290
if (preg_match(self::GROUP_REGEXP, $group, $match)) {
12391
$orgUnits[] = $match['orgUnits'];
12492
}
12593
}
126-
$teachers[$record['uid'][0]]['orgUnits'] = $orgUnits;
94+
$users[$record['uid'][0]]['orgUnits'] = $orgUnits;
12795
}
128-
return $teachers;
96+
return $users;
12997
}
13098

13199
}

src/Legislator/LegislatorBundle/Resources/config/services.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ services:
2222
class: Legislator\LegislatorBundle\Integration\LDAPRetriever
2323
arguments: ["%ldap_url%", "%ldap_base_dn%"]
2424

25-
legislator.teacher_search:
26-
class: Legislator\LegislatorBundle\Integration\LDAPTeacherSearch
25+
legislator.user_search:
26+
class: Legislator\LegislatorBundle\Integration\LDAPUserSearch
2727
arguments: ["@legislator.ldap_retriever", "%org_unit%"]

src/Legislator/LegislatorBundle/Security/LegislatorUserProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function loadUserByUsername($username)
2828
$user = $this->findUser($username);
2929

3030
if ($this->cosign_login_enabled) {
31-
$ldapSearch = $this->container->get('legislator.teacher_search');
31+
$ldapSearch = $this->container->get('legislator.user_search');
3232
$user_info = $ldapSearch->byLogin($username);
3333
if (!array_key_exists($username, $user_info)) {
3434
throw new UsernameNotFoundException(sprintf('Username "%s" not found in LDAP.', $username));

0 commit comments

Comments
 (0)