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