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;
54 function onInitializePlugin(){
55 parent::onInitializePlugin();
56 if(!isset($this->host)){
57 throw new Exception("must specify a host");
59 if(!isset($this->basedn)){
60 throw new Exception("must specify a basedn");
62 if(!isset($this->provider_name)){
63 throw new Exception("provider_name must be set. Use the provider_name from the LDAP Authentication plugin.");
65 if(!isset($this->uniqueMember_attribute)){
66 throw new Exception("uniqueMember_attribute must be set.");
68 if(!isset($this->roles_to_groups)){
69 throw new Exception("roles_to_groups must be set.");
73 //---interface implementation---//
74 function loginAllowed($user) {
75 $user_username = new User_username();
76 $user_username->user_id=$user->id;
77 $user_username->provider_name=$this->provider_name;
78 if($user_username->find() && $user_username->fetch()){
79 $entry = $this->ldap_get_user($user_username->username);
81 if(isset($this->login_group)){
82 if(is_array($this->login_group)){
83 foreach($this->login_group as $group){
84 if($this->isMemberOfGroup($entry->dn(),$group)){
89 if($this->isMemberOfGroup($entry->dn(),login_group)){
95 //if a user exists, we can assume he's allowed to login
106 function hasRole($profile, $name) {
107 $user_username = new User_username();
108 $user_username->user_id=$profile->id;
109 $user_username->provider_name=$this->provider_name;
110 if($user_username->find() && $user_username->fetch()){
111 $entry = $this->ldap_get_user($user_username->username);
113 if(isset($this->roles_to_groups[$name])){
114 if(is_array($this->roles_to_groups[$name])){
115 foreach($this->roles_to_groups[$name] as $group){
116 if($this->isMemberOfGroup($entry->dn(),$group)){
121 if($this->isMemberOfGroup($entry->dn(),$this->roles_to_groups[$name])){
131 function isMemberOfGroup($userDn, $groupDn)
133 $ldap = ldap_get_connection();
134 $link = $ldap->getLink();
135 $r = ldap_compare($link, $groupDn, $this->uniqueMember_attribute, $userDn);
138 }else if($r === false){
141 common_log(LOG_ERR, ldap_error($r));