]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/LdapAuthorization/LdapAuthorizationPlugin.php
20bbd256257fb16af9437c602bfb38d47569b01a
[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 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 require_once INSTALLDIR.'/plugins/Authorization/AuthorizationPlugin.php';
35 require_once 'Net/LDAP2.php';
36
37 class LdapAuthorizationPlugin extends AuthorizationPlugin
38 {
39     public $host=null;
40     public $port=null;
41     public $version=null;
42     public $starttls=null;
43     public $binddn=null;
44     public $bindpw=null;
45     public $basedn=null;
46     public $options=null;
47     public $filter=null;
48     public $scope=null;
49     public $provider_name = null;
50     public $uniqueMember_attribute = null;
51     public $roles_to_groups = null;
52
53     function onInitializePlugin(){
54         parent::onInitializePlugin();
55         if(!isset($this->host)){
56             throw new Exception("must specify a host");
57         }
58         if(!isset($this->basedn)){
59             throw new Exception("must specify a basedn");
60         }
61         if(!isset($this->provider_name)){
62             throw new Exception("provider_name must be set. Use the provider_name from the LDAP Authentication plugin.");
63         }
64         if(!isset($this->uniqueMember_attribute)){
65             throw new Exception("uniqueMember_attribute must be set.");
66         }
67         if(!isset($this->roles_to_groups)){
68             throw new Exception("roles_to_groups must be set.");
69         }
70     }
71
72     //---interface implementation---//
73     function loginAllowed($user) {
74         $user_username = new User_username();
75         $user_username->user_id=$user->id;
76         $user_username->provider_name=$this->provider_name;
77         if($user_username->find() && $user_username->fetch()){
78             $entry = $this->ldap_get_user($user_username->username);
79             if($entry){
80                 //if a user exists, we can assume he's allowed to login
81                 return true;
82             }else{
83                 return null;
84             }
85         }else{
86             return null;
87         }
88     }
89
90     function hasRole($profile, $name) {
91         $user_username = new User_username();
92         $user_username->user_id=$profile->id;
93         $user_username->provider_name=$this->provider_name;
94         if($user_username->find() && $user_username->fetch()){
95             $entry = $this->ldap_get_user($user_username->username);
96             if($entry){
97                 if(isset($this->roles_to_groups[$name])){
98                     if(is_array($this->roles_to_groups[$name])){
99                         foreach($this->roles_to_groups[$name] as $group){
100                             if($this->isMemberOfGroup($entry->dn(),$group)){
101                                 return true;
102                             }
103                         }
104                     }else{
105                         if($this->isMemberOfGroup($entry->dn(),$this->roles_to_groups[$name])){
106                             return true;
107                         }
108                     }
109                 }
110             }
111         }
112         return false;
113     }
114
115     function isMemberOfGroup($userDn, $groupDn)
116     {
117         $ldap = ldap_get_connection();
118         $link = $ldap->getLink();
119         $r = ldap_compare($link, $groupDn, $this->uniqueMember_attribute, $userDn);
120         if ($r === true){
121             return true;
122         }else if($r === false){
123             return false;
124         }else{
125             common_log(LOG_ERR, ldap_error($r));
126             return false;
127         }
128     }
129 }