hooks.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  6. *
  7. * @copyright Copyright (c) 2015, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  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, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Share;
  24. class Hooks extends \OC\Share\Constants {
  25. /**
  26. * Function that is called after a user is deleted. Cleans up the shares of that user.
  27. * @param array $arguments
  28. */
  29. public static function post_deleteUser($arguments) {
  30. // Delete any items shared with the deleted user
  31. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*share`'
  32. .' WHERE `share_with` = ? AND `share_type` = ? OR `share_type` = ?');
  33. $query->execute(array($arguments['uid'], self::SHARE_TYPE_USER, self::$shareTypeGroupUserUnique));
  34. // Delete any items the deleted user shared
  35. $query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*share` WHERE `uid_owner` = ?');
  36. $result = $query->execute(array($arguments['uid']));
  37. while ($item = $result->fetchRow()) {
  38. Helper::delete($item['id']);
  39. }
  40. }
  41. /**
  42. * Function that is called after a user is added to a group.
  43. * TODO what does it do?
  44. * @param array $arguments
  45. */
  46. public static function post_addToGroup($arguments) {
  47. // Find the group shares and check if the user needs a unique target
  48. $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `share_type` = ? AND `share_with` = ?');
  49. $result = $query->execute(array(self::SHARE_TYPE_GROUP, $arguments['gid']));
  50. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`item_type`, `item_source`,'
  51. .' `item_target`, `parent`, `share_type`, `share_with`, `uid_owner`, `permissions`,'
  52. .' `stime`, `file_source`, `file_target`) VALUES (?,?,?,?,?,?,?,?,?,?,?)');
  53. while ($item = $result->fetchRow()) {
  54. $sourceExists = \OC\Share\Share::getItemSharedWithBySource($item['item_type'], $item['item_source'], self::FORMAT_NONE, null, true, $arguments['uid']);
  55. if ($sourceExists) {
  56. $fileTarget = $sourceExists['file_target'];
  57. $itemTarget = $sourceExists['item_target'];
  58. } else {
  59. $itemTarget = Helper::generateTarget($item['item_type'], $item['item_source'], self::SHARE_TYPE_USER, $arguments['uid'],
  60. $item['owner'], null, $item['parent']);
  61. // do we also need a file target
  62. if ($item['item_type'] === 'file' || $item['item_type'] === 'folder') {
  63. $fileTarget = Helper::generateTarget('file', $item['file_target'], self::SHARE_TYPE_USER, $arguments['uid'],
  64. $item['owner'], null, $item['parent']);
  65. } else {
  66. $fileTarget = null;
  67. }
  68. }
  69. // Insert an extra row for the group share if the item or file target is unique for this user
  70. if ($itemTarget != $item['item_target'] || $fileTarget != $item['file_target']) {
  71. $query->execute(array($item['item_type'], $item['item_source'], $itemTarget, $item['id'],
  72. self::$shareTypeGroupUserUnique, $arguments['uid'], $item['uid_owner'], $item['permissions'],
  73. $item['stime'], $item['file_source'], $fileTarget));
  74. \OC_DB::insertid('*PREFIX*share');
  75. }
  76. }
  77. }
  78. /**
  79. * Function that is called after a user is removed from a group. Shares are cleaned up.
  80. * @param array $arguments
  81. */
  82. public static function post_removeFromGroup($arguments) {
  83. $sql = 'SELECT `id`, `share_type` FROM `*PREFIX*share`'
  84. .' WHERE (`share_type` = ? AND `share_with` = ?) OR (`share_type` = ? AND `share_with` = ?)';
  85. $result = \OC_DB::executeAudited($sql, array(self::SHARE_TYPE_GROUP, $arguments['gid'],
  86. self::$shareTypeGroupUserUnique, $arguments['uid']));
  87. while ($item = $result->fetchRow()) {
  88. if ($item['share_type'] == self::SHARE_TYPE_GROUP) {
  89. // Delete all reshares by this user of the group share
  90. Helper::delete($item['id'], true, $arguments['uid']);
  91. } else {
  92. Helper::delete($item['id']);
  93. }
  94. }
  95. }
  96. /**
  97. * Function that is called after a group is removed. Cleans up the shares to that group.
  98. * @param array $arguments
  99. */
  100. public static function post_deleteGroup($arguments) {
  101. $sql = 'SELECT `id` FROM `*PREFIX*share` WHERE `share_type` = ? AND `share_with` = ?';
  102. $result = \OC_DB::executeAudited($sql, array(self::SHARE_TYPE_GROUP, $arguments['gid']));
  103. while ($item = $result->fetchRow()) {
  104. Helper::delete($item['id']);
  105. }
  106. }
  107. }