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
123namespace Legislator \LegislatorBundle \Integration ;
134use 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}
0 commit comments