LdapAuthenticationPlugin.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. const PLUGIN_VERSION = '2.0.0';
  35. function onInitializePlugin(){
  36. parent::onInitializePlugin();
  37. if(!isset($this->attributes['nickname'])){
  38. // TRANS: Exception thrown when initialising the LDAP Auth plugin fails because of an incorrect configuration.
  39. throw new Exception(_m('You must specify a nickname attribute.'));
  40. }
  41. if($this->password_changeable && (! isset($this->attributes['password']) || !isset($this->password_encoding))){
  42. // TRANS: Exception thrown when initialising the LDAP Auth plugin fails because of an incorrect configuration.
  43. throw new Exception(_m('If password_changeable is set, the password attribute and password_encoding must also be specified.'));
  44. }
  45. $this->ldapCommon = new LdapCommon(get_object_vars($this));
  46. }
  47. function onAutoload($cls)
  48. {
  49. switch ($cls)
  50. {
  51. case 'LdapCommon':
  52. require_once(INSTALLDIR.'/plugins/LdapCommon/LdapCommon.php');
  53. return false;
  54. }
  55. return parent::onAutoload($cls);
  56. }
  57. function onEndShowPageNotice($action)
  58. {
  59. $name = $action->trimmed('action');
  60. $instr = false;
  61. switch ($name)
  62. {
  63. case 'register':
  64. if($this->autoregistration) {
  65. // TRANS: Instructions for LDAP authentication.
  66. $instr = _m('Do you have an LDAP account? Use your standard username and password.');
  67. }
  68. break;
  69. case 'login':
  70. // TRANS: Instructions for LDAP authentication.
  71. $instr = _m('Do you have an LDAP account? Use your standard username and password.');
  72. break;
  73. default:
  74. return true;
  75. }
  76. if($instr) {
  77. $output = common_markup_to_html($instr);
  78. $action->raw($output);
  79. }
  80. return true;
  81. }
  82. //---interface implementation---//
  83. function checkPassword($username, $password)
  84. {
  85. return $this->ldapCommon->checkPassword($username,$password);
  86. }
  87. function autoRegister($username, $nickname)
  88. {
  89. if(is_null($nickname)){
  90. $nickname = $username;
  91. }
  92. $entry = $this->ldapCommon->get_user($username,$this->attributes);
  93. if($entry){
  94. $registration_data = array();
  95. foreach($this->attributes as $sn_attribute=>$ldap_attribute){
  96. //ldap won't let us read a user's password,
  97. //and we're going to set the password to a random string later anyways,
  98. //so don't bother trying to read it.
  99. if($sn_attribute != 'password'){
  100. $registration_data[$sn_attribute]=$entry->getValue($ldap_attribute,'single');
  101. }
  102. }
  103. if(isset($registration_data['email']) && !empty($registration_data['email'])){
  104. $registration_data['email_confirmed']=true;
  105. }
  106. $registration_data['nickname'] = $nickname;
  107. //set the database saved password to a random string.
  108. $registration_data['password']=common_random_hexstr(16);
  109. return User::register($registration_data);
  110. }else{
  111. //user isn't in ldap, so we cannot register him
  112. return false;
  113. }
  114. }
  115. function changePassword($username,$oldpassword,$newpassword)
  116. {
  117. return $this->ldapCommon->changePassword($username,$oldpassword,$newpassword);
  118. }
  119. function suggestNicknameForUsername($username)
  120. {
  121. $entry = $this->ldapCommon->get_user($username, $this->attributes);
  122. if(!$entry){
  123. //this really shouldn't happen
  124. $nickname = $username;
  125. }else{
  126. $nickname = $entry->getValue($this->attributes['nickname'],'single');
  127. if(!$nickname){
  128. $nickname = $username;
  129. }
  130. }
  131. return common_nicknamize($nickname);
  132. }
  133. public function onPluginVersion(array &$versions): bool
  134. {
  135. $versions[] = array('name' => 'LDAP Authentication',
  136. 'version' => self::PLUGIN_VERSION,
  137. 'author' => 'Craig Andrews',
  138. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/LdapAuthentication',
  139. 'rawdescription' =>
  140. // TRANS: Plugin description.
  141. _m('The LDAP Authentication plugin allows for StatusNet to handle authentication through LDAP.'));
  142. return true;
  143. }
  144. }