backend.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @copyright 2012 Frank Karlitschek frank@owncloud.org
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * error code for functions not provided by the group backend
  24. */
  25. define('OC_GROUP_BACKEND_NOT_IMPLEMENTED', -501);
  26. /**
  27. * actions that user backends can define
  28. */
  29. define('OC_GROUP_BACKEND_CREATE_GROUP', 0x00000001);
  30. define('OC_GROUP_BACKEND_DELETE_GROUP', 0x00000010);
  31. define('OC_GROUP_BACKEND_ADD_TO_GROUP', 0x00000100);
  32. define('OC_GROUP_BACKEND_REMOVE_FROM_GOUP', 0x00001000);
  33. define('OC_GROUP_BACKEND_GET_DISPLAYNAME', 0x00010000);
  34. /**
  35. * Abstract base class for user management
  36. */
  37. abstract class OC_Group_Backend implements OC_Group_Interface {
  38. protected $possibleActions = array(
  39. OC_GROUP_BACKEND_CREATE_GROUP => 'createGroup',
  40. OC_GROUP_BACKEND_DELETE_GROUP => 'deleteGroup',
  41. OC_GROUP_BACKEND_ADD_TO_GROUP => 'addToGroup',
  42. OC_GROUP_BACKEND_REMOVE_FROM_GOUP => 'removeFromGroup',
  43. OC_GROUP_BACKEND_GET_DISPLAYNAME => 'displayNamesInGroup',
  44. );
  45. /**
  46. * @brief Get all supported actions
  47. * @return int bitwise-or'ed actions
  48. *
  49. * Returns the supported actions as int to be
  50. * compared with OC_USER_BACKEND_CREATE_USER etc.
  51. */
  52. public function getSupportedActions() {
  53. $actions = 0;
  54. foreach($this->possibleActions AS $action => $methodName) {
  55. if(method_exists($this, $methodName)) {
  56. $actions |= $action;
  57. }
  58. }
  59. return $actions;
  60. }
  61. /**
  62. * @brief Check if backend implements actions
  63. * @param int $actions bitwise-or'ed actions
  64. * @return boolean
  65. *
  66. * Returns the supported actions as int to be
  67. * compared with OC_GROUP_BACKEND_CREATE_GROUP etc.
  68. */
  69. public function implementsActions($actions) {
  70. return (bool)($this->getSupportedActions() & $actions);
  71. }
  72. /**
  73. * @brief is user in group?
  74. * @param string $uid uid of the user
  75. * @param string $gid gid of the group
  76. * @return bool
  77. *
  78. * Checks whether the user is member of a group or not.
  79. */
  80. public function inGroup($uid, $gid) {
  81. return in_array($gid, $this->getUserGroups($uid));
  82. }
  83. /**
  84. * @brief Get all groups a user belongs to
  85. * @param string $uid Name of the user
  86. * @return array with group names
  87. *
  88. * This function fetches all groups a user belongs to. It does not check
  89. * if the user exists at all.
  90. */
  91. public function getUserGroups($uid) {
  92. return array();
  93. }
  94. /**
  95. * @brief get a list of all groups
  96. * @param string $search
  97. * @param int $limit
  98. * @param int $offset
  99. * @return array with group names
  100. *
  101. * Returns a list with all groups
  102. */
  103. public function getGroups($search = '', $limit = -1, $offset = 0) {
  104. return array();
  105. }
  106. /**
  107. * check if a group exists
  108. * @param string $gid
  109. * @return bool
  110. */
  111. public function groupExists($gid) {
  112. return in_array($gid, $this->getGroups($gid, 1));
  113. }
  114. /**
  115. * @brief get a list of all users in a group
  116. * @param string $gid
  117. * @param string $search
  118. * @param int $limit
  119. * @param int $offset
  120. * @return array with user ids
  121. */
  122. public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  123. return array();
  124. }
  125. /**
  126. * @brief get a list of all display names in a group
  127. * @param string $gid
  128. * @param string $search
  129. * @param int $limit
  130. * @param int $offset
  131. * @return array with display names (value) and user ids (key)
  132. */
  133. public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  134. $displayNames = array();
  135. $users = $this->usersInGroup($gid, $search, $limit, $offset);
  136. foreach ($users as $user) {
  137. $displayNames[$user] = $user;
  138. }
  139. return $displayNames;
  140. }
  141. }