]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/LdapAuthorization/LdapAuthorizationPlugin.php
plugins onAutoload now only overloads if necessary (extlibs etc.)
[quix0rs-gnu-social.git] / plugins / LdapAuthorization / LdapAuthorizationPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to enable LDAP Authorization
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 LdapAuthorizationPlugin extends AuthorizationPlugin
35 {
36     public $roles_to_groups = array();
37     public $login_group = null;
38
39     function onInitializePlugin(){
40         if(!isset($this->provider_name)){
41             // TRANS: Exception thrown when initialising the LDAP Auth plugin fails because of an incorrect configuration.
42             throw new Exception(_m('provider_name must be set. Use the provider_name from the LDAP Authentication plugin.'));
43         }
44         if(!isset($this->uniqueMember_attribute)){
45             // TRANS: Exception thrown when initialising the LDAP Auth plugin fails because of an incorrect configuration.
46             throw new Exception(_m('uniqueMember_attribute must be set.'));
47         }
48         $this->ldapCommon = new LdapCommon(get_object_vars($this));
49     }
50
51     function onAutoload($cls)
52     {
53         switch ($cls)
54         {
55          case 'LdapCommon':
56             require_once(INSTALLDIR.'/plugins/LdapCommon/LdapCommon.php');
57             return false;
58         }
59
60         return parent::onAutoload($cls);
61     }
62
63     //---interface implementation---//
64     function loginAllowed($user) {
65         $user_username = new User_username();
66         $user_username->user_id=$user->id;
67         $user_username->provider_name=$this->provider_name;
68         if($user_username->find() && $user_username->fetch()){
69             $entry = $this->ldapCommon->get_user($user_username->username);
70             if($entry){
71                 if(isset($this->login_group)){
72                     if(is_array($this->login_group)){
73                         foreach($this->login_group as $group){
74                             if($this->ldapCommon->is_dn_member_of_group($entry->dn(),$group)){
75                                 return true;
76                             }
77                         }
78                     }else{
79                         if($this->ldapCommon->is_dn_member_of_group($entry->dn(),$this->login_group)){
80                             return true;
81                         }
82                     }
83                     return null;
84                 }else{
85                     //if a user exists, we can assume he's allowed to login
86                     return true;
87                 }
88             }else{
89                 return null;
90             }
91         }else{
92             return null;
93         }
94     }
95
96     function hasRole($profile, $name) {
97         $user_username = new User_username();
98         $user_username->user_id=$profile->id;
99         $user_username->provider_name=$this->provider_name;
100         if($user_username->find() && $user_username->fetch()){
101             $entry = $this->ldapCommon->get_user($user_username->username);
102             if($entry){
103                 if(isset($this->roles_to_groups[$name])){
104                     if(is_array($this->roles_to_groups[$name])){
105                         foreach($this->roles_to_groups[$name] as $group){
106                             if($this->ldapCommon->is_dn_member_of_group($entry->dn(),$group)){
107                                 return true;
108                             }
109                         }
110                     }else{
111                         if($this->ldapCommon->is_dn_member_of_group($entry->dn(),$this->roles_to_groups[$name])){
112                             return true;
113                         }
114                     }
115                 }
116             }
117         }
118         return false;
119     }
120
121     function onPluginVersion(&$versions)
122     {
123         $versions[] = array('name' => 'LDAP Authorization',
124                             'version' => STATUSNET_VERSION,
125                             'author' => 'Craig Andrews',
126                             'homepage' => 'http://status.net/wiki/Plugin:LdapAuthorization',
127                             'rawdescription' =>
128                             // TRANS: Plugin description.
129                             _m('The LDAP Authorization plugin allows for StatusNet to handle authorization through LDAP.'));
130         return true;
131     }
132 }