groups.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @copyright (C) 2014 ownCloud, Inc.
  6. *
  7. * @author Tom <tom@owncloud.com>
  8. * @author Bart Visscher
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  12. * License as published by the Free Software Foundation; either
  13. * version 3 of the License, or any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public
  21. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Provisioning_API;
  25. use \OC_OCS_Result;
  26. use \OC_Group;
  27. use \OC_SubAdmin;
  28. class Groups{
  29. /**
  30. * returns a list of groups
  31. */
  32. public static function getGroups($parameters){
  33. $search = !empty($_GET['search']) ? $_GET['search'] : '';
  34. $limit = !empty($_GET['limit']) ? $_GET['limit'] : null;
  35. $offset = !empty($_GET['offset']) ? $_GET['offset'] : null;
  36. return new OC_OCS_Result(array('groups' => OC_Group::getGroups($search, $limit, $offset)));
  37. }
  38. /**
  39. * returns an array of users in the group specified
  40. */
  41. public static function getGroup($parameters){
  42. // Check the group exists
  43. if(!OC_Group::groupExists($parameters['groupid'])){
  44. return new OC_OCS_Result(null, \OC_API::RESPOND_NOT_FOUND, 'The requested group could not be found');
  45. }
  46. // Check subadmin has access to this group
  47. if(\OC_User::isAdminUser(\OC_User::getUser())
  48. || in_array($parameters['groupid'], \OC_SubAdmin::getSubAdminsGroups(\OC_User::getUser()))){
  49. return new OC_OCS_Result(array('users' => OC_Group::usersInGroup($parameters['groupid'])));
  50. } else {
  51. return new OC_OCS_Result(null, \OC_API::RESPOND_UNAUTHORISED, 'User does not have access to specified group');
  52. }
  53. }
  54. /**
  55. * creates a new group
  56. */
  57. public static function addGroup($parameters){
  58. // Validate name
  59. $groupid = isset($_POST['groupid']) ? $_POST['groupid'] : '';
  60. if( preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $groupid ) || empty($groupid)){
  61. \OC_Log::write('provisioning_api', 'Attempt made to create group using invalid characters.', \OC_Log::ERROR);
  62. return new OC_OCS_Result(null, 101, 'Invalid group name');
  63. }
  64. // Check if it exists
  65. if(OC_Group::groupExists($groupid)){
  66. return new OC_OCS_Result(null, 102);
  67. }
  68. if(OC_Group::createGroup($groupid)){
  69. return new OC_OCS_Result(null, 100);
  70. } else {
  71. return new OC_OCS_Result(null, 103);
  72. }
  73. }
  74. public static function deleteGroup($parameters){
  75. // Check it exists
  76. if(!OC_Group::groupExists($parameters['groupid'])){
  77. return new OC_OCS_Result(null, 101);
  78. } else if($parameters['groupid'] == 'admin' || !OC_Group::deleteGroup($parameters['groupid'])){
  79. // Cannot delete admin group
  80. return new OC_OCS_Result(null, 102);
  81. } else {
  82. return new OC_OCS_Result(null, 100);
  83. }
  84. }
  85. public static function getSubAdminsOfGroup($parameters) {
  86. $group = $parameters['groupid'];
  87. // Check group exists
  88. if(!OC_Group::groupExists($group)) {
  89. return new OC_OCS_Result(null, 101, 'Group does not exist');
  90. }
  91. // Go
  92. if(!$subadmins = OC_Subadmin::getGroupsSubAdmins($group)) {
  93. return new OC_OCS_Result(null, 102, 'Unknown error occured');
  94. } else {
  95. return new OC_OCS_Result($subadmins);
  96. }
  97. }
  98. }