Permalink
Browse files

Updated with LDAP service class for searching users

  • Loading branch information...
1 parent bfc9971 commit bd445a3b9f4189e9ecd9c7526f47967786c08b38 @jfaustin jfaustin committed Apr 28, 2011
Showing with 306 additions and 17 deletions.
  1. +18 −17 Ncstate/Service/Dining.php
  2. +15 −0 Ncstate/Service/Exception.php
  3. +162 −0 Ncstate/Service/Ldap/Connector.php
  4. +111 −0 Ncstate/Service/User.php
View
@@ -1,10 +1,9 @@
<?php
/**
- * Set of classes to programatically communicate with the NC State University
- * Dining API
+ * Set of classes to programatically communicate with services at NC State
+ * University
*
- * @package Ncstate_Dining
- * @see http://webapps.ncsu.edu/dining
+ * @package Ncstate_Service
* @author Office of Information Technology - Outreach Technology
*/
@@ -15,6 +14,8 @@
* The class uses the public Dining API service provided by OIT
* to get the menus, locations, and hours for the various dining establishments
* on campus.
+ *
+ * @see http://webapps.ncsu.edu/dining
*
*/
class Ncstate_Service_Dining
@@ -144,7 +145,7 @@ public function setFormat($format)
* @param string $diet Optionally return the menu items that match a specific
* diet ('iron', 'weightGain', 'loseBodyFat', 'calcium', 'vegetarian', 'inactiveDay')
*
- * @throws Ncstate_Service_Dining_Exception
+ * @throws Ncstate_Service_Exception
*
* @return stdClass object from the request
*
@@ -156,8 +157,8 @@ public function getMenu($locationKey, $meal = null, $date = null, $diet = null)
$meal = strtolower($meal);
if (!in_array($meal, $this->_validMealTypes)) {
- require_once 'Ncstate/Service/Dining/Exception.php';
- throw new Ncstate_Service_Dining_Exception('Meal type must be one of "' . implode(', ', $this->_validMealTypes) . '"');
+ require_once 'Ncstate/Service/Exception.php';
+ throw new Ncstate_Service_Exception('Meal type must be one of "' . implode(', ', $this->_validMealTypes) . '"');
}
}
@@ -166,8 +167,8 @@ public function getMenu($locationKey, $meal = null, $date = null, $diet = null)
$diet = strtolower($diet);
if (!in_array($diet, $this->_validDietTypes)) {
- require_once 'Ncstate/Service/Dining/Exception.php';
- throw new Ncstate_Service_Dining_Exception('Diet type must be one of "' . implode(', ', $this->_validDietTypes) . '"');
+ require_once 'Ncstate/Service/Exception.php';
+ throw new Ncstate_Service_Exception('Diet type must be one of "' . implode(', ', $this->_validDietTypes) . '"');
}
}
@@ -197,7 +198,7 @@ public function getMenu($locationKey, $meal = null, $date = null, $diet = null)
* @param string $locationKey The key for the location for which to get the menu
* @param string $date Date in YYYY-M-D format or 'today' or 'tomorrow' (today by default)
*
- * @throws Ncstate_Service_Dining_Exception
+ * @throws Ncstate_Service_Exception
*
* @return stdClass object from the request
*
@@ -221,7 +222,7 @@ public function getHours($locationKey, $date = null)
*
* @param string $type A valid location type.
*
- * @throws Ncstate_Service_Dining_Exception
+ * @throws Ncstate_Service_Exception
*
* @return stdClass object from the request
*
@@ -240,7 +241,7 @@ public function getLocations($type = null)
/**
* Returns a list of all the location types.
*
- * @throws Ncstate_Service_Dining_Exception
+ * @throws Ncstate_Service_Exception
*
* @return stdClass object from the request
*
@@ -258,7 +259,7 @@ public function getLocationTypes()
* @param string $method The method to call
* @param array $args key value array of arguments
*
- * @throws Ncstate_Service_Dining_Exception
+ * @throws Ncstate_Service_Exception
*
* @return stdClass object
*/
@@ -297,8 +298,8 @@ protected function _request($method, $args)
if ($this->_lastRawResponse === false) {
$this->_lastRawResponse = curl_error($ch);
- require_once 'Ncstate/Service/Dining/Exception.php';
- throw new Ncstate_Service_Dining_Exception('CURL Error: ' . curl_error($ch));
+ require_once 'Ncstate/Service/Exception.php';
+ throw new Ncstate_Service_Exception('CURL Error: ' . curl_error($ch));
}
curl_close($ch);
@@ -324,8 +325,8 @@ protected function _request($method, $args)
// Server provides error messages in the 'status' field. It will either be 'success' or 'failure'
if (strtolower($this->_lastParsedResponse[$method]['status']) == 'failure') {
- require_once 'Ncstate/Service/Dining/Exception.php';
- throw new Bn_Service_Brewerydb_Exception('Dining Service Error: ' .
+ require_once 'Ncstate/Service/Exception.php';
+ throw new Ncstate_Service_Exception('Dining Service Error: ' .
$this->_lastParsedResponse['response']['message']);
}
@@ -0,0 +1,15 @@
+<?php
+/**
+ * Set of classes to programatically create certain aspects of the
+ * approved brand at NC State University
+ *
+ * @package Ncstate_Brand
+ * @see http://ncsu.edu/brand
+ * @author Office of Information Technology - Outreach Technology
+ */
+
+/**
+ * Exception class thrown by any of the services in this library
+ */
+class Ncstate_Service_Exception extends Exception
+{}
@@ -0,0 +1,162 @@
+<?php
+/**
+ * Set of classes to programatically communicate with services at NC State
+ * University
+ *
+ * @package Ncstate_Service
+ * @author Office of Information Technology - Outreach Technology
+ */
+
+/**
+ * Generic connector to the NC State LDAP service. Connects to secure service
+ * when a username and password are provided, otherwise it connects to the
+ * unsecure service on anonymous bind.
+ *
+ * @see http://www.ldap.ncsu.edu
+ */
+
+class Ncstate_Service_Ldap_Connector
+{
+ /**
+ * NC State's secure LDAP service
+ *
+ * @var string
+ */
+ const SECURE_LDAP_SERVER = 'ldaps://ldap.ncsu.edu';
+
+ /**
+ * NC State's unsecure LDAP service (for anonymous binds)
+ *
+ * @var string
+ */
+ const LDAP_SERVER = 'ldap://ldap.ncsu.edu';
+
+ /**
+ * The LDAP link object
+ *
+ * @var resource
+ */
+ private $_link = null;
+
+ /**
+ * Whether the LDAP connection is anonymous or not
+ *
+ * @var boolean
+ */
+ private $_anonymous = true;
+
+ /**
+ * Connect to a given LDAP host with the qualifications stated
+ *
+ * @param $ldapBindDn - LDAP directory bind
+ * @param $ldapPass - Password (not needed for anon. bind)
+ */
+ public function __construct($ldapBindDn, $ldapPass)
+ {
+ // Use secure option if using authenticated LDAP
+ $ldapHost = ($ldapBindDn == '' || $ldapPass == '') ? self::LDAP_SERVER : self::SECURE_LDAP_SERVER;
+
+ $this->_anonymous = ($ldapBindDn == '' || $ldapPass == '');
+
+ // Connect to the resource
+ $resource = @ldap_connect($ldapHost);
+
+ ldap_set_option($resource, LDAP_OPT_PROTOCOL_VERSION, 3);
+ ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 0);
+
+ if (ldap_errno($resource) != 0) {
+ require_once 'Ncstate/Service/Exception.php';
+ throw new Ncstate_Service_Exception(ldap_error($resource), ldap_errno($resource));
+ }
+
+ if (!@ldap_bind($resource, $ldapBindDn, $ldapPass)) {
+ require_once 'Ncstate/Service/Exception.php';
+ throw new Ncstate_Service_Exception(ldap_error($resource), ldap_errno($resource));
+ }
+
+ $this->_link = $resource;
+ }
+
+ /**
+ * Runs a standard search on LDAP
+ *
+ * @param string $queryString
+ * @param string $context
+ * @return beautified LDAP result set
+ * @throws Ncstate_Service_Exception on LDAP Error
+ */
+ public function search($queryString, $context, $returnFields, $sortKey = null, $sortOrder = null)
+ {
+ // Make sure there are some return fields set
+ if (count($returnFields) == 0) {
+ $returnFields = array('*','+');
+ }
+
+ // Do the search
+ $ldapResult = @ldap_search($this->getLink(), $context, $queryString, $returnFields, 0, $this->_maxResults);
+
+ if (!$ldapResult) {
+ require_once 'Ncstate/Service/Exception.php';
+ throw new Ncstate_Service_Exception(ldap_error($this->_link), ldap_errno($this->_link));
+ }
+
+ // Return empty array if there are no etries
+ if (@ldap_count_entries($this->getLink(), $ldapResult) == 0) {
+ return array();
+ }
+
+ $result = @ldap_get_entries($this->getLink(), $ldapResult);
+
+ unset($result["count"]);
+
+ $ret = array();
+ foreach ($result as $r) {
+ $temp = array();
+
+ $keys = array_keys($r);
+
+ foreach ($keys as $k) {
+
+ if (!is_int($k) && $k != "dn" && $k != "count") {
+ if (is_array($r[$k])) {
+ $temp[$k] = $r[$k][0];
+ } else {
+ $temp[$k] = $r[$k];
+ }
+ }
+ }
+
+ $ret[] = $temp;
+ }
+
+ if (!is_null($sortKey)) {
+ usort($ret, create_function('$a, $b', "return strnatcasecmp(\$a['$sortKey'], \$b['$sortKey']);"));
+
+ if ($sortOrder == "desc") {
+ $ret = array_reverse($ret);
+ }
+ }
+
+ return $ret;
+ }
+
+ /**
+ * Returns anonymous flag
+ *
+ * @return boolean
+ */
+ public function isAnonymous()
+ {
+ return $this->_anonymous;
+ }
+
+ /**
+ * Returns the LDAP resource
+ *
+ * @return resource
+ */
+ public function getLink()
+ {
+ return $this->_link;
+ }
+}
Oops, something went wrong.

0 comments on commit bd445a3

Please sign in to comment.