AuthorizationPlugin.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Superclass for plugins that do 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. /**
  33. * Superclass for plugins that do authorization
  34. *
  35. * @category Module
  36. * @package GNUsocial
  37. * @author Craig Andrews <candrews@integralblue.com>
  38. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  39. * @link http://status.net/
  40. */
  41. abstract class AuthorizationPlugin extends Plugin
  42. {
  43. //is this plugin authoritative for authorization?
  44. public $authoritative = false;
  45. //------------Auth plugin should implement some (or all) of these methods------------\\
  46. /**
  47. * Is a user allowed to log in?
  48. * @param user
  49. * @return boolean true if the user is allowed to login, false if explicitly not allowed to login, null if we don't explicitly allow or deny login
  50. */
  51. function loginAllowed($user) {
  52. return null;
  53. }
  54. /**
  55. * Does a profile grant the user a named role?
  56. * @param profile
  57. * @return boolean true if the profile has the role, false if not
  58. */
  59. function hasRole($profile, $name) {
  60. return false;
  61. }
  62. //------------Below are the methods that connect StatusNet to the implementing Auth plugin------------\\
  63. function onStartSetUser($user) {
  64. $loginAllowed = $this->loginAllowed($user);
  65. if($loginAllowed === true){
  66. return;
  67. }else if($loginAllowed === false){
  68. $user = null;
  69. return false;
  70. }else{
  71. if($this->authoritative) {
  72. $user = null;
  73. return false;
  74. }else{
  75. return;
  76. }
  77. }
  78. }
  79. function onStartSetApiUser($user) {
  80. return $this->onStartSetUser($user);
  81. }
  82. function onStartHasRole($profile, $name, &$has_role) {
  83. if($this->hasRole($profile, $name)){
  84. $has_role = true;
  85. return false;
  86. }else{
  87. if($this->authoritative) {
  88. $has_role = false;
  89. return false;
  90. }else{
  91. return;
  92. }
  93. }
  94. }
  95. }