Source for file LDAPAuthNMethod.class.php

Documentation is available at LDAPAuthNMethod.class.php

  1. <?php
  2. /**
  3. * @package harmoni.osid_v2.agentmanagement.authn_methods
  4. *
  5. * @copyright Copyright &copy; 2005, Middlebury College
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  7. *
  8. * @version $Id: LDAPAuthNMethod.class.php,v 1.17 2007/09/04 20:25:37 adamfranco Exp $
  9. */
  10. require_once(dirname(__FILE__)."/AuthNMethod.abstract.php");
  11. require_once(dirname(__FILE__)."/LDAPConnector.class.php");
  12. require_once(dirname(__FILE__)."/LDAPGroup.class.php");
  13.  
  14. /**
  15. * The LDAPAuthNMethod is used to authenticate against an LDAP system.
  16. *
  17. * @package harmoni.osid_v2.agentmanagement.authn_methods
  18. *
  19. * @copyright Copyright &copy; 2005, Middlebury College
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  21. *
  22. * @version $Id: LDAPAuthNMethod.class.php,v 1.17 2007/09/04 20:25:37 adamfranco Exp $
  23. */
  24. class LDAPAuthNMethod
  25. extends AuthNMethod
  26. {
  27.  
  28. /**
  29. * Stores the configuration. Calls the parent configuration first,
  30. * then does additional operations.
  31. *
  32. * @param object Properties $configuration
  33. * @return object
  34. * @access public
  35. * @since 3/24/05
  36. */
  37. function assignConfiguration ( $configuration ) {
  38. parent::assignConfiguration($configuration);
  39. $this->_connector = new LDAPConnector($configuration);
  40. $this->_configuration->addProperty('connector', $this->_connector);
  41. // Validate the configuration options we use:
  42. ArgumentValidator::validate (
  43. $this->_configuration->getProperty('properties_fields'),
  44. ArrayValidatorRuleWithRule::getRule(StringValidatorRule::getRule()));
  45. }
  46. /**
  47. * Create a Tokens Object
  48. *
  49. * @return object Tokens
  50. * @access public
  51. * @since 3/1/05
  52. */
  53. function createTokensObject () {
  54. $tokensClass = $this->_configuration->getProperty('tokens_class');
  55. $newTokens = new $tokensClass($this->_configuration);
  56. $validatorRule = ExtendsValidatorRule::getRule('LDAPAuthNTokens');
  57. if ($validatorRule->check($newTokens))
  58. return $newTokens;
  59. else
  60. throwError( new Error("Configuration Error: tokens_class, '".$tokensClass."' does not extend UsernamePasswordAuthNTokens.",
  61. "LDAPAuthNMethod", true));
  62. }
  63. /**
  64. * Authenticate an AuthNTokens object
  65. *
  66. * @param object AuthNTokens $authNTokens
  67. * @return boolean
  68. * @access public
  69. * @since 3/1/05
  70. */
  71. function authenticateTokens ( $authNTokens ) {
  72. ArgumentValidator::validate ($authNTokens, ExtendsValidatorRule::getRule("AuthNTokens"));
  73. return $this->_connector->authenticateDN($authNTokens->getUsername(),
  74. $authNTokens->getPassword());
  75. }
  76. /**
  77. * Return true if the tokens can be matched in the system.
  78. *
  79. * @param object AuthNTokens $authNTokens
  80. * @return boolean
  81. * @access public
  82. * @since 3/1/05
  83. */
  84. function tokensExist ( $authNTokens ) {
  85. ArgumentValidator::validate ($authNTokens, ExtendsValidatorRule::getRule("AuthNTokens"));
  86. return $this->_connector->userDNExists($authNTokens->getUsername());
  87. }
  88. /**
  89. * A private method used to populate the Properties that correspond to the
  90. * given AuthNTokens
  91. *
  92. * @param object AuthNTokens $authNTokens
  93. * @param object Properties $properties
  94. * @return void
  95. * @access private
  96. * @since 3/1/05
  97. */
  98. function _populateProperties ( $authNTokens, $properties ) {
  99. ArgumentValidator::validate ($authNTokens, ExtendsValidatorRule::getRule("AuthNTokens"));
  100. ArgumentValidator::validate ($properties, ExtendsValidatorRule::getRule("Properties"));
  101. $propertiesFields =$this->_configuration->getProperty('properties_fields');
  102. if (!is_array($propertiesFields) || !count($propertiesFields))
  103. return;
  104. $fieldsToFetch = array();
  105. foreach ($propertiesFields as $propertyKey => $fieldName) {
  106. $fieldsToFetch[] = $fieldName;
  107. }
  108. $info = $this->_connector->getInfo($authNTokens->getUsername(), $fieldsToFetch);
  109. if ($info) {
  110. foreach ($propertiesFields as $propertyKey => $fieldName) {
  111. if (isset($info[$fieldName])) {
  112. if (count($info[$fieldName]) <= 1)
  113. $properties->addProperty($propertyKey, $info[$fieldName][0]);
  114. else
  115. $properties->addProperty($propertyKey, $info[$fieldName]);
  116. }
  117. }
  118. } else
  119. return;
  120. }
  121. /**
  122. * Get an iterator of the AuthNTokens that match the search string passed.
  123. * The '*' wildcard character can be present in the string and will be
  124. * converted to the system wildcard for the AuthNMethod if wildcards are
  125. * supported or removed (and the exact string searched for) if they are not
  126. * supported.
  127. *
  128. * When multiple fields are searched on an OR search is performed, i.e.
  129. * '*ach*' would match username/fullname 'achapin'/'Chapin, Alex' as well as
  130. * 'zsmith'/'Smith, Zach'.
  131. *
  132. * @param string $searchString
  133. * @return object ObjectIterator
  134. * @access public
  135. * @since 3/3/05
  136. */
  137. function getTokensBySearch ( $searchString ) {
  138. ArgumentValidator::validate ($searchString, StringValidatorRule::getRule());
  139. $propertiesFields =$this->_configuration->getProperty('properties_fields');
  140. if (is_array($propertiesFields) && count($propertiesFields)) {
  141. $filter = "(|";
  142. foreach ($propertiesFields as $propertyKey => $fieldName) {
  143. $filter .= " (".$fieldName."=".$searchString.")";
  144. }
  145. $filter .= ")";
  146. $dns = $this->_connector->getUserDNsBySearch($filter);
  147. } else
  148. $dns = array();
  149.  
  150. $tokens = array();
  151. foreach ($dns as $dn) {
  152. $tokens[] =$this->createTokensForIdentifier($dn);
  153. }
  154. $obj = new HarmoniObjectIterator($tokens);
  155. return $obj;
  156. }
  157. /**
  158. * Get an iterator of the AuthNTokens that match the search string passed.
  159. * The '*' wildcard character can be present in the string and will be
  160. * converted to the system wildcard for the AuthNMethod if wildcards are
  161. * supported or removed (and the exact string searched for) if they are not
  162. * supported.
  163. *
  164. * When multiple fields are searched on an OR search is performed, i.e.
  165. * '*ach*' would match username/fullname 'achapin'/'Chapin, Alex' as well as
  166. * 'zsmith'/'Smith, Zach'.
  167. *
  168. * @param string $searchString
  169. * @return object ObjectIterator
  170. * @access public
  171. * @since 3/3/05
  172. */
  173. function getGroupTokensBySearch ( $searchString ) {
  174. ArgumentValidator::validate ($searchString, StringValidatorRule::getRule());
  175. $propertiesFields =$this->_configuration->getProperty('properties_fields');
  176. if (is_array($propertiesFields) && count($propertiesFields)) {
  177. $filter = "(|";
  178. foreach ($propertiesFields as $propertyKey => $fieldName) {
  179. $filter .= " (".$fieldName."=".$searchString.")";
  180. }
  181. $filter .= ")";
  182. $dns = $this->_connector->getGroupDNsBySearch($filter);
  183. } else
  184. $dns = array();
  185.  
  186. $tokens = array();
  187. foreach ($dns as $dn) {
  188. $tokens[] =$this->createTokensForIdentifier($dn);
  189. }
  190. $obj = new HarmoniObjectIterator($tokens);
  191. return $obj;
  192. }
  193. /**
  194. * Get an iterator of the AuthNTokens that match the search string passed.
  195. * The '*' wildcard character can be present in the string and will be
  196. * converted to the system wildcard for the AuthNMethod if wildcards are
  197. * supported or removed (and the exact string searched for) if they are not
  198. * supported.
  199. *
  200. * When multiple fields are searched on an OR search is performed, i.e.
  201. * '*ach*' would match username/fullname 'achapin'/'Chapin, Alex' as well as
  202. * 'zsmith'/'Smith, Zach'.
  203. *
  204. * @param string $searchString
  205. * @return object ObjectIterator
  206. * @access public
  207. * @since 3/3/05
  208. */
  209. function getClassTokensBySearch ( $searchString ) {
  210. ArgumentValidator::validate ($searchString, StringValidatorRule::getRule());
  211. $propertiesFields =$this->_configuration->getProperty('properties_fields');
  212. if (is_array($propertiesFields) && count($propertiesFields)) {
  213. $filter = "(|";
  214. foreach ($propertiesFields as $propertyKey => $fieldName) {
  215. $filter .= " (".$fieldName."=".$searchString.")";
  216. }
  217. $filter .= ")";
  218. $dns = $this->_connector->getClassesDNsBySearch($filter);
  219. } else
  220. $dns = array();
  221.  
  222. $tokens = array();
  223. foreach ($dns as $dn) {
  224. $tokens[] =$this->createTokensForIdentifier($dn);
  225. }
  226. $obj = new HarmoniObjectIterator($tokens);
  227. return $obj;
  228. }
  229.  
  230. /*******************************************************
  231. * Directory methods
  232. *********************************************************/
  233.  
  234. /**
  235. * Answer TRUE if this AuthN method supports directory functionality
  236. *
  237. * @return boolean
  238. * @access public
  239. * @since 2/23/06
  240. */
  241. function supportsDirectory () {
  242. // Override if implementing
  243. return TRUE;
  244. }
  245. /**
  246. * Answer an iterator of all groups
  247. *
  248. * @param object AuthNTokens $authNTokens
  249. * @return object AgentIterator
  250. * @access public
  251. * @since 2/23/06
  252. */
  253. function getAllGroups () {
  254. return $this->getRootGroups();
  255. }
  256. /**
  257. * Answer an iterator of the top-level groups, may be equivalent to
  258. * getAllGroups() if this directory is not hierarchically organized.
  259. *
  260. * @param object AuthNTokens $authNTokens
  261. * @return object AgentIterator
  262. * @access public
  263. * @since 2/23/06
  264. */
  265. function getRootGroups () {
  266. if (!isset($this->_rootGroups)) {
  267. $connector =$this->_configuration->getProperty('connector');
  268. $groupDN = $this->_configuration->getProperty("GroupBaseDN");
  269. $filter = "(objectclass=*)";
  270. $dns = $connector->getDNsByList($filter, $groupDN);
  271. $this->_rootGroups = array();
  272. foreach ($dns as $dn) {
  273. if ($dn != $groupDN)
  274. $this->_rootGroups[] = new LDAPGroup($dn, $this->getType(),
  275. $this->_configuration,
  276. $this);
  277. }
  278. }
  279. $iterator = new HarmoniIterator($this->_rootGroups);
  280. return $iterator;
  281. }
  282. /**
  283. * Answer a group by Id
  284. *
  285. * @param object Id $id
  286. * @return object AgentIterator
  287. * @access public
  288. * @since 2/23/06
  289. */
  290. function getGroup ( $id ) {
  291. $group = new LDAPGroup($id->getIdString(), $this->getType(),
  292. $this->_configuration,
  293. $this);
  294. return $group;
  295. }
  296. /**
  297. * Answer a true if the Id corresponds to a valid group.
  298. *
  299. * @todo search on DN and ObjectClass
  300. *
  301. * @param object Id $id
  302. * @return boolean
  303. * @access public
  304. * @since 2/23/06
  305. */
  306. function isGroup ( $id ) {
  307. $idString = str_replace(' ', '', $id->getIdString());
  308. $baseDN = str_replace(' ', '', $this->_configuration->getProperty("GroupBaseDN"));
  309. return preg_match('/.+'.$baseDN.'$/i', $idString);
  310. }
  311. /**
  312. * Answer an iterator of groups that contain the tokens. If $includeSubgroups
  313. * is true then groups will be returned if any descendent group contains
  314. * the tokens.
  315. *
  316. * @param object AuthNTokens $authNTokens
  317. * @return object AgentIterator
  318. * @access public
  319. * @since 2/23/06
  320. */
  321. function getGroupsContainingTokens ( $authNTokens, $includeSubgroups ) {
  322. $connector =$this->_configuration->getProperty('connector');
  323. $groupDN = $this->_configuration->getProperty("GroupBaseDN");
  324. // Parent Groups of Agents
  325. $info = $this->_connector->getInfo($authNTokens->getUsername(), array('memberof'));
  326. if (isset($info['memberof'])) {
  327. $dns = $info['memberof'];
  328. $groups = array();
  329. foreach ($dns as $dn) {
  330. if ($dn != $groupDN && !isset($groups[$dn]))
  331. $groups[$dn] = new LDAPGroup($dn, $this->getType(),
  332. $this->_configuration,
  333. $this);
  334. }
  335. }
  336. if ($includeSubgroups && isset($dns)) {
  337. foreach ($dns as $dn) {
  338. if ($dn != $groupDN) {
  339. $parentGroups =$this->getGroupsContainingGroup($dn, true);
  340. while ($parentGroups->hasNext()) {
  341. $group =$parentGroups->next();
  342. $groupId =$group->getId();
  343. if (!isset($groups[$groupId->getIdString()]))
  344. $groups[$groupId->getIdString()] =$group;
  345. }
  346. }
  347. }
  348. }
  349. $iterator = new HarmoniIterator($groups);
  350. return $iterator;
  351. }
  352. /**
  353. * Answer an iterator of groups that contain the Id. If $includeSubgroups
  354. * is true then groups will be returned if any descendent group contains
  355. * the Id.
  356. *
  357. * @param object Id $id
  358. * @return object AgentIterator
  359. * @access public
  360. * @since 2/23/06
  361. */
  362. function getGroupsContainingGroup ( $id, $includeSubgroups ) {
  363. if (is_object($id))
  364. $idString = $id->getIdString();
  365. else
  366. $idString = $id;
  367. $groups = array();
  368. $baseDN = str_replace(' ', '', $this->_configuration->getProperty("GroupBaseDN"));
  369. $levels = 0;
  370. while (strlen($idString) && ($includeSubgroups || $levels < 1)) {
  371. $levels++;
  372. for ($i = 0; $i < strlen($idString); $i++) {
  373. if ($idString[$i] == ',' && $idString[$i-1] != '\\') {
  374. $idString = substr($idString, $i+1);
  375. break;
  376. }
  377. }
  378. if (preg_match('/^'.$baseDN.'$/i', str_replace(' ', '', $idString)))
  379. break;
  380. if (!isset($groups[$idString]))
  381. $groups[$idString] = new LDAPGroup($idString, $this->getType(),
  382. $this->_configuration,
  383. $this);
  384. }
  385. $iterator = new HarmoniIterator($groups);
  386. return $iterator;
  387. }
  388. }
  389.  
  390. ?>

Documentation generated on Wed, 19 Sep 2007 10:24:49 -0400 by phpDocumentor 1.3.0RC3