]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/LdapAuthentication/LdapAuthenticationPlugin.php
Merge branch '0.9.x' of git@gitorious.org:statusnet/mainline into 1.0.x
[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 Craig Andrews http://candrews.integralblue.com
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             throw new Exception("must specify a nickname attribute");
40         }
41         if($this->password_changeable && (! isset($this->attributes['password']) || !isset($this->password_encoding))){
42             throw new Exception("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
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     }
56
57     function onEndShowPageNotice($action)
58     {
59         $name = $action->trimmed('action');
60         $instr = false;
61
62         switch ($name)
63         {
64          case 'register':
65             if($this->autoregistration) {
66                 $instr = 'Have an LDAP account? Use your standard username and password.';
67             }
68             break;
69          case 'login':
70             $instr = 'Have an LDAP account? Use your standard username and password.';
71             break;
72          default:
73             return true;
74         }
75
76         if($instr) {
77             $output = common_markup_to_html($instr);
78             $action->raw($output);
79         }
80         return true;
81     }
82     
83     //---interface implementation---//
84
85     function checkPassword($username, $password)
86     {
87         return $this->ldapCommon->checkPassword($username,$password);
88     }
89
90     function autoRegister($username, $nickname)
91     {
92         if(is_null($nickname)){
93             $nickname = $username;
94         }
95         $entry = $this->ldapCommon->get_user($username,$this->attributes);
96         if($entry){
97             $registration_data = array();
98             foreach($this->attributes as $sn_attribute=>$ldap_attribute){
99                 $registration_data[$sn_attribute]=$entry->getValue($ldap_attribute,'single');
100             }
101             if(isset($registration_data['email']) && !empty($registration_data['email'])){
102                 $registration_data['email_confirmed']=true;
103             }
104             $registration_data['nickname'] = $nickname;
105             //set the database saved password to a random string.
106             $registration_data['password']=common_good_rand(16);
107             return User::register($registration_data);
108         }else{
109             //user isn't in ldap, so we cannot register him
110             return false;
111         }
112     }
113
114     function changePassword($username,$oldpassword,$newpassword)
115     {
116         return $this->ldapCommon->changePassword($username,$oldpassword,$newpassword);
117     }
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
134     function onPluginVersion(&$versions)
135     {
136         $versions[] = array('name' => 'LDAP Authentication',
137                             'version' => STATUSNET_VERSION,
138                             'author' => 'Craig Andrews',
139                             'homepage' => 'http://status.net/wiki/Plugin:LdapAuthentication',
140                             'rawdescription' =>
141                             _m('The LDAP Authentication plugin allows for StatusNet to handle authentication through LDAP.'));
142         return true;
143     }
144 }