3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to enable LDAP Authorization
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 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/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
34 require_once INSTALLDIR.'/plugins/Authorization/AuthorizationPlugin.php';
35 require_once 'Net/LDAP2.php';
37 class LdapAuthorizationPlugin extends AuthorizationPlugin
42 public $starttls=null;
49 public $provider_name = null;
50 public $uniqueMember_attribute = null;
51 public $roles_to_groups = null;
52 public $login_group = null;
53 public $attributes = array();
55 function onInitializePlugin(){
56 parent::onInitializePlugin();
57 if(!isset($this->host)){
58 throw new Exception("must specify a host");
60 if(!isset($this->basedn)){
61 throw new Exception("must specify a basedn");
63 if(!isset($this->provider_name)){
64 throw new Exception("provider_name must be set. Use the provider_name from the LDAP Authentication plugin.");
66 if(!isset($this->uniqueMember_attribute)){
67 throw new Exception("uniqueMember_attribute must be set.");
69 if(!isset($this->roles_to_groups)){
70 throw new Exception("roles_to_groups must be set.");
72 if(!isset($this->attributes['username'])){
73 throw new Exception("username attribute must be set.");
77 //---interface implementation---//
78 function loginAllowed($user) {
79 $user_username = new User_username();
80 $user_username->user_id=$user->id;
81 $user_username->provider_name=$this->provider_name;
82 if($user_username->find() && $user_username->fetch()){
83 $entry = $this->ldap_get_user($user_username->username);
85 if(isset($this->login_group)){
86 if(is_array($this->login_group)){
87 foreach($this->login_group as $group){
88 if($this->ldap_is_dn_member_of_group($entry->dn(),$group)){
93 if($this->ldap_is_dn_member_of_group($entry->dn(),$this->login_group)){
99 //if a user exists, we can assume he's allowed to login
110 function hasRole($profile, $name) {
111 $user_username = new User_username();
112 $user_username->user_id=$profile->id;
113 $user_username->provider_name=$this->provider_name;
114 if($user_username->find() && $user_username->fetch()){
115 $entry = $this->ldap_get_user($user_username->username);
117 if(isset($this->roles_to_groups[$name])){
118 if(is_array($this->roles_to_groups[$name])){
119 foreach($this->roles_to_groups[$name] as $group){
120 if($this->ldap_is_dn_member_of_group($entry->dn(),$group)){
125 if($this->ldap_is_dn_member_of_group($entry->dn(),$this->roles_to_groups[$name])){
135 function ldap_is_dn_member_of_group($userDn, $groupDn)
137 $ldap = $this->ldap_get_connection();
138 $link = $ldap->getLink();
139 $r = ldap_compare($link, $groupDn, $this->uniqueMember_attribute, $userDn);
142 }else if($r === false){
145 common_log(LOG_ERR, ldap_error($r));
150 function ldap_get_config(){
152 $keys = array('host','port','version','starttls','binddn','bindpw','basedn','options','filter','scope');
153 foreach($keys as $key){
154 $value = $this->$key;
156 $config[$key]=$value;
162 //-----the below function were copied from LDAPAuthenticationPlugin. They will be moved to a utility class soon.----\\
163 function ldap_get_connection($config = null){
165 $config = $this->ldap_get_config();
168 //cannot use Net_LDAP2::connect() as StatusNet uses
169 //PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
170 //PEAR handling can be overridden on instance objects, so we do that.
171 $ldap = new Net_LDAP2($config);
172 $ldap->setErrorHandling(PEAR_ERROR_RETURN);
174 if (Net_LDAP2::isError($err)) {
175 common_log(LOG_WARNING, 'Could not connect to LDAP server: '.$err->getMessage());
182 * get an LDAP entry for a user with a given username
184 * @param string $username
185 * $param array $attributes LDAP attributes to retrieve
188 function ldap_get_user($username,$attributes=array(),$ldap=null){
190 $ldap = $this->ldap_get_connection();
192 $filter = Net_LDAP2_Filter::create($this->attributes['username'], 'equals', $username);
194 'attributes' => $attributes
196 $search = $ldap->search(null,$filter,$options);
198 if (PEAR::isError($search)) {
199 common_log(LOG_WARNING, 'Error while getting DN for user: '.$search->getMessage());
203 if($search->count()==0){
205 }else if($search->count()==1){
206 $entry = $search->shiftEntry();
209 common_log(LOG_WARNING, 'Found ' . $search->count() . ' ldap user with the username: ' . $username);