LdapAuthorizationPlugin.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. if (!defined('STATUSNET') && !defined('LACONICA')) {
  30. exit(1);
  31. }
  32. class LdapAuthorizationPlugin extends AuthorizationPlugin
  33. {
  34. public $roles_to_groups = array();
  35. public $login_group = null;
  36. function onInitializePlugin(){
  37. if(!isset($this->provider_name)){
  38. // TRANS: Exception thrown when initialising the LDAP Auth plugin fails because of an incorrect configuration.
  39. throw new Exception(_m('provider_name must be set. Use the provider_name from the LDAP Authentication plugin.'));
  40. }
  41. if(!isset($this->uniqueMember_attribute)){
  42. // TRANS: Exception thrown when initialising the LDAP Auth plugin fails because of an incorrect configuration.
  43. throw new Exception(_m('uniqueMember_attribute must be set.'));
  44. }
  45. $this->ldapCommon = new LdapCommon(get_object_vars($this));
  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. return parent::onAutoload($cls);
  56. }
  57. //---interface implementation---//
  58. function loginAllowed($user) {
  59. $user_username = new User_username();
  60. $user_username->user_id=$user->id;
  61. $user_username->provider_name=$this->provider_name;
  62. if($user_username->find() && $user_username->fetch()){
  63. $entry = $this->ldapCommon->get_user($user_username->username);
  64. if($entry){
  65. if(isset($this->login_group)){
  66. if(is_array($this->login_group)){
  67. foreach($this->login_group as $group){
  68. if($this->ldapCommon->is_dn_member_of_group($entry->dn(),$group)){
  69. return true;
  70. }
  71. }
  72. }else{
  73. if($this->ldapCommon->is_dn_member_of_group($entry->dn(),$this->login_group)){
  74. return true;
  75. }
  76. }
  77. return null;
  78. }else{
  79. //if a user exists, we can assume he's allowed to login
  80. return true;
  81. }
  82. }else{
  83. return null;
  84. }
  85. }else{
  86. return null;
  87. }
  88. }
  89. function hasRole($profile, $name) {
  90. $user_username = new User_username();
  91. $user_username->user_id=$profile->id;
  92. $user_username->provider_name=$this->provider_name;
  93. if($user_username->find() && $user_username->fetch()){
  94. $entry = $this->ldapCommon->get_user($user_username->username);
  95. if($entry){
  96. if(isset($this->roles_to_groups[$name])){
  97. if(is_array($this->roles_to_groups[$name])){
  98. foreach($this->roles_to_groups[$name] as $group){
  99. if($this->ldapCommon->is_dn_member_of_group($entry->dn(),$group)){
  100. return true;
  101. }
  102. }
  103. }else{
  104. if($this->ldapCommon->is_dn_member_of_group($entry->dn(),$this->roles_to_groups[$name])){
  105. return true;
  106. }
  107. }
  108. }
  109. }
  110. }
  111. return false;
  112. }
  113. function onPluginVersion(array &$versions)
  114. {
  115. $versions[] = array('name' => 'LDAP Authorization',
  116. 'version' => GNUSOCIAL_VERSION,
  117. 'author' => 'Craig Andrews',
  118. 'homepage' => 'http://status.net/wiki/Plugin:LdapAuthorization',
  119. 'rawdescription' =>
  120. // TRANS: Plugin description.
  121. _m('The LDAP Authorization plugin allows for StatusNet to handle authorization through LDAP.'));
  122. return true;
  123. }
  124. }