LdapAuthenticationPlugin.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Plugin to enable LDAP Authentication
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Plugin
  23. * @package StatusNet
  24. * @author Craig Andrews <candrews@integralblue.com>
  25. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET') && !defined('LACONICA')) {
  30. exit(1);
  31. }
  32. class LdapAuthenticationPlugin extends AuthenticationPlugin
  33. {
  34. function onInitializePlugin(){
  35. parent::onInitializePlugin();
  36. if(!isset($this->attributes['nickname'])){
  37. // TRANS: Exception thrown when initialising the LDAP Auth plugin fails because of an incorrect configuration.
  38. throw new Exception(_m('You must specify a nickname attribute.'));
  39. }
  40. if($this->password_changeable && (! isset($this->attributes['password']) || !isset($this->password_encoding))){
  41. // TRANS: Exception thrown when initialising the LDAP Auth plugin fails because of an incorrect configuration.
  42. throw new Exception(_m('If password_changeable is set, the password attribute and password_encoding must also be specified.'));
  43. }
  44. $this->ldapCommon = new LdapCommon(get_object_vars($this));
  45. }
  46. function onAutoload($cls)
  47. {
  48. switch ($cls)
  49. {
  50. case 'LdapCommon':
  51. require_once(INSTALLDIR.'/plugins/LdapCommon/LdapCommon.php');
  52. return false;
  53. }
  54. return parent::onAutoload($cls);
  55. }
  56. function onEndShowPageNotice($action)
  57. {
  58. $name = $action->trimmed('action');
  59. $instr = false;
  60. switch ($name)
  61. {
  62. case 'register':
  63. if($this->autoregistration) {
  64. // TRANS: Instructions for LDAP authentication.
  65. $instr = _m('Do you have an LDAP account? Use your standard username and password.');
  66. }
  67. break;
  68. case 'login':
  69. // TRANS: Instructions for LDAP authentication.
  70. $instr = _m('Do you have an LDAP account? Use your standard username and password.');
  71. break;
  72. default:
  73. return true;
  74. }
  75. if($instr) {
  76. $output = common_markup_to_html($instr);
  77. $action->raw($output);
  78. }
  79. return true;
  80. }
  81. //---interface implementation---//
  82. function checkPassword($username, $password)
  83. {
  84. return $this->ldapCommon->checkPassword($username,$password);
  85. }
  86. function autoRegister($username, $nickname)
  87. {
  88. if(is_null($nickname)){
  89. $nickname = $username;
  90. }
  91. $entry = $this->ldapCommon->get_user($username,$this->attributes);
  92. if($entry){
  93. $registration_data = array();
  94. foreach($this->attributes as $sn_attribute=>$ldap_attribute){
  95. //ldap won't let us read a user's password,
  96. //and we're going to set the password to a random string later anyways,
  97. //so don't bother trying to read it.
  98. if($sn_attribute != 'password'){
  99. $registration_data[$sn_attribute]=$entry->getValue($ldap_attribute,'single');
  100. }
  101. }
  102. if(isset($registration_data['email']) && !empty($registration_data['email'])){
  103. $registration_data['email_confirmed']=true;
  104. }
  105. $registration_data['nickname'] = $nickname;
  106. //set the database saved password to a random string.
  107. $registration_data['password']=common_random_hexstr(16);
  108. return User::register($registration_data);
  109. }else{
  110. //user isn't in ldap, so we cannot register him
  111. return false;
  112. }
  113. }
  114. function changePassword($username,$oldpassword,$newpassword)
  115. {
  116. return $this->ldapCommon->changePassword($username,$oldpassword,$newpassword);
  117. }
  118. function suggestNicknameForUsername($username)
  119. {
  120. $entry = $this->ldapCommon->get_user($username, $this->attributes);
  121. if(!$entry){
  122. //this really shouldn't happen
  123. $nickname = $username;
  124. }else{
  125. $nickname = $entry->getValue($this->attributes['nickname'],'single');
  126. if(!$nickname){
  127. $nickname = $username;
  128. }
  129. }
  130. return common_nicknamize($nickname);
  131. }
  132. function onPluginVersion(array &$versions)
  133. {
  134. $versions[] = array('name' => 'LDAP Authentication',
  135. 'version' => GNUSOCIAL_VERSION,
  136. 'author' => 'Craig Andrews',
  137. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/LdapAuthentication',
  138. 'rawdescription' =>
  139. // TRANS: Plugin description.
  140. _m('The LDAP Authentication plugin allows for StatusNet to handle authentication through LDAP.'));
  141. return true;
  142. }
  143. }