PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/tests/kernel/datatypes/ezuser/setup_accounts.php

https://github.com/zerustech/ezpublish
PHP | 247 lines | 170 code | 21 blank | 56 comment | 5 complexity | b871d5535a91f9e32004e1698d728b15 MD5 | raw file
  1. <?php
  2. /**
  3. * Setup the test accounts on the ldap server.
  4. *
  5. * Assumes the username entries are 'uid'.
  6. * Note that all tests will crash horribly if any new trilogy characters are introduced here ;)
  7. *
  8. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  9. * @license For full copyright and license information view LICENSE file distributed with this source code.
  10. * @version //autogentag//
  11. * @package tests
  12. */
  13. $dc = "dc=phpuc,dc=ez,dc=no";
  14. $host = "phpuc.ez.no";
  15. $connection = Ldap::connect( "ldap://$host", "cn=%s,{$dc}", 'admin', 'wee123' );
  16. Ldap::delete( $connection, 'yoda', "ou=StarWars,{$dc}" );
  17. Ldap::delete( $connection, 'boba.fett', "ou=StarWars,{$dc}" );
  18. Ldap::delete( $connection, 'obi.wan', "ou=StarWars,{$dc}" );
  19. Ldap::delete( $connection, 'jabba.thehutt', "ou=StarWars,{$dc}" );
  20. Ldap::delete( $connection, 'darth.vader', "ou=StarWars,{$dc}" );
  21. Ldap::delete( $connection, 'leia', "ou=StarWars,{$dc}" );
  22. Ldap::delete( $connection, 'han.solo', "ou=StarWars,{$dc}" );
  23. Ldap::delete( $connection, 'chewbacca', "ou=StarWars,{$dc}" );
  24. Ldap::deleteGroup( $connection, 'Jedi', "ou=RebelAlliance,ou=StarWars,{$dc}" );
  25. Ldap::deleteGroup( $connection, 'RebelAlliance', "ou=StarWars,{$dc}" );
  26. Ldap::deleteGroup( $connection, 'Sith', "ou=GalacticEmpire,ou=StarWars,$dc" );
  27. Ldap::deleteGroup( $connection, 'GalacticEmpire', "ou=StarWars,{$dc}" );
  28. Ldap::deleteGroup( $connection, 'Rogues', "ou=StarWars,{$dc}" );
  29. Ldap::deleteGroup( $connection, 'StarWars', $dc );
  30. Ldap::addGroup( $connection, 'StarWars', $dc );
  31. $chewbaccaDN =
  32. Ldap::add( $connection, 'chewbacca', '{MD5}' . base64_encode( pack( 'H*', md5( 'aaawwwwrrrkk' ) ) ), "ou=StarWars,{$dc}", 'Chewbacca', 'Chewbacca',
  33. array( 'givenName' => 'Chewbacca',
  34. 'displayName' => 'Chewbacca the Wokiee',
  35. 'ou' => array( 'StarWars', 'Rogues', 'RebelAlliance' ),
  36. 'mail' => array( 'chewbacca@millenniumfalcon.net' ) ) );
  37. $hanSoloDN =
  38. Ldap::add( $connection, 'han.solo', '{MD5}' . base64_encode( pack( 'H*', md5( 'leiaishot' ) ) ), "ou=StarWars,{$dc}", 'Solo', 'Han Solo',
  39. array( 'givenName' => 'Han',
  40. 'displayName' => 'He who shot first',
  41. 'ou' => array( 'StarWars', 'Rogues', 'RebelAlliance' ),
  42. 'mail' => array( 'han.solo@millenniumfalcon.net' ) ) );
  43. $princessLeiaDN =
  44. Ldap::add( $connection, 'leia', '{MD5}' . base64_encode( pack( 'H*', md5( 'bunhead' ) ) ), "ou=StarWars,{$dc}", 'Organa', 'Leia Organa',
  45. array( 'givenName' => 'Leia',
  46. 'displayName' => 'Princess Leia',
  47. 'ou' => array( 'StarWars', 'RebelAlliance' ),
  48. 'mail' => array( 'leia@rebelalliance.org' ) ) );
  49. $darthVaderDN =
  50. Ldap::add( $connection, 'darth.vader', '{MD5}' . base64_encode( pack( 'H*', md5( 'whosyourdaddy' ) ) ), "ou=StarWars,{$dc}", 'Skywalker', 'Anakin Skywalker',
  51. array( 'givenName' => 'Anakin',
  52. 'displayName' => 'Darth Vader',
  53. 'ou' => array( 'StarWars', 'GalacticEmpire', 'Sith' ),
  54. 'mail' => array( 'vader@empire.com' ) ) );
  55. $jabbaTheHuttDN =
  56. Ldap::add( $connection, 'jabba.thehutt', '{MD5}' . base64_encode( pack( 'H*', md5( 'wishihadlegs' ) ) ), "ou=StarWars,{$dc}", 'Hutt', 'Jabba Hutt',
  57. array( 'givenName' => 'Jabba',
  58. 'displayName' => 'Jabba the Hutt',
  59. 'ou' => array( 'Hutts' ),
  60. 'mail' => array( 'jabba@hutt.com' ) ) );
  61. $obiWanDN =
  62. Ldap::add( $connection, 'obi.wan', '{MD5}' . base64_encode( pack( 'H*', md5( 'thesearenotthedroids' ) ) ), "ou=StarWars,{$dc}", 'Kenobi', 'Obi Wan Kenobi',
  63. array( 'givenName' => 'Obi Wan',
  64. 'displayName' => 'Obi Wan Kenobi',
  65. 'ou' => array( 'StarWars', 'RebelAlliance', 'Jedi' ),
  66. 'seeAlso' => array( "ou=StarWars,{$dc}", "ou=RebelAlliance,ou=StarWars,{$dc}", "ou=Jedi,ou=RebelAlliance,ou=StarWars,{$dc}" ),
  67. 'mail' => array( 'obi.wan@jedi.org' ) ) );
  68. $bobaFettDN =
  69. Ldap::add( $connection, 'boba.fett', '{MD5}' . base64_encode( pack( 'H*', md5( 'ihatesarlacs' ) ) ), "ou=StarWars,{$dc}", 'Fett', 'Boba Fett',
  70. array( 'givenName' => 'Boba',
  71. 'displayName' => 'Boba Fett',
  72. 'ou' => array( 'StarWars', 'Rogues' ),
  73. 'seeAlso' => array( "ou=StarWars,{$dc}", "ou=Rogues,ou=StarWars,{$dc}" ),
  74. 'mail' => array( 'boba.fett@bountyhunter.com' ) ) );
  75. $yodaDN =
  76. Ldap::add( $connection, 'yoda', '{MD5}' . base64_encode( pack( 'H*', md5( 'dagobah4eva' ) ) ), "ou=StarWars,{$dc}", 'Yoda', 'Yoda',
  77. array( 'givenName' => 'Yoda',
  78. 'displayName' => 'Yoda',
  79. 'ou' => array( 'StarWars', 'RebelAlliance', 'Jedi' ),
  80. 'seeAlso' => array( "ou=StarWars,{$dc}", "ou=RebelAlliance,ou=StarWars,{$dc}", "ou=Jedi,ou=RebelAlliance,ou=StarWars,{$dc}" ),
  81. 'mail' => array( 'yoda@jedi.org' ) ) );
  82. Ldap::addGroup( $connection, 'RebelAlliance', "ou=StarWars,{$dc}",
  83. array( 'seeAlso' => array( $princessLeiaDN, $chewbaccaDN, $hanSoloDN, $obiWanDN, $yodaDN ) ) );
  84. Ldap::addGroup( $connection, 'Rogues', "ou=StarWars,{$dc}",
  85. array( 'seeAlso' => array( $chewbaccaDN, $hanSoloDN ) ) );
  86. Ldap::addGroup( $connection, 'GalacticEmpire', "ou=StarWars,{$dc}",
  87. array( 'seeAlso' => array( $darthVaderDN ) ) );
  88. Ldap::addGroup( $connection, 'Sith', "ou=GalacticEmpire,ou=StarWars,$dc",
  89. array( 'seeAlso' => array( $darthVaderDN ) ) );
  90. Ldap::addGroup( $connection, 'Jedi', "ou=RebelAlliance,ou=StarWars,{$dc}",
  91. array( 'seeAlso' => array( $obiWanDN, $yodaDN ) ) );
  92. // This dumps all the LDAP data
  93. // Ldap::fetchAll( $connection, $dc );
  94. Ldap::close( $connection );
  95. /**
  96. * Support for LDAP functions connect, add, delete and get_entries.
  97. */
  98. class Ldap
  99. {
  100. /**
  101. * Connects to an LDAP server specified by $uri, with admin $user and $password.
  102. *
  103. * Returns a resource which can be used in LDAP functions like add, delete, search.
  104. *
  105. * @param string $uri Uri for LDAP, such as 'ldap://example.com'
  106. * @param string $format Format for an entry, like 'cn=%s,dc=example,dc=com'. %s is a literal placeholder for username
  107. * @param string $user Admin username
  108. * @param string $password Password for admin
  109. * @return resource
  110. */
  111. public static function connect( $uri, $format, $user, $password )
  112. {
  113. if ( !extension_loaded( 'ldap' ) )
  114. {
  115. die( 'LDAP extension is not loaded.' );
  116. }
  117. $connection = ldap_connect( $uri );
  118. if ( !$connection )
  119. {
  120. throw new Exception( "Could not connect to host '{$uri}'" );
  121. }
  122. ldap_set_option( $connection, LDAP_OPT_PROTOCOL_VERSION, 3 );
  123. @ldap_bind( $connection, sprintf( $format, $user ), $password );
  124. $err = ldap_errno( $connection );
  125. switch ( $err )
  126. {
  127. case 0x51: // LDAP_SERVER_DOWN
  128. case 0x52: // LDAP_LOCAL_ERROR
  129. case 0x53: // LDAP_ENCODING_ERROR
  130. case 0x54: // LDAP_DECODING_ERROR
  131. case 0x55: // LDAP_TIMEOUT
  132. case 0x56: // LDAP_AUTH_UNKNOWN
  133. case 0x57: // LDAP_FILTER_ERROR
  134. case 0x58: // LDAP_USER_CANCELLED
  135. case 0x59: // LDAP_PARAM_ERROR
  136. case 0x5a: // LDAP_NO_MEMORY
  137. throw new Exception( "Could not connect to host '{$uri}'. (0x" . dechex( $err ) . ")" );
  138. break;
  139. }
  140. return $connection;
  141. }
  142. /**
  143. * Adds an entry in the LDAP directory.
  144. *
  145. * Throws a warning if the entry already exists.
  146. *
  147. * @param resource $connection Connection resource returned by ldap_connect()
  148. * @param string $user Username
  149. * @param string $password Password for username. Use an encryption function and put method in front of hash, like: '{MD5}hash'
  150. * @param string $dc The dc part of the entry, like: 'dc=example,dc=com'
  151. */
  152. public static function add( $connection, $user, $password, $dc, $sn, $cn, $extra = array() )
  153. {
  154. $ldaprecord['uid'][0] = $user;
  155. $ldaprecord['objectclass'][0] = "person";
  156. $ldaprecord['objectclass'][] = "organizationalPerson";
  157. $ldaprecord['objectclass'][] = "inetOrgPerson";
  158. $ldaprecord['sn'] = array( $sn );
  159. $ldaprecord['cn'] = array( $cn );
  160. $ldaprecord['objectclass'][] = "top";
  161. $ldaprecord['userPassword'][0] = $password;
  162. foreach ( $extra as $key => $value )
  163. {
  164. $ldaprecord[$key] = $value;
  165. }
  166. $dn = "uid={$user},{$dc}";
  167. $success = ldap_add( $connection, $dn, $ldaprecord );
  168. if ( $success )
  169. return $dn;
  170. else
  171. return false;
  172. }
  173. /**
  174. * Deletes an entry from the LDAP directory.
  175. *
  176. * @param resource $connection Connection resource returned by ldap_connect()
  177. * @param string $user Username to delete
  178. * @param string $dc The dc part of the entry, like: 'dc=example,dc=com'
  179. */
  180. public static function delete( $connection, $user, $dc )
  181. {
  182. ldap_delete( $connection, "uid={$user},{$dc}" );
  183. }
  184. public static function addGroup( $connection, $group, $dc, $extra = array() )
  185. {
  186. $ldaprecord['ou'] = $group;
  187. $ldaprecord['objectclass'][0] = "organizationalUnit";
  188. $ldaprecord['objectclass'][1] = "top";
  189. foreach ( $extra as $key => $value )
  190. {
  191. $ldaprecord[$key] = $value;
  192. }
  193. $dn = "ou={$group},{$dc}";
  194. $success = ldap_add( $connection, $dn, $ldaprecord );
  195. if ( $success )
  196. return $dn;
  197. else
  198. return false;
  199. }
  200. public static function deleteGroup( $connection, $group, $dc )
  201. {
  202. ldap_delete( $connection, "ou={$group},{$dc}" );
  203. }
  204. /**
  205. * Returns an array of all the entries in the LDAP directory.
  206. *
  207. * @param resource $connection Connection resource returned by ldap_connect()
  208. * @param string $dc The dc part of the entry, like: 'dc=example,dc=com'
  209. * @return array(mixed)
  210. */
  211. public static function fetchAll( $connection, $dc )
  212. {
  213. $sr = ldap_search( $connection, $dc, '(&(ou=*))' );
  214. var_dump( ldap_get_entries( $connection, $sr ) );
  215. }
  216. /**
  217. * Closes the connection to the LDAP server.
  218. *
  219. * @param resource $connection Connection resource returned by ldap_connect()
  220. */
  221. public static function close( $connection )
  222. {
  223. ldap_close( $connection );
  224. }
  225. }
  226. ?>