groups.php 3.4 KB

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