3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to enable LDAP Authentication
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.
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.
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/>.
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/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
34 class LdapAuthenticationPlugin extends AuthenticationPlugin
36 const PLUGIN_VERSION = '2.0.0';
37 function onInitializePlugin(){
38 parent::onInitializePlugin();
39 if(!isset($this->attributes['nickname'])){
40 // TRANS: Exception thrown when initialising the LDAP Auth plugin fails because of an incorrect configuration.
41 throw new Exception(_m('You must specify a nickname attribute.'));
43 if($this->password_changeable && (! isset($this->attributes['password']) || !isset($this->password_encoding))){
44 // TRANS: Exception thrown when initialising the LDAP Auth plugin fails because of an incorrect configuration.
45 throw new Exception(_m('If password_changeable is set, the password attribute and password_encoding must also be specified.'));
47 $this->ldapCommon = new LdapCommon(get_object_vars($this));
50 function onAutoload($cls)
55 require_once(INSTALLDIR.'/plugins/LdapCommon/LdapCommon.php');
59 return parent::onAutoload($cls);
62 function onEndShowPageNotice($action)
64 $name = $action->trimmed('action');
70 if($this->autoregistration) {
71 // TRANS: Instructions for LDAP authentication.
72 $instr = _m('Do you have an LDAP account? Use your standard username and password.');
76 // TRANS: Instructions for LDAP authentication.
77 $instr = _m('Do you have an LDAP account? Use your standard username and password.');
84 $output = common_markup_to_html($instr);
85 $action->raw($output);
90 //---interface implementation---//
92 function checkPassword($username, $password)
94 return $this->ldapCommon->checkPassword($username,$password);
97 function autoRegister($username, $nickname)
99 if(is_null($nickname)){
100 $nickname = $username;
102 $entry = $this->ldapCommon->get_user($username,$this->attributes);
104 $registration_data = array();
105 foreach($this->attributes as $sn_attribute=>$ldap_attribute){
106 //ldap won't let us read a user's password,
107 //and we're going to set the password to a random string later anyways,
108 //so don't bother trying to read it.
109 if($sn_attribute != 'password'){
110 $registration_data[$sn_attribute]=$entry->getValue($ldap_attribute,'single');
113 if(isset($registration_data['email']) && !empty($registration_data['email'])){
114 $registration_data['email_confirmed']=true;
116 $registration_data['nickname'] = $nickname;
117 //set the database saved password to a random string.
118 $registration_data['password']=common_random_hexstr(16);
119 return User::register($registration_data);
121 //user isn't in ldap, so we cannot register him
126 function changePassword($username,$oldpassword,$newpassword)
128 return $this->ldapCommon->changePassword($username,$oldpassword,$newpassword);
131 function suggestNicknameForUsername($username)
133 $entry = $this->ldapCommon->get_user($username, $this->attributes);
135 //this really shouldn't happen
136 $nickname = $username;
138 $nickname = $entry->getValue($this->attributes['nickname'],'single');
140 $nickname = $username;
143 return common_nicknamize($nickname);
146 function onPluginVersion(array &$versions)
148 $versions[] = array('name' => 'LDAP Authentication',
149 'version' => self::PLUGIN_VERSION,
150 'author' => 'Craig Andrews',
151 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/LdapAuthentication',
153 // TRANS: Plugin description.
154 _m('The LDAP Authentication plugin allows for StatusNet to handle authentication through LDAP.'));